Karthik Ashokkumar
Karthik Ashokkumar

Reputation: 59

How to send images and file attachments through AWS SES using Node.js?

This documentation doesn't provide a solution for node.js

Upvotes: 0

Views: 1473

Answers (1)

prakashkumarsoni
prakashkumarsoni

Reputation: 37

npm install aws-sdk 

then

    const AWS = require('aws-sdk')
    
    AWS.config.update({
        accessKeyId: config.aws_ses.accessKeyId,//access key id of aws ses
        secretAccessKey: config.aws_ses.secretAccessKey,//secret access key of aws ses
        region: config.aws_ses.region,//region of your instance eg 'us-west-2',
        ses: '2020-01-13',// Date
        
    });
    
    const ses = new AWS.SES();
    fs.readFile("./attachment.txt", function (err, data) {
      if(err){
         console.log("error : ",err);
      }else{
          const params = {
             Destination: {
                  ToAddresses: [to]
              },
              Message: {
                  Body: {
                      Html: {
                          Charset: 'UTF-8',
                          Data: mailOptions.html
                      }
                  },
                  Subject: {
                      Charset: 'UTF-8',
                      Data: mailOptions.subject
                  },
                  attachments: [{'filename': 'attachment.txt', 'content': data}]
              },
              ReturnPath: config.aws_ses.fromName, // eg [email protected]
              Source: config.aws_ses.fromName  // eg [email protected]
          }
      }
    
    })
    
     ses.sendEmail(params, (err, data) => {
          if (err) {
              console.log(err,null);
          } else {
              
              var returnMsg = 'Mail sent successfully';
              console.log(null, { message: returnMsg });
          }
      }) 

Upvotes: 2

Related Questions