question

newuser-e47e126b-f8c0-43f6-bb16-dbc956334d55 avatar image

Error while using SMTP in aws lambda function

Hi,

I am trying to create a skill that sends email to the user using the smtp library in python. The program works when i run it locally but its throwing error when i use it in a lambda function. below is the code:

    msg = email.mime.Multipart.MIMEMultipart()
    msg['Subject'] = 'test'
    msg['From'] = 'abc@xyz.com'
    msg['To'] = 'abc@xyz.com'
    smtpserver = smtplib.SMTP("smtp.office365.com",587)
    smtpserver.starttls()
    smtpserver.login("abc@xyz.com", "123")
    smtpserver.sendmail("sender",'receiver', msg.as_string())
    smtpserver.close()
alexa skills kitlambda
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Brian@Amazon avatar image
Brian@Amazon answered

What error is it throwing?

10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

newuser-e47e126b-f8c0-43f6-bb16-dbc956334d55 avatar image
newuser-e47e126b-f8c0-43f6-bb16-dbc956334d55 answered

It was a time out error. I changed the interval settings in lambda configuration.Now it works. Thank you

10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Divya Khandelwal avatar image
Divya Khandelwal answered

server = smtplib.SMTP('smtp.office365.com', 587) server.ehlo() server.starttls() server.ehlo() server.login("abcxyz@example.com", "hahah") text = msg.as_string() server.sendmail(fromaddr, rcpt, text)

use this code while sending email by using Python

Thank You

Email Marketing Expert


10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

sonu singhal avatar image
sonu singhal answered
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()

visit

10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Dhamaal Games avatar image
Dhamaal Games answered
const AWS = require('aws-sdk')
const sesConfig = {
apiVersion: '2010-12-01',
region: 'selected_region',
}
const ses = new AWS.SES(sesConfig)
exports.handler =  async (event, context, callback) => {
 const incomingMsg = JSON.parse(event.Records[0].body)
  const params = {
    Destination: {
     ToAddresses: [incomingMsg.toEmail],
    },
    Message: {
     Body: {
      Html: {
       Charset: 'UTF-8',
       Data: incomingMsg.htmlContent,
      },
      Text: {
       Charset: 'UTF-8',
       Data: incomingMsg.textContent,
      },
     },
     Subject: {
      Charset: 'UTF-8',
      Data:incomingMsg.subject,
     },
    },
    ConfigurationSetName: configurationName,
    Source: incomingMsg.source,
    ReplyToAddresses: [
     'email@domain.com',
    ],
   }
await ses.sendEmail(params).promise().then((data) => {
   console.log(data)
   const response = {
        "statusCode": 200,
        "body": JSON.stringify(data),
        "isBase64Encoded": false
    };
    callback(null, response);
 }).catch((err) => {
   console.error(err)
      const response = {
        "statusCode": 501,
        "body": JSON.stringify(err),
        "isBase64Encoded": false
    };
    callback(err,response);

}); }

Visit

10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.