简体   繁体   English

带有 AWS SES 的 smtplib 无法发送带附件的电子邮件

[英]smtplib with AWS SES could not send emails with attachments

I use AWS SES to send emails from my instance - it works when I send just messages, but when files are attached to emails, it could not send and just gets pending.我使用 AWS SES 从我的实例发送电子邮件 - 当我只发送消息时它可以工作,但是当文件附加到电子邮件时,它无法发送并且只是等待处理。

It works when I do it from a local system, but from AWS.当我从本地系统而不是 AWS 执行时,它会起作用。 I'm wondering what happens when a file is attached to an email - is there somethings in AWS blocking it - or I need to use something different from smptlib of email?我想知道当文件附加到 email 时会发生什么 - AWS 中是否有某些东西阻止它 - 或者我需要使用不同于 email 的 smptlib 的东西?

I use the script below:我使用下面的脚本:

subject = 'xx'
body = "xx"
sender_email = "xx@gmail.com" `#this email address is confirmed in aws`
user = "xxxxx"  #ses username  

receiver_email = 'xxx@gmail.com' `#this email address is confirmed in aws`
password = 'xxxxx' # ses password   

message = MIMEMultipart()
message["From"] = formataddr(('name', 'xxx@gmail.com'))
message["To"] = receiver_email
message["Subject"] = subject    
# Add attachment to message and convert message to string
message.attach(MIMEText(body, "plain"))    
filename =  'sdf.txt' #the file that will be sent.    
   
with open(filename, "rb") as attachment:
    ## Add file as application/octet-stream
    # Email client can usually download this automatically as attachment

    part = MIMEBase("application", "octet-stream")
    part.set_payload(attachment.read())

# Encode file in ASCII characters to send by email   
encoders.encode_base64(part)   
   
part.add_header(
    "Content-Disposition",
    f"attachment; filename= {filename}",
)    

message.attach(part)
text = message.as_string()    
   
# Log in to server using secure context and send email
context = ssl.create_default_context()
try:
    with smtplib.SMTP_SSL("email-smtp.us-east-1.amazonaws.com", 465, context=context  ) as server:
        server.login(user, password)
        server.sendmail(sender_email, receiver_email, text)
        server.close()
except smtplib.SMTPException as e:
        print("Error: ", e)    

I modified the script, below - now it works nicely either with/without attachments in aws.我修改了下面的脚本 - 现在它在 aws 中有/没有附件都可以很好地工作。

msg = MIMEMultipart()
body_part = MIMEText('xxxx', 'plain')
msg['Subject'] = 'xxx'
msg['From'] = formataddr(('xxxx', 'xxx@gmail.com'))
msg['To'] = 'xxx@gmail.com'
# Add body to email
msg.attach(body_part)
# open and read the file in binary
with open(path ,'rb') as file:
# Attach the file with filename to the email
    msg.attach(MIMEApplication(file.read(), Name='xxx'))
# Create SMTP object
smtp_obj = smtplib.SMTP_SSL("email-smtp.us-east-1.amazonaws.com", 465)
# Login to the server
user = "xxx"
# Replace smtp_password with your Amazon SES SMTP password.
password = 'xxx'
smtp_obj.login(user, password)
# Convert the message to a string and send it
smtp_obj.sendmail(msg['From'], msg['To'], msg.as_string())
smtp_obj.quit()

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

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