在Python中将主题标题添加到server.sendmail()

在Python中将主题标题添加到server.sendmail(),python,email,smtp,Python,Email,Smtp,我正在编写一个python脚本来从终端发送电子邮件。在我目前发送的邮件中,它没有主题。我们如何在此电子邮件中添加主题 我当前的代码: import smtplib msg = """From: hello@hello.com To: hi@hi.com\n Here's my message!\nIt is lovely! """ server = smtplib.SMTP_SSL('smtp.example.com', port=465)

我正在编写一个python脚本来从终端发送电子邮件。在我目前发送的邮件中,它没有主题。我们如何在此电子邮件中添加主题

我当前的代码:

    import smtplib

    msg = """From: hello@hello.com
    To: hi@hi.com\n
    Here's my message!\nIt is lovely!
    """

    server = smtplib.SMTP_SSL('smtp.example.com', port=465)
    server.set_debuglevel(1)
    server.ehlo
    server.login('examplelogin', 'examplepassword')
    server.sendmail('me@me.com', ['anyone@anyone.com '], msg)
    server.quit()

您需要将
主题
放在消息的标题中

范例-

import smtplib

msg = """From: hello@hello.com
To: hi@hi.com\n
Subject: <Subject goes here>\n
Here's my message!\nIt is lovely!
"""

server = smtplib.SMTP_SSL('smtp.example.com', port=465)
server.set_debuglevel(1)
server.ehlo
server.login('examplelogin', 'examplepassword')
server.sendmail('me@me.com', ['anyone@anyone.com '], msg)
server.quit()
导入smtplib
msg=“”发件人:hello@hello.com
致:hi@hi.com\n
主题:\n
这是我的留言!\n太可爱了!
"""
服务器=smtplib.SMTP\u SSL('SMTP.example.com',端口=465)
服务器。设置调试级别(1)
server.ehlo
server.login('examplelogin','examplepassword')
server.sendmail('me@me.com', ['anyone@anyone.com“],msg)
server.quit()

您需要将
主题
放在邮件的标题中

范例-

import smtplib

msg = """From: hello@hello.com
To: hi@hi.com\n
Subject: <Subject goes here>\n
Here's my message!\nIt is lovely!
"""

server = smtplib.SMTP_SSL('smtp.example.com', port=465)
server.set_debuglevel(1)
server.ehlo
server.login('examplelogin', 'examplepassword')
server.sendmail('me@me.com', ['anyone@anyone.com '], msg)
server.quit()
导入smtplib
msg=“”发件人:hello@hello.com
致:hi@hi.com\n
主题:\n
这是我的留言!\n太可爱了!
"""
服务器=smtplib.SMTP\u SSL('SMTP.example.com',端口=465)
服务器。设置调试级别(1)
server.ehlo
server.login('examplelogin','examplepassword')
server.sendmail('me@me.com', ['anyone@anyone.com“],msg)
server.quit()

的确,你错过的主题。通过使用一些API(比如yagmail)而不是Header样式,可以很容易地避免这些事情

我相信(免责声明:我是维护者)会对您有很大帮助,因为它提供了一个简单的API

import yagmail
yag = yagmail.SMTP('hello@gmail.com', 'yourpassword')
yag.send(to = 'hi@hi.com', subject ='Your subject', contents = 'Some message text')
实际上只有三行

对于Python 3,使用
pip Install yagmail
pip3 Install yagmail
安装


更多信息请访问。

的确,您错过的主题。通过使用一些API(比如yagmail)而不是Header样式,可以很容易地避免这些事情

我相信(免责声明:我是维护者)会对您有很大帮助,因为它提供了一个简单的API

import yagmail
yag = yagmail.SMTP('hello@gmail.com', 'yourpassword')
yag.send(to = 'hi@hi.com', subject ='Your subject', contents = 'Some message text')
实际上只有三行

对于Python 3,使用
pip Install yagmail
pip3 Install yagmail
安装


更多信息请访问。

您只需使用MIMEMultipart()


希望这能奏效

您只需使用MIMEMultipart()


希望这能奏效

那是行不通的。对你的回答稍加修改就行了。\n的位置造成了差异。正确的代码是:发件人:hello@hello.com致:hi@hi.com主题:\n不过,感谢您提出相关建议。:)哦,好的,是的,更新了答案,虽然理想情况下,空格换行符应该在多行字符串中正确阅读。我们还建议您接受所有问题的答案,如果它们对您的帮助令人满意,将有助于社区。这不起作用。对你的回答稍加修改就行了。\n的位置造成了差异。正确的代码是:发件人:hello@hello.com致:hi@hi.com主题:\n不过,感谢您提出相关建议。:)哦,好的,是的,更新了答案,但理想情况下,空格换行符应该在多行字符串中正确阅读。我们还建议您接受所有问题的答案,如果它们对您有满意的帮助,将对社区有帮助。@user248884您将使用的域是什么?您可以使用
yagmail.SMTP('hello@domain.com“,”password“,host=“YOUR host”,port=“YOUR port”)
@user248884您将使用的域是什么?您可以使用
yagmail.SMTP('hello@domain.com“,”password“,host=“YOUR host”,port=“YOUR port”)
AttributeError:“list”对象没有
text=msg.as_string()的属性“encode”。as_string()
AttributeError:“list”对象没有
text=msg.as_string()的属性“encode”