通过python发送gmail电子邮件+;smtplib。Can';不要把身体和主体分开

通过python发送gmail电子邮件+;smtplib。Can';不要把身体和主体分开,python,character-encoding,smtp,gmail,smtplib,Python,Character Encoding,Smtp,Gmail,Smtplib,我想通过python脚本从我的gmail帐户发送电子邮件。 我的代码也能工作 唯一的问题是我希望电子邮件有主题和正文。 但我的剧本把一切都放在了主题上 这是我的密码: import smtplib with smtplib.SMTP("smtp.gmail.com", 587) as smtp: smtp.ehlo() smtp.starttls() smtp.ehlo() smtp.login("MY EMAIL", "MY PASSWORD")

我想通过python脚本从我的gmail帐户发送电子邮件。
我的代码也能工作

唯一的问题是我希望电子邮件有主题和正文。
但我的剧本把一切都放在了主题上

这是我的密码:

import smtplib

with smtplib.SMTP("smtp.gmail.com", 587) as smtp:
    smtp.ehlo()
    smtp.starttls()
    smtp.ehlo()

    smtp.login("MY EMAIL", "MY PASSWORD")

    subject = "Contract Details"
    body = "Grettings. Here are the contract details. Thank you."

    msg = f"""Subject: {subject}\n\n{body}"""
    email = "MY EMAIL"
    def encode(x):
        """ The replaces in this function fix some problems in the encoding.
            For now, they are not that relevant. They work. """
        return str(x.encode("ascii", errors="ignore")).replace("b'", "").replace("'", "")

    #  I'm sending it to myself, so the first two arguments are the same:
    smtp.sendmail(encode(email), encode(email), encode(msg))
我已经尝试在网上找到解决方案,很多视频和文章都说应该用两行新行将主题和身体分开。
这就是为什么我使用
msg=f”““Subject:{Subject}\n\n{body}”“”

然而,我明白了:

正如你所看到的,我的主体和身体都在主体中(红色),而身体是空白的

我还尝试了无特征线、一条特征线、三条特征线。
我甚至试着写
msg=f”““Subject:{Subject}\n\n body:{body}”“”

但什么都不管用

如何将身体与主体分开?

好的,所以我改变了:

smtp.sendmail(encode(email), encode(email), encode(msg))
致:

现在它工作了。

好的,所以我改变了:

smtp.sendmail(encode(email), encode(email), encode(msg))
致:


现在它起作用了。

@snakecharmerb现在试过了,不起作用either@snakecharmerb现在试过了,也没用