Send Email with Firebase Functions and Amazon SES

Viewed 950

I just want to send an email to test the connection via Firebase Functions and AWS Simple Email Service (SES) from a verified domain and verified email addresses (still in sandbox). Therefore I installed node-ses and created the following code. I use vuejs for the webapp and nodejs.

// The Cloud Functions for Firebase SDK to create Cloud Functions and setup triggers.
const functions = require('firebase-functions');

// The Firebase Admin SDK to access Firestore.
const admin = require('firebase-admin');
admin.initializeApp();

// AWS Credentials
var ses = require('node-ses'), 
    client = ses.createClient({
        key: '...', 
        secret: '...',
        amazon: 'https://email-smtp.eu-west-3.amazonaws.com'
});

exports.scheduledFunction = functions.pubsub.schedule('every 10 minutes').onRun((context) => {

// Give SES the details and let it construct the message for you.
client.sendEmail({
    to: 'support@myVerfiedDomain.com'
  , from: 'do_not_reply@myVerfiedDomain.com'
  //, cc: 'theWickedWitch@nerds.net'
  //, bcc: ['canAlsoBe@nArray.com', 'forrealz@.org']
  , subject: 'Test'
  , message: 'Test Message'
  , altText: 'Whatever'
 }, function (err, data, res) {
  console.log(err)
  console.log(data)
  console.log(res)
 })
})

The problem is, I can't even deploy this function: every time I get the same error message:

...
+  functions: created scheduler job firebase-schedule-scheduledFunction-us-central1
+  functions[scheduledFunction(us-central1)]: Successful upsert schedule operation. 

Functions deploy had errors with the following functions:
        scheduledFunction(us-central1)

To try redeploying those functions, run:
    firebase deploy --only "functions:scheduledFunction"

To continue deploying other features (such as database), run:
    firebase deploy --except functions

Error: Functions did not deploy properly.

But if I deploy a simple Firebase Function it works. So it has nothing to do with my setup.

Does anybody know what I do wrong?

Thanks!! Chris

1 Answers

I found a solution. I still do not know how it works with node-ses but I know how it works with nodemailer.

  1. Install nodemailer (npm i nodemailer)
  2. Install nodemailer-ses-transport
  3. Change the region to one that suits your settings
  4. Input the following in your index.js of Firebase Functions (put your AWS credentials)

--- SOURCE CODE BELOW ---

// The Cloud Functions for Firebase SDK to create Cloud Functions and setup triggers. 
const functions = require('firebase-functions');

// The Firebase Admin SDK to access Firestore. 
const admin = require('firebase-admin');
admin.initializeApp();

// Nodemailer 
var nodemailer = require('nodemailer');
var ses = require('nodemailer-ses-transport');
    
// Create transporter
var transporter = nodemailer.createTransport(ses({
    accessKeyId: '...',
    secretAccessKey: '...',
    region: 'eu-west-3'
}));

exports.sendEmail = functions.pubsub.schedule('every 1 minutes').onRun((context) => {
    transporter.sendMail({
        from: 'sender@yourVerifiedDomain.com',
        to: 'receiver@yourVerifiedDomain.com',
        subject: 'Email Testing',
        html: '<h1>Title</h1>',
            /*
            attachments: [
                {
                filename: 'report',
                path: 'C:\\xampp\\htdocs\\js\\report.xlsx',
                contentType: 'application/vnd.ms-excel'
                }
            ]
            */
    },
    function(err, data) {
        if (err) throw err;
        console.log('Email sent:');
        console.log(data);
    }); 
});
Related