python smtpd.SMTPServer:如何在process_message中发送回复消息?

时间:2018-09-25 21:17:49

标签: python

smtpd.SMTPServer的实现中,我可以通过process_message()回调接收消息。如何将回复消息发送回发件人?

1 个答案:

答案 0 :(得分:2)

这是一个脚本,可以发送对我有用的电子邮件。我不知道您是否想要,但希望它能为您提供帮助。

import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText


fromaddr = "YOUR ADDRESS"
toaddr = "ADDRESS YOU WANT TO SEND TO"
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "SUBJECT OF THE MAIL"

body = "YOUR MESSAGE HERE"
msg.attach(MIMEText(body, 'plain'))

server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(fromaddr, "YOUR PASSWORD")
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
server.quit()