E-mail 使用 smtplib 发送以垃圾邮件结尾

E-mail sent with smtplib ends as spam

我有以下代码:

import smtplib, ssl


def send_email(temperature):
    port = 465  # For SSL
    password = "my_password"
    sender_email = "my_sender@gmail.com"
    receiver_email = "my_receiver@x.y"
    message = """\
    Subject: Temperature is %0.2f degrees C

     """ % temperature

    server = smtplib.SMTP_SSL("smtp.gmail.com", port)
    server.login(sender_email, password)
    server.sendmail(sender_email, receiver_email, message)

if __name__ == "__main__":
    send_email(7.7)

第一条运行后,第一条消息收到OK。 接下来的邮件位于垃圾邮件文件夹中,没有发件人地址、主题和 body。我试着标记它 因为不是垃圾邮件,但它没有帮助。 邮件 headers 具有正确的发件人地址、主题和 body。

我能改正吗?

我是这样解决的:

import smtplib, ssl
from email.mime.text import MIMEText

def send_email(temperature):
    port = 465  # For SSL
    password = "my_password"
    sender_email = "my_sender_address@gmail.com"
    receiver_email = "my_receiver_address.x.y"
    message = MIMEText("Temperature is %0.2f degrees" % temperature)
    message['Subject'] = "%0.2f degrees" % temperature
    message['From'] = sender_email
    message['To'] = receiver_email

    server = smtplib.SMTP_SSL("smtp.gmail.com", port)
    server.login(sender_email, password)
    server.sendmail(sender_email, [receiver_email], message.as_string())
    server.quit()

if __name__ == "__main__":
    send_email(7.7)