Reputation: 437
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: '[email protected]'
, from: '[email protected]'
//, cc: '[email protected]'
//, bcc: ['[email protected]', '[email protected]']
, 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
Upvotes: 2
Views: 1874
Reputation: 4673
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: ['[email protected]'], // 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: '[email protected]', // 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
Upvotes: 1
Reputation: 437
I found a solution. I still do not know how it works with node-ses
but I know how it works with nodemailer
.
npm i nodemailer
)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: '[email protected]',
to: '[email protected]',
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);
});
});
Upvotes: 2