简体   繁体   English

使用Python在AWS EC2上发送电子邮件

[英]Sending email on AWS EC2 using Python

I want to generate email contents on the fly and send email from a Gmail account using Python from an EC2 server. 我想即时生成电子邮件内容,并使用来自EC2服务器的Python使用Gmail帐户发送电子邮件。 It works fine when I do it on localhost, but when I run the same code on my EC2, it doesn't work... 当我在localhost上运行时,它工作正常,但是当我在EC2上运行相同的代码时,它不起作用...

Here is my code: 这是我的代码:

from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
msg = MIMEMultipart()

part = MIMEApplication(file("<pathtopdf>").read())
part.add_header('Content-Disposition', 'attachment; filename="<nameofpdf.pdf>"')
msg.attach(part)

msg['From'] = "<username>@gmail.com"
msg['To'] = "<username>@gmail.com"
msg['Subject'] = "Hello PDF"
import smtplib 
server = smtplib.SMTP('smtp.gmail.com',587) #port 465 or 587
server.ehlo()
server.starttls()
server.ehlo()
server.login(<login info params>)
server.close()

Is there anyway to do this without using Amazon SES? 无论如何,不​​使用Amazon SES就能做到这一点吗? I only have to send an email upon customer request, and I just have to generate a quick PDF based on customer input and send an email to the customer at the email they specify to send to... it's a very simple operation and the hoops I have to jump through are getting a tad frustrating. 我只需要根据客户的请求发送电子邮件,我只需要根据客户的输入生成快速的PDF并在他们指定的电子邮件中向客户发送电子邮件...这是一个非常简单的操作,非常麻烦我必须跳过一些令人沮丧的事情。

EDIT 编辑

I recommend you to use AWS SES. 我建议您使用AWS SES。 Because Gmail blocks EC2 IP addresses. 因为Gmail会阻止EC2 IP地址。

Gmail SMTP is not working in ec2 instance Gmail SMTP在ec2实例中不起作用

您必须在出站规则中启用端口587。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM