Search code examples
pythonemailexchange-serverimapbcc

Python: how to store a draft email with BCC recipients to Exchange Server via IMAP?


I try to store a draft e-mail via IMAP to a folder running on MS Exchange. Everything ok, except that Bcc recipients don't get shown in the draft message stored on the server. Bcc recipients also don't receive the email if I send it with MS Outlook. If I read the message back with Python after I have stored it on the server, I can see the Bcc in the draft.

The following Python code reproduces this behavior:

import imaplib 
import time 
from email.MIMEMultipart import MIMEMultipart 
from email.MIMEText import MIMEText 

message = MIMEMultipart() 
message['Subject'] = 'Test Draft' 
message['From'] = '[email protected]' 
message['to'] = '[email protected]' 
message['cc'] = '[email protected]' 
message['bcc'] = '[email protected]' 
message.attach(MIMEText('This is a test.\n')) 

server= imaplib.IMAP4('the.ser.ver.ip') 
server.login('test', 'test') 
server.append("Drafts" 
              ,'\Draft' 
              ,imaplib.Time2Internaldate(time.time()) 
              ,str(message)) 
server.logout() 

If I run this code, a draft gets stored into the Draft folder on the Exchange Server. But if I look at the draft with MS Outlook, it does not include the bcc recipient (message['bcc'] = '[email protected]'). Message, to, from, cc ok, no error.

If I download drafts that already include a bcc from an Exchange folder, I can also see the bcc. Only uploading doesn't work for me.

Any help very much appreciated. Thanks. BTW, MAPI is not an option.

Update: X-Receiver didn't work for me. As for playing around with an IMAP-Folder in Outlook, I got an interesting result. If I access the draft via the IMAP-Folder in Outlook, I see the bcc. But if I access it via the MAPI-Folder, I don't see it. Will play a little bit around with that.

Conclusion: Actually, the code works just fine. See below for the answer that I found.


Solution

  • Actually, the code works just fine. It creates the proper mail with all the right headers including bcc.

    How does the mail client display bcc?

    The mail client (e.g. Python or MS Outlook via IMAP or MAPI in my case) decides whether and how to display bcc-headers. Outlook for example doesn't display bcc headers from an IMAP folder. This is a feature to hide bcc recipients from each other where they have not been stripped away from the mail before (it is not clear from the standard whether one bcc recipient is allowed to see all other bcc recipients or not, see Wikipedia).

    Who handles bcc upon sending an email?

    Suppose now that we have drafted a message in a mail client and stored it in an IMAP or MAPI folder. The server providing the IMAP / MAPI folders leaves the draft message unchanged. What happens to the bcc-headers upon sending the mail is implementation dependent, and might depend both on the mail client and the mail transfer agent (e.g. MS Exchange Server in my case). In a nutshell, people do not agree whether the mail client or the mail transfer agent is reponsible for removing bcc headers. It seems however to be the case that a majority of developers is of the opinion that it is the mail client's business with the mail transfer agent not touching the mail (e.g. MS Exchange, MS SMTP, Exim, OpenWave). In this case, the mail transfer agent sends the email to the recipient as defined in the RCPT TO: of the SMTP communication, and leaves the email unchanged otherwise. Some other mail transfer agents strip bcc headers from emails however (e.g. sendmail, Lotus Notes). A very thorough discussion can be found on the Exim mailing list starting here.

    In the case of MS Outlook and MS Exchange, MS Outlook never sends bcc (but sends individual emails for each bcc recipient) and MS Exchange does not touch the email headers, but sends the full email (possibly including bcc recipients) to the recipients defined in RCPT TO:.

    Conclusion

    I did not understand that there is no guaranteed behavior for bcc, and that usually the client handles bcc. I will rewrite my Python code to loop over bcc recipients and generate one email for each bcc recipient.