我正在尝试将电子邮件发送到私有(private) SMTP 服务器,并且可以使用 python smtplib
执行此操作,如下所示
s = smtplib.SMTP(server, port)
s.sendmail(me, you, msg.as_string())
尝试将其转换为 Sendgrid(因为 Azure 阻止 SMTP),但它没有指定专用 SMTP 服务器的选项。
import sendgrid
import os
from sendgrid.helpers.mail import *
sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY'))
from_email = Email("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="225647515662475a434f524e470c414d4f" rel="noreferrer noopener nofollow">[email protected]</a>")
to_email = Email("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="81f5e4f2f5c1e4f9e0ecf1ede4afe2eeec" rel="noreferrer noopener nofollow">[email protected]</a>")
subject = "Sending with SendGrid is Fun"
content = Content("text/plain", "and easy to do anywhere, even with Python")
mail = Mail(from_email, subject, to_email, content)
response = sg.client.mail.send.post(request_body=mail.get())
print(response.status_code)
print(response.body)
print(response.headers)
任何帮助将不胜感激。
谢谢
最佳答案
这很正常,sendgrid 的 python 包不使用 smtp 协议(protocol),但实际上请求一个使用经典 HTTPS 请求的 API...
因此,除非您的 smtp 服务器可以通过 API(定制的?)访问,否则您不可能使用 sendgrid 包。
如果您想自定义 sendgrid 的 python 包,以便指向您自己的 API 端点(该端点将充当您的 smtp 请求的中继),您可以自由修改其源代码:https://github.com/sendgrid/sendgrid-python
关于python - sendgrid - 如何指定收件人 SMTP 服务器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46330958/