使用Python smtpd模块发送/接收电子邮件

发布于 2024-11-29 04:31:49 字数 1951 浏览 1 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

韵柒 2024-12-06 04:31:49

应该将消息发送给您自己...

执行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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文