Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think the part you had wrong was how to replace the headers in the message, and the fact that you don't need to make a copy of the message, you can just operate directly on it after creating it from the raw data you fetched from the IMAP server.</p> <p>You did omit some detail so here's my complete solution with all details spelled out. Note that I'm putting the SMTP connection in STARTTLS mode since I need that and note that I've separated the IMAP phase and the SMTP phase from each other. Maybe you thought that altering the message would somehow alter it on the IMAP server? If you did, this should show you clearly that that doesn't happen.</p> <pre><code>import smtplib, imaplib, email imap_host = "mail.example.com" smtp_host = "mail.example.com" smtp_port = 587 user = "xyz" passwd = "xyz" msgid = 7 from_addr = "from.me@example.com" to_addr = "to.you@example.com" # open IMAP connection and fetch message with id msgid # store message data in email_data client = imaplib.IMAP4(imap_host) client.login(user, passwd) client.select('INBOX') status, data = client.fetch(msgid, "(RFC822)") email_data = data[0][1] client.close() client.logout() # create a Message instance from the email data message = email.message_from_string(email_data) # replace headers (could do other processing here) message.replace_header("From", from_addr) message.replace_header("To", to_addr) # open authenticated SMTP connection and send message with # specified envelope from and to addresses smtp = smtplib.SMTP(smtp_host, smtp_port) smtp.starttls() smtp.login(user, passwd) smtp.sendmail(from_addr, to_addr, message.as_string()) smtp.quit() </code></pre> <p>Hope this helps even if this answer comes quite late.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload