0

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?

user2966197
  • 2,793
  • 10
  • 45
  • 77
  • 1
    Is that the only code you have? Where is your handler? – Noel Llevares Oct 18 '17 at 03:15
  • 3
    [Port 25 is documented as being blocked from Lambda Functions](https://aws.amazon.com/lambda/faqs/#functions) and is probably the default port being used here, which is actually wrong but very commonly used anyway. Your mail server should accept mail on MSA port 587 (or another port). – Michael - sqlbot Oct 18 '17 at 05:37

0 Answers0