【问题标题】:How to send Email using smtplib in django app deployed on Aws lambda?如何在部署在 Aws lambda 上的 django 应用程序中使用 smtplib 发送电子邮件?
【发布时间】:2018-12-25 12:58:27
【问题描述】:

我想使用运行在 AWS 上的 Django 应用程序中的 python 包 smtplib 发送电子邮件。

我已经使用 zappa 部署了 Django 应用程序。我编写了 smtplib sendmail() 函数来发送邮件。当我尝试在本地执行该项目时。它正在发送邮件。但是当我部署到 AWS 上时。它正在抛出“端点请求超时”。请帮我解决我的问题。

【问题讨论】:

  • 发布完整的错误信息。
  • @xyres {"Error":"Endpoint request timeout"} 是浏览器上显示的错误
  • 可能是您的 lambda 函数无法访问 Internet。看到这个答案:stackoverflow.com/a/47363665

标签: python django amazon-web-services smtplib


【解决方案1】:

当我遇到类似问题时,这对我有用。只需修改它以满足您的需求:

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders

# # comment implies that the field needs to be filled in

fromaddr = ""    # from email address
toaddr = ""      # destination email address
smtp_user = ""    # SMTP username used for authentication
smtp_pass = ""    # SMTP password used for authentication
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = ""    # subject

body = ""    # body
msg.attach(MIMEText(body, 'plain'))

filename = ""    # filename including extension
attachment = open(r"", "rb")    # e.g. (r"C:\pic\pic.jpg", "rb")

part = MIMEBase('application', 'octet-stream')
part.set_payload((attachment).read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', "attachment; filename= %s" % filename)

msg.attach(part)

server = smtplib.SMTP('', 587)    # e.g. ('in-v3.mailjet.com', 587)
server.starttls()
server.login(smtp_user, smtp_pass)
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
server.quit()

【讨论】:

  • 感谢回复,还是有同样的错误。对于 smtp 连接,AWS 中是否需要进行任何更改。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-09-22
  • 2011-09-17
  • 2017-09-20
  • 2018-12-09
  • 2015-09-07
  • 1970-01-01
  • 2011-02-03
相关资源
最近更新 更多