Python:如何将电子邮件文本正文传递到Office365电子邮件

时间:2019-03-17 18:00:25

标签: python python-3.x email office365

尝试使以下脚本适用于Office365。发送一封电子邮件,但我无法获取脚本来识别实际的电子邮件文本正文(仅发送主题行)。以下脚本适用于gmail。有什么需要修改的想法吗?

谢谢!

import smtplib, ssl

port = 587 
smtp_server = "smtp.office365.com"
sender_email = "me@email.com"
receiver_email = {'User1': 'user1@email.com'}
password = "password"

subject = input('Enter the subject line: ')
message = input('Enter the message: ')

email = """\
Subject: %s
%s
""" % (subject, message)

for key, value in receiver_email.items():                   

    context = ssl.create_default_context()
    with smtplib.SMTP(smtp_server, port) as server:
        server.ehlo()  # Can be omitted
        server.starttls(context=context)
        server.ehlo()  # Can be omitted
        server.login(sender_email, password)
        server.sendmail(sender_email, value, email)               
        server.quit()

1 个答案:

答案 0 :(得分:0)

在电子邮件对象中缺少“ \ n”。现在可以使用了。

email = """\
Subject: %s\n
%s
""" % (subject, message)