我和我的朋友一直在给一个邮件发送者编码,但是如果你能帮忙的话,我们不能在邮件中发送主题;非常感谢:
import smtplib
def send_email(send_to, subject, message):
server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
server.login("*******", "******")
server.sendmail('******', send_to, message, subject)
server.quit()
target = input('Who are you sending the email to? ')
subj = input('What is your subject? ')
body = input('Enter the message you want to send: ')
send_email(target, subj, body)
except SMTPException:
print("Error: unable to send email")
对
smtplib.SMTP.sendmail()
的调用不采用subject
参数。有关如何调用它的说明,请参阅文档。你知道吗主题行以及所有其他标题都作为消息的一部分,以一种称为RFC822格式的格式包含在最初定义格式的现已过时的文档之后。使您的信息符合该格式,例如:
import smtplib fromx = 'xxx@gmail.com' to = 'xxx@gmail.com' subject = 'subject' #Line that causes trouble msg = 'Subject:{}\n\nexample'.format(subject) server = smtplib.SMTP('smtp.gmail.com:587') server.starttls() server.ehlo() server.login('xxx@gmail.com', 'xxx') server.sendmail(fromx, to, msg) server.quit()
当然,使消息符合所有适当标准的更简单方法是使用Python
email.message
标准库,如下所示:import smtplib from email.mime.text import MIMEText fromx = 'xxx@gmail.com' to = 'xxx@gmail.com' msg = MIMEText('example') msg['Subject'] = 'subject' msg['From'] = fromx msg['To'] = to server = smtplib.SMTP('smtp.gmail.com:587') server.starttls() server.ehlo() server.login('xxx@gmail.com', 'xxx') server.sendmail(fromx, to, msg.as_string()) server.quit()
其他例子也有。你知道吗
相关问题 更多 >
编程相关推荐