簡體   English   中英

使用 Firebase 函數和 Amazon SES 發送 Email

[英]Send Email with Firebase Functions and Amazon SES

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). 因此,我安裝了 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)
 })
})

問題是,我什至無法部署這個 function:每次我收到相同的錯誤消息時:

...
+  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 Function 就可以了。 所以這與我的設置無關。

有人知道我做錯了什么嗎?

謝謝!! 克里斯

我找到了解決方案。 我仍然不知道它如何與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);
    }); 
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM