微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用 Firebase 函数和 Amazon SES 发送电子邮件

如何解决使用 Firebase 函数和 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 一起工作。

  1. 安装 nodemailer (npm i nodemailer)
  2. 安装 nodemailer-ses-transport
  3. 将区域更改为适合您设置的区域
  4. 在 Firebase 函数的 index.js 中输入以下内容(输入您的 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);
    }); 
});

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。