Python SMTP库空白电子邮件随着时间的推移
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python SMTP库空白电子邮件随着时间的推移相关的知识,希望对你有一定的参考价值。
我正在使用内置的Python库来发送电子邮件。一切正常,除非我把时间放在我发送的信息中。像7:30 pm
。
当我在电子邮件中留出时间时,电子邮件收件人只会收到一封空白电子邮件。如果我删除时间,电子邮件发送就好了。我猜是时候搞砸了电子邮件。我应该在代码中更改什么,以便在电子邮件正文中留出时间。谢谢!
from smtplib import SMTP
def sendEmail(message,address):
debuglevel = 0
smtp = SMTP()
smtp.set_debuglevel(debuglevel)
#connect to the email server
smtp.connect('smtp.gmail.com', 587)
smtp.starttls()
smtp.login('ouremail@gmail.com', 'ourpassword')
#set the email to be sent from
from_addr = "Auto Notification <sender email>"
#send the email from our address and the message and recepients provided in the function definition.
smtp.sendmail(from_addr, address, message)
smtp.quit()
finalStr = "The Girls Varsity Basketball has been changed to 02/06/2018 at 7:30 pm * - ( previously 6:15 pm ) at Clear Lake High School"
sendEmail(finalStr, 'myemail@gmail.com')
答案
在传输到SMTP服务器期间,某些字符被误解为控制字符。尝试将字符串编码为MIMEText:
from email.mime.text import MIMEText
#send the email from our address and the message and recepients provided in the function definition.
encoded_message = MIMEText(message).as_string()
smtp.sendmail(from_addr, address, encoded_message)
以下是传输时结果的不同之处。请注意,编码是在工作示例中声明的。
之前
send: b'The Girls Varsity Basketball has been changed to 02/06/2018 at 7:30 pm * - ( previously 6:15 pm ) at Clear Lake High School
.
'
后:
send: b'Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
The Girls Varsity Basketball has been changed to 02/06/2018 at 7:30 pm * - ( previously 6:15 pm ) at Clear Lake High School
.
'
以上是关于Python SMTP库空白电子邮件随着时间的推移的主要内容,如果未能解决你的问题,请参考以下文章