使用 Firebase Functions 和 Amazon SES 发送电子邮件
Send Email with Firebase Functions and Amazon SES
我只想通过 Firebase Functions 和 AWS Simple Email Service (SES) 从经过验证的域和经过验证的电子邮件地址(仍在沙箱中)发送一封电子邮件来测试连接。因此我安装了 node-ses 并创建了以下代码。我将 vuejs 用于 webapp 和 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)
})
})
问题是,我什至无法部署此功能:每次我收到相同的错误消息:
...
+ 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.
但如果我部署一个简单的 Firebase 函数,它就可以工作。所以这和我的设置无关。
有人知道我做错了什么吗?
谢谢!!
克里斯
我找到了解决办法。我仍然不知道它如何与 node-ses
一起使用,但我知道它如何与 nodemailer
.
一起使用
- 安装 nodemailer (
npm i nodemailer
)
- 安装 nodemailer-ses-transport
- 将区域更改为适合您设置的区域
- 在您的
index.js
Firebase 函数中输入以下内容(输入您的 AWS 凭证)
--- 下面的源代码 ---
// 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);
});
});
我只想通过 Firebase Functions 和 AWS Simple Email Service (SES) 从经过验证的域和经过验证的电子邮件地址(仍在沙箱中)发送一封电子邮件来测试连接。因此我安装了 node-ses 并创建了以下代码。我将 vuejs 用于 webapp 和 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)
})
})
问题是,我什至无法部署此功能:每次我收到相同的错误消息:
...
+ 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.
但如果我部署一个简单的 Firebase 函数,它就可以工作。所以这和我的设置无关。
有人知道我做错了什么吗?
谢谢!! 克里斯
我找到了解决办法。我仍然不知道它如何与 node-ses
一起使用,但我知道它如何与 nodemailer
.
- 安装 nodemailer (
npm i nodemailer
) - 安装 nodemailer-ses-transport
- 将区域更改为适合您设置的区域
- 在您的
index.js
Firebase 函数中输入以下内容(输入您的 AWS 凭证)
--- 下面的源代码 ---
// 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);
});
});