I am trying to send an email from an Amazon Lambda function
using python's
SMTP
library. Here is my code so far:
import smtplib
from_addr = 'fromemailid@company.com'
username = 'user1'
password = 'pwd'
def send_email():
to_addrs = "user1@company.com"
msg = "\r\n".join([
"From: fromemailid@company.com",
"To: "+to_addrs,
"Subject: Test Email ",
"",
"Hello" + ", \n This is a test email"
])
server = smtplib.SMTP('123.45.678.9')
server.ehlo()
server.starttls()
server.login(username, password)
server.sendmail(from_addr, to_addrs, msg)
server.quit()
if __name__ == '__main__':
send_email()
In my code above the hostname
is of the format of an ip_address
. When I execute this code I get error as TimeoutError: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
.
I tried server = smtplib.SMTP('123.45.678.9', local_hostname = 'mail.company.com')
as well but same error. If I try server = smtplib.SMTP('mail.company.com')
then I get following error - [Errno -2] Name or service not known
. How can send an email from within the lambda function?