python SMTP 标头格式

发布于 2024-12-07 16:46:16 字数 744 浏览 0 评论 0原文

以下代码正确地格式化了我的电子邮件脚本。它将收件人、发件人、主题和正文放置在电子邮件的正确部分。我的问题是,我无法弄清楚为什么在我的连接函数中需要两个回车符和两个换行符(“\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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

战皆罪 2024-12-14 16:46:16

由于标准,正文必须与标题分隔开线。

消息由标头字段和可选的正文组成。身体
只是包含 ASCII 字符的行序列。这是
通过空行与标题分隔(即没有任何内容的行)
在 CRLF 之前)。

电子邮件软件符合这些标准,并且期望正文为空行。如果没有空行,它只是假设文本是标题的一部分,通常这些电子邮件软件默认隐藏大部分标题。

Because of the standard, body must be separated from the headers with an empty line.

A message consists of header fields and, optionally, a body. The body
is simply a sequence of lines containing ASCII characters. It is
separated from the headers by a null line (i.e., a line with nothing
preceding the CRLF).

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文