电子邮件发送代码可以正常工作,但它会在消息字段中打印所有内容。在过去的3个小时里,我基本上都尝试了一切。有什么建议吗?
import smtplib
fromaddr="xxxxx@xxx.com"
toaddr="xxxxx@xxxx.com"
message = '''\\
... From: xxxxx
... Subject: testin'...
...
... This is a test '''
password="xxxx"
subject="this is supposed to be the subject"
server=smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login(fromaddr,password)
server.sendmail(fromaddr,toaddr,message,subject)
server.quit()
subject = "this is supposed to be the subject"
答案 0 :(得分:1)
使用MIMEText创建消息一直对我有用。 以下是如何使用它:
import smtplib
from email.mime.text import MIMEText
# message body goes here
message = '''\\
... This is a test '''
msg = MIMEText(message, 'plain')
msg['Subject'] = "this is supposed to be the subject"
msg['From'] = "xxxxx@xxx.com"
msg['To'] = "xxxxx@xxxx.com"
server=smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login(fromaddr,password)
server.sendmail(msg['From'], msg['To'], msg.as_string())
server.quit()
我希望这有帮助!