Using AWS SDK

I am trying to use aws sdk and call SES API ( https://docs.aws.amazon.com/ses/latest/DeveloperGuide/Welcome.html ) for sending email.

I imported the aws-sdk module and this is my code on the backend module :

import {aws} from ‘aws-sdk’;

export function sendEmail() {
console.log(aws)
}

This my code on the client side:
export function button1_click(event) {
sendEmail().then(product => {
console.log(product);
})
. catch (error => {
console.log(error);
});
}

When I run this I get “undefined”.

What am I missing when trying to import aws ??

You are simply console logging ‘aws’ in the backend.

I’m pretty sure you need Access (API) Keys to authorize your call and you need to use their .sendEmail() function to send all the parameters (body, sender, recipient)

Read here: https://docs.aws.amazon.com/ses/latest/DeveloperGuide/send-an-email-using-sdk.html

Take a look at an example here: https://jaketrent.com/post/send-email-aws-sdk-node-example/

Thank you for the quick response. My point from the above code is that I am not able to load aws module at all in my backend code. So without that I can’t call send email anyways, so i am trying to solve that first .

I used the code from your example like this:
import {AWS} from ‘aws-sdk’;
export function sendEmail() {
// Set the region
const ses = new AWS.SES();
const params = {
Destination: {
ToAddresses: [‘to-email@example.com’]
},
Message: {
Body: {
Html: {
Charset: ‘UTF-8’,
Data:
‘This message body contains HTML formatting, like Amazon SES Developer Guide.’
},
Text: {
Charset: ‘UTF-8’,
Data: ‘This is the message body in text format.’
}
},
Subject: {
Charset: ‘UTF-8’,
Data: ‘Test email from code’
}
},
ReturnPath: ‘from-email@example.com’,
Source: ‘from-email@example.com’
};
ses.sendEmail(params, (err, data) => {
if (err)
console.log(err, err.stack);
else
console.log(data);
});
}

Th error I got when I executed the above code is :

TypeError: Cannot read property ‘SES’ of undefined

So its failing in the first step itself and I am not sure why its unable to load AWS from aws-sdk .

I still need to replace the email params to the right values, I posted it here without my actual params so that I can debug the aws module issue.

Any suggestions please ?

You need to study the Documentation that Amazon provides. A sample backend code will look like the below.

//amazon.jsw

const AWS = require("aws-sdk");

AWS.config.update({
   accessKeyId: 'XXXXXXXXX',//your access key 
   secretAccessKey: 'XXXXXX', //secret key
   region: 'XXXXXXX' //region
});

export function emailing() {
   const ses = new AWS.SES({ apiVersion: "2019-12-01" }); //api version
   const params = {
   Destination: {
    ToAddresses: ["john@gmail.com"]
   },
   ConfigurationSetName: "No name given",
   Message: {
    Body: {
    Html: {
     Charset: "UTF-8",
     Data: "<html><body><h1>Hello  Charith</h1><p style='color:red'>Sample description</p> <p>Time 1517831318946</p></body></html>"
   },
   Text: {
    Charset: "UTF-8",
    Data: "Hello Charith Sample description time 1517831318946"
   }
  },
   Subject: {
    Charset: "UTF-8",
    Data: "Test email"
   }
  },
   Source: "corvid@gmail.com"
  };
  const sendEmail = ses.sendEmail(params).promise();
  sendEmail
  .then(data => {
    console.log("email submitted to SES", data);
  })
  .catch(error => {
    console.log(error);
  });
}

Call the backend function emailing() from your page to test and you will see that it has loaded correctly and will be throwing the correct errors for the above code.