NEELAM SAI KUMAR
NEELAM SAI KUMAR

Reputation: 26

How to send Email using smtplib in django app deployed on Aws lambda?

I want to send email using python package smtplib from Django application which is running on AWS.

I had deployed the Django application using zappa. I had written the smtplib sendmail() function to send mail. When I tried executing the project in my local. It is sending mail. But when I had deployed onto AWS. It is throwing "End Point Request Timeout". Please help me in solving my issue.

Upvotes: 0

Views: 843

Answers (1)

Reez0
Reez0

Reputation: 2689

This worked for me when I had a similar issue. Just modify it to fit your needs:

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()

Upvotes: 1

Related Questions