使用Python smtpd模块发送/接收电子邮件
我正在尝试使用 Python smtpd 模块创建一个简单的 smtp 服务器。我可以收到一封电子邮件并将其打印出来。我尝试向向我发送包含 hello world 消息的电子邮件的人发回一封电子邮件,但最终陷入了无限循环。我尝试使用自己的服务器发送电子邮件,但它只是将其解释为已收到的另一封电子邮件。
我如何使用它来发送和接收电子邮件?
import smtplib, smtpd
import asyncore
import email.utils
from email.mime.text import MIMEText
import threading
class SMTPReceiver(smtpd.SMTPServer):
def process_message(self, peer, mailfrom, rcpttos, data):
print 'Receiving message from:', peer
print 'Message addressed from:', mailfrom
print 'Message addressed to :', rcpttos
print 'Message length :', len(data)
print data
def send_response():
msg = MIMEText('Hello world!')
msg['To'] = email.utils.formataddr(('Recipient', mailfrom))
msg['From'] = email.utils.formataddr(('Author', '[email protected]'))
msg['Subject'] = ''
print 'Connecting to mail server'
server = smtplib.SMTP()
server.set_debuglevel(1)
server.connect()
print 'Attempting to send message'
try:
server.sendmail('[email protected]', [mailfrom], msg.as_string())
except Exception, ex:
print 'Could not send mail', ex
finally:
server.quit()
print 'Finished sending message'
threading.Thread(target=send_response).start()
return
def main():
server = SMTPReceiver(('', 25), None)
asyncore.loop()
if __name__ == '__main__':
main()
注意:示例中未使用真实的电子邮件地址。 注2:不使用它作为邮件服务器。只是想发送/接收简单的电子邮件以获得简单的服务。
I'm trying to make a simple smtp server using the Python smtpd module. I can receive an e-mail and print it out. I try to send an e-mail back to the person who sent me the e-mail with a hello world message and I end up with an infinite loop. I try to use my own server to send the e-mail and it just interprets it as another e-mail that's been received.
How can I use this to both send and receive e-mail?
import smtplib, smtpd
import asyncore
import email.utils
from email.mime.text import MIMEText
import threading
class SMTPReceiver(smtpd.SMTPServer):
def process_message(self, peer, mailfrom, rcpttos, data):
print 'Receiving message from:', peer
print 'Message addressed from:', mailfrom
print 'Message addressed to :', rcpttos
print 'Message length :', len(data)
print data
def send_response():
msg = MIMEText('Hello world!')
msg['To'] = email.utils.formataddr(('Recipient', mailfrom))
msg['From'] = email.utils.formataddr(('Author', '[email protected]'))
msg['Subject'] = ''
print 'Connecting to mail server'
server = smtplib.SMTP()
server.set_debuglevel(1)
server.connect()
print 'Attempting to send message'
try:
server.sendmail('[email protected]', [mailfrom], msg.as_string())
except Exception, ex:
print 'Could not send mail', ex
finally:
server.quit()
print 'Finished sending message'
threading.Thread(target=send_response).start()
return
def main():
server = SMTPReceiver(('', 25), None)
asyncore.loop()
if __name__ == '__main__':
main()
Note: Not using a real e-mail address in the example.
Note 2: Not using this as a mail server. Just want to send/receive simple e-mails for a simple service.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不应该将消息发送给您自己...
执行MX 查找 并将邮件发送到适当的 SMTP 服务器。
You're not supposed to send the message to yourself...
Perform a MX lookup and send the message to the appropriate SMTP server.