关于python smtpd库,我尝试覆盖process_message方法,但是当我尝试使用客户端连接到该方法并将消息发送到一个gmail帐户时,它只是将消息打印在控制台上,但是我想要它实际上是在本地计算机中发出类似于postfix的消息。我应该如何实现?
我用google smtpd进行搜索,但是找不到太多有用的消息
import smtpd
import asyncore
class CustomSMTPServer(smtpd.SMTPServer):
def process_message(self, peer, mailfrom, rcpttos, data, **kwargs):
print('Receiving message from:', peer)
print('Message addressed from:', mailfrom)
print('Message addressed to :', rcpttos)
print('Message length :', len(data))
return
server = CustomSMTPServer(('127.0.0.1', 1025), None)
asyncore.loop()
答案 0 :(得分:0)
引用Robert Putt's answer,您将在交付能力方面陷入困境。最好的解决方案是在本地托管SMTP服务器(当然,最高的解决方案是使用AmazonSES或类似MailGun的API)。 DigialOcean在这里有一个很好的教程。然后,您可以使用下面的Python代码发送电子邮件。
import smtplib
sender = 'no_reply@mydomain.com'
receivers = ['person@otherdomain.com']
message = """From: No Reply <no_reply@mydomain.com>
To: Person <person@otherdomain.com>
Subject: Test Email
This is a test e-mail message.
"""
try:
smtpObj = smtplib.SMTP('localhost')
smtpObj.sendmail(sender, receivers, message)
print("Successfully sent email")
except SMTPException:
print("Error: unable to send email")
希望这会有所帮助!