Below is the python code to send email using Amazon Simple Email Service.
import smtplib
import email.utils
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import json
def lambda_handler(event, context):
print(event)
SENDER = 'sender_email_id'
SENDERNAME = 'sender_name'
RECIPIENT = 'recipient_email_id'
USERNAME_SMTP = "aws_smtp_username"
PASSWORD_SMTP = "aws_smtp_pd"
HOST = "Host server name"
PORT = 587
SUBJECT = 'Test Email'
BODY_TEXT = ("This is the test mail\r\n"
"This mail was sent for testing purpose "
"Kindly ignore this mail."
)
msg = MIMEMultipart('alternative')
msg['Subject'] = SUBJECT
msg['From'] = email.utils.formataddr((SENDERNAME, SENDER))
msg['To'] = RECIPIENT
part1 = MIMEText(BODY_TEXT, 'plain')
msg.attach(part1)
try:
server = smtplib.SMTP(HOST, PORT)
server.ehlo()
server.starttls()
server.ehlo()
server.login(USERNAME_SMTP, PASSWORD_SMTP)
server.sendmail(SENDER, RECIPIENT, msg.as_string())
server.close()
except Exception as e:
print ("Error: ", e)
else:
print ("Email sent!")
return {
'statusCode': 200,
'body': json.dumps('Success')
}
Thanks And Regards
Praveen Kumar Rajendran
praveenkumar558@gmail.com
0 comments:
Post a Comment