DJango 邮件在本地工作但不在生产中

Posted

技术标签:

【中文标题】DJango 邮件在本地工作但不在生产中【英文标题】:DJango mail working in local but not in production 【发布时间】:2021-12-05 06:36:34 【问题描述】:

以下是我向我的用户发送邮件的代码:

mail = EmailMultiAlternatives(subject=subject, body=text_content, from_email="email@host.com", to=["email@host.com"], bcc=["email@host.com"])
mail.attach_file(model.document.path)
mail.attach_alternative(html_content, "text/html")
mail_status = mail.send()

我的settings.py如下:

EMAIL_HOST = "mail.host.com"
EMAIL_PORT = 587

EMAIL_HOST_USER = "email@host.com"
EMAIL_HOST_PASSWORD = str(os.getenv('EMAIL_HOST_PASSWORD'))

问题是上面的代码在我的本地 Windows 机器上运行得非常好,但是在运行 Ubuntu (Linux) 的远程服务器上运行时代码挂起并且没有显示输出。

知道我哪里出错了吗?

【问题讨论】:

【参考方案1】:

这很可能是端口问题。确认这一点的最佳方法是使用telnet 客户端。

telnet mail.host.com 587

如果您在发出此命令后没有看到Connected 文本,那么问题很可能是因为port。在这种情况下,您可能需要explicitly open this port。

【讨论】:

以上是关于DJango 邮件在本地工作但不在生产中的主要内容,如果未能解决你的问题,请参考以下文章

Django 在本地迁移工作,但不在 Elastic Beanstalk 生产上

validate_email python库在本地机器上工作,但不在aws弹性bean上

Django Reset PW 电子邮件不在 AWS 上发送

Vercel中的Nodemailer不在生产中发送电子邮件

Django中的Send_mail,在shell中工作,在本地工作,不在视图中

Laravel 视图在生产中工作但不在本地