python SMTP 标头格式
以下代码正确地格式化了我的电子邮件脚本。它将收件人、发件人、主题和正文放置在电子邮件的正确部分。我的问题是,我无法弄清楚为什么在我的连接函数中需要两个回车符和两个换行符(“\r\n\r\n”)来显示电子邮件中的正文,而其余标题只需要一个CR。
def message(self):
subject = input("What is the subject line of your message")
headers = ["from: " + self.sendfrom,
"to: " + self.sendto,
"subject: " + subject,
"content_type: text/html"]
headers = "\r\n".join(headers)
msg = input("type your message")
return headers, msg
def connect(self, headers, msg):
self.server.starttls()
self.server.login(self.usrname,self.pswd)
self.server.sendmail(self.sendfrom, self.sendto, headers + "\r\n\r\n" + msg)
print("I sent the email")
return self.server
谢谢!
The following piece of code is formatting my email script correctly. It is placing the to, from, subject, and body in the correct parts of the email. My problem is that I can't figure out why it needs two returns and two newlines ("\r\n\r\n") in my connect function to display the body in the email when the rest of the headers only need one CR.
def message(self):
subject = input("What is the subject line of your message")
headers = ["from: " + self.sendfrom,
"to: " + self.sendto,
"subject: " + subject,
"content_type: text/html"]
headers = "\r\n".join(headers)
msg = input("type your message")
return headers, msg
def connect(self, headers, msg):
self.server.starttls()
self.server.login(self.usrname,self.pswd)
self.server.sendmail(self.sendfrom, self.sendto, headers + "\r\n\r\n" + msg)
print("I sent the email")
return self.server
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于标准,正文必须与标题分隔开线。
电子邮件软件符合这些标准,并且期望正文为空行。如果没有空行,它只是假设文本是标题的一部分,通常这些电子邮件软件默认隐藏大部分标题。
Because of the standard, body must be separated from the headers with an empty line.
e-mail software conforms to these standards and expects an empty line for the body. If there is no empty line, it just assumes the text is part of headers, and usually these email softwares hide most headers by default.