简体   繁体   English

Python smtplib“主题”为什么不打印任何主题。 我现在已经尝试了几乎任何东西

[英]Python smtplib ''Subject'' why doesn't it print any subject. I've tried almost anything now

The email sending code works fine without problems, but it prints all of it in the message field. 电子邮件发送代码可以正常工作,不会出现问题,但是会在消息字段中打印所有代码。 I've basically tried everything the last 3 hours. 我基本上已经尝试了最近3个小时的所有操作。 Any advice guys? 有什么建议吗?

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"

Creating the message using MIMEText has always worked for me. 使用MIMEText创建消息一直对我有用。 Here is how you can use it: 使用方法如下:

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()

I hope this helps! 我希望这有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM