Gmail/Yahoo 测试 smtplib python 脚本的良好替代品

问题描述 投票:0回答:1

有哪些好的 smtp 电子邮件服务器可以用来测试 Python 脚本实践?

在另一个SO线程中,用户表示谷歌不再提供此功能,因此无论您尝试什么,您的代码都将无法工作(而且,我想,雅虎的情况也是如此)!所以我想知道我们还可以使用网络上的其他哪些服务。

以下是我脚本的相关行:

import os.path
import mimetypes
import smtplib
import getpass
import ssl # might need to remove!
from email.message import EmailMessage

message = EmailMessage()

sender = "[email protected]"
recipient = "[email protected]"

message['From'] = sender
message['To'] = recipient
message['Subject'] = 'Greetings from {} to {}'.format(sender, recipient)
body = """Hey there!
...
... I'm learning to send emails with Python!"""
message.set_content(body)

attachment_path = "./rebase.png"
attachment_filename = os.path.basename(attachment_path)
mime_type, _ = mimetypes.guess_type(attachment_path)
mime_type, mime_subtype = mime_type.split('/', 1)

with open(attachment_path, 'rb') as ap:
    message.add_attachment(ap.read(),
                            maintype=mime_type,
                            subtype=mime_subtype,
                            filename=os.path.basename(attachment_path))

mail_server = smtplib.SMTP_SSL('smtp.gmail.com', 465)

mail_server.set_debuglevel(1)
mail_pass = getpass.getpass('PWD_GOES_HERE!')
print("logging in...")
mail_server.login(sender, mail_pass)
mail_server.send_message(message)
mail_server.quit()
python smtp smtplib
1个回答
0
投票

有一些专门为此目的而设计的电子邮件测试工具。例如,MailHog 就是其中之一。它是一个开源电子邮件测试工具,旨在让开发人员在项目的开发阶段测试他们的电子邮件。它是如何工作的?它使用下面的假 SMTP 服务器来捕获您的电子邮件并在舒适的网络界面中向您显示它们。

还有其他此类工具,例如 MailTrap、MailCatcher、MailSlurp 和 PostHoc。您可以通过快速谷歌搜索找到许多其他内容。

我并不是在寻找其他主要电子邮件提供商,只是寻找一些工具来测试我的脚本是否具有电子邮件发送功能。

© www.soinside.com 2019 - 2024. All rights reserved.