0

I am able to send an email using smtplib package using the below code

But I want to just save it as draft in my outlook mailbox.

But I don't know how can I save it as draft using smtplib.

Is there any package that we have that can allow me to use html and css tags, attach files, write email body text, include multiple To and cc recepients and save it as draft but not send them

The reason why am sharing full code is because I want to be able to do all the tasks, formattings, attachments and save it as draft (but not send) in outlook.

Can help me know how can we save an outlook message as draft in outlook using python?

for x in filenames:
    print(os.path.basename(x))
    filename = os.path.basename(x)
    file_pattern = filename.split('_2020')[0]
    print(file_pattern)
    
    data = pd.read_csv(filename)
    output = build_table(data,'blue_light', font_size='8px',font_family='Open Sans,sans-serif',
                     text_align='center',width='70px',index=False,even_color='black',even_bg_color='white')
    
    temp_email_df = email_list[email_list['Region']==file_pattern]
    rec_list.append(temp_email_df.iloc[0,4:].to_string(header=False, index=False))
    print(rec_list)
    print(type(rec_list))
    cc_recipients = ['abc@company.com']
    message = MIMEMultipart()
    message['Subject'] = 'For your review - records'
    message['From'] = 'def@company.com'
    message['To'] = ", ".join(rec_list)
    message['Cc'] = ", ".join(cc_recipients)
    rec_list.extend(cc_recipients)
    
    name = temp_email_df.iloc[0,3:4].to_string(header=False, index=False)
    top_text = """
    <html><body><p>Hello """ + name+"," """ </p>
    <p>Based on our examination of data, please do the below</p>
    <p>These are identified based on factors such as: </p>
    a) factor1<br>
    b) factor2<br>
    c) factor3<br>
    d) factor4<br>
    e) factor5<br>
    f) factor6<br>
    <p> </p>

    <p> Appreciate your support in following up the cases as referenced below (which is also available as email attachment). </p>
    
    </body></html>
    """ 
    bottom_text = """
    <html><body>
    <p>If you have any questions, please do let me know.</p>
     Regards,<br>
     Abc<br>
    </body></html>"""
    
    
    part1 = top_text
    part2 = output
    part3 = bottom_text
    partfinal = part1 + part2 + part3
    message.attach(MIMEText(partfinal, "html"))
    
    with open(filename, "rb") as attachment:
        part = MIMEBase("application", "octet-stream")
        part.set_payload(attachment.read())
        encoders.encode_base64(part)

# Add header as key/value pair to attachment part
    part.add_header(
    "Content-Disposition",
    f"attachment; filename= {filename}",
    )
    
    # Add attachment to message and convert message to string
    message.attach(part)
    
    msg_body = message.as_string()
    
    
    server = SMTP('mail-test.company.com', 25)
    #server.send_message(msg_body,message['From'],message['To'])
    server.sendmail(message['From'],rec_list,msg_body)
    server.quit()
    rec_list.clear()
The Great
  • 7,215
  • 7
  • 40
  • 128
  • Is `smtplib` mandatory? It is possible using the win32api and outlook but then you need outlook installed. – Finn Jul 29 '22 at 06:51
  • @Finn - would that allow me to include attachements, add html content and plain text like I have above in post, include table in email body etc? If yes, can help me with it is please? What do you think about imaplib? – The Great Jul 29 '22 at 06:55
  • There are good examples available online like [this](https://stackoverflow.com/questions/58602116/define-mailbox-to-which-to-save-an-email-win32client-python) and [this](https://gist.github.com/ITSecMedia/b45d21224c4ea16bf4a72e2a03f741af) . – Finn Jul 29 '22 at 07:18
  • Please don't randomly ping people who have answered tangentially related questions. – tripleee Jul 29 '22 at 09:54
  • @tripleee - Sorry, I thought it was related question. Hence, I linked my post there. Apologies if you found it was not related – The Great Jul 29 '22 at 10:00
  • As an aside, your code seems to be written for Python 3.5 or earlier. The `email` library was overhauled in 3.6 and is now quite a bit more versatile and logical. Probably throw away what you have and start over with the [examples from the `email` documentation.](https://docs.python.org/3/library/email.examples.html) – tripleee Jul 29 '22 at 10:05

1 Answers1

2

SMTP is not connected to Outlook in any way. smtplib connects directly to the recipients' mail servers.

If you want to manipulate Outlook, you can use COM services, like

outlook = win32com.Dispatch('outlook.application')

Here's a summary of the COM process: https://www.codeforests.com/2020/06/05/how-to-send-email-from-outlook/

Tim Roberts
  • 48,973
  • 4
  • 21
  • 30
  • Nice. Can show me a simple example using `win32` on how to include attachments, html body, table in email body etc? – The Great Jul 29 '22 at 06:56
  • 1
    what about imaplib? does it allow to do the tasks that I mentioned and save it as draft message? – The Great Jul 29 '22 at 06:57
  • 1
    Depends on what Outlook is connected to and how. If your mailbox is on an IMAP server, you should certainly be able to add messages to arbitrary folders. – tripleee Jul 29 '22 at 09:53
  • If your service provider does IMAP, and your "Drafts" folders is one of the IMAP folders, then that's the easy solution. Here's a summary of the COM process: https://www.codeforests.com/2020/06/05/how-to-send-email-from-outlook/ – Tim Roberts Jul 29 '22 at 18:40
  • I managed to solve this problem with the help of win32 but one issue. While I am able to see the message in `drafts` folder but when I click on `send` after reviewing the draft, I see it stays in outbox forever (but doesn't get sent). My code does `mail_item.Save()` to save it in draft. I wish to manually click send but is there any reason why is it outbox and doesn't get sent at all. – The Great Jul 31 '22 at 11:25
  • You're triple-checked the recipient addresses? That's one reason for hanging in outbox. – Tim Roberts Jul 31 '22 at 22:53