2

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

Chris
  • 415
  • 5
  • 19

2 Answers2

2

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);
    }); 
});
Cezille07
  • 350
  • 1
  • 7
  • 17
Chris
  • 415
  • 5
  • 19
  • Hi, is this still working or you found a better solution? – Kai Tera Nov 24 '22 at 15:54
  • Hi, its still working for me. I use it every day. – Chris Nov 24 '22 at 21:53
  • I am getting an error : "Exception from a finished function: SignatureDoesNotMatch: The request signature we calculated does not match the signature you provided. Check your AWS Secret Access Key and signing method. Consult the service documentation for details." Have you faced with this issue ever? – Kai Tera Nov 25 '22 at 06:09
  • 1
    No never. But is it possible that you use another region? In my posted answer I use the ```eu-west-3``` region. You need to replace this region with your region in your AWS settings, otherwise it will not work. Hope this helps! – Chris Nov 25 '22 at 20:12
  • Hi again, do you use aws ses unsubscribe feature? Are you able to set ListManagementOptions? – Kai Tera Dec 07 '22 at 14:54
  • Hi Kai, I don't use the unsubscribe feature so I can't answer your question. – Chris Dec 07 '22 at 21:44
  • Thanks. I solved my issue. If you do not want to use nodemailer you can check this link out [link](https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/ses-examples-sending-email.html) – Kai Tera Dec 07 '22 at 22:15
1

The above solution was not working for me so here are the steps I followed.

First install nodemailer

yarn add nodemailer

Then install @aws-sdk

yarn add @aws-sdk

Just so you know I am creating a test file here not a function so everything below will be a simple nextjs 13 component which sends a test email.

Remember nodemailer is a node plugin it does not work on front end so we have to cretae an API or a function and then only send email via calling that function or triggering the api.

Then just use it like below:

exports.sendEmail = functions.https.onRequest(async (req, res) => {
  const params = {
    Destination: {
      ToAddresses: ['recipient@example.com'], // Add recipient email addresses
    },
    Message: {
      Body: {
        Text: {
          Data: 'Hello, this is the email content!', // Add your email content here
        },
      },
      Subject: {
        Data: 'Firebase Cloud Function Email', // Add your email subject here
      },
    },
    Source: 'sender@example.com', // Add the sender email address
  };

  try {
    await ses.sendEmail(params).promise();
    res.status(200).send('Email sent successfully');
  } catch (error) {
    console.error('Error sending email:', error);
    res.status(500).send('Error sending email');
  }
});

Code reference: here

Lonare
  • 3,581
  • 1
  • 41
  • 45