Search code examples
pythonemailoutlooksmtplibimaplib

python draft outlook email with attachments, html body and table in body text?


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()

Solution

  • 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/