简体   繁体   中英

Microsoft Outlook python new line character

I am trying to figure out how to send some output from a python script to my outlook mailbox. Now I'm having some issues with formatting. My original method is below

def email_mailbox(file_checked, error_list):
    error_msg = ''
    for e in error_list:
        if e is not None:
            e += '\n'
            error_msg += e
    if len(error_msg) > 1:
        error_msg += '\nAutomated Test Result: FAILED'
    else:
        error_msg = 'Automated Test Result: PASSED'
    msg = MIMEText(error_msg)
    msg['Subject'] = 'Automated Review ' + file_checked[:-6]
    msg['From'] = 'a'
    msg['To'] = 'b'
    s = smtplib.SMTP('smlsmtp')
    s.sendmail('a', 'b', msg.as_string())
    s.quit()
    print(file_checked + ' Review Email Sent')

Consider automating Outlook from your preferred programming language instead. See How to automate Outlook from another program and C# app automates Outlook (CSAutomateOutlook) . The Chapter 17: Working with Item Bodies article in MSDN describes all possible ways of working with message bodies.

Be aware, Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.

If you are building a solution that runs in a server-side context, you should try to use components that have been made safe for unattended execution. Or, you should try to find alternatives that allow at least part of the code to run client-side. If you use an Office application from a server-side solution, the application will lack many of the necessary capabilities to run successfully. Additionally, you will be taking risks with the stability of your overall solution. See Considerations for server-side Automation of Office for more information.

Definitely late for this answer, but someone might end up here as I did. Using the win32com library to automate Outlook I ended up setting the body format to 2 and use HTMLBody for the message. Of course keep the answer regarding automating MS products in mind.

import win32com.client as win32
olApp = win32.Dispatch('Outlook.Application')
olNS = olApp.GetNameSpace('MAPI')

mailItem = olApp.CreateItem(0)
mailItem.Subject = 'Subject'
mailItem.BodyFormat = 2
mailItem.HTMLBody = "<html>Some text<br>New line</html>"
mailItem.To = 'myself@example.com'
mailItem.Send()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM