【问题标题】:Add various fields in the text section of nodemailer在nodemailer的文本部分添加各种字段
【发布时间】:2020-06-22 11:14:15
【问题描述】:

我正在使用 nodemailer 从我的 Angular 应用程序发送邮件。我有一个变量,它有几个属性,如firstnamemiddlenameemailmobileaddress。我正在从 firebase 获取这些数据,每个变量都可以通过编写 $data.firstname$data.email 来访问。我现在只能发送 1 个变量。我想将所有变量发送到带有标签的邮件中

所以邮件内容应该是

  • 电子邮件 - abc@123
  • 名字 - ABC
  • 地址 - LMN
  • 手机 - 7777777777

请帮帮我。

const functions = require('firebase-functions');
const admin = require('firebase-admin');
const nodemailer = require('nodemailer');
admin.initializeApp()
require('dotenv').config()


const {SENDER_EMAIL, SENDER_PASS} = process.env;

exports.sendMailNotification1=functions.firestore.document('submissions/{docID}')
.onCreate((snap, ctx)=> {
    const data=snap.data();

    let authData=nodemailer.createTransport({
        host: 'smtp.gmail.com',
        port: 465,
        secure:true,
        auth:{
            user: SENDER_EMAIL,
            pass: SENDER_PASS
        }
    });
    authData.sendMail({
        from: 'xxx@gmail.com',
        to: 'xyz@gmail.com',
        subject: 'Appointment Info ',
        text:`${data.fname}`,
        html:`${data.email}`,
    }).then(res=>console.log('Succesfully Sent')).catch(err=> console.log(err)
    );
})

【问题讨论】:

  • 我不确定我是否理解,但您有供用户输入这些数据的联系表吗?
  • 是的,我有一个表格,用户输入数据。我应该将该数据发送到某个 X 人的邮件中。所以我在nodemailer的帮助下做到了这一点。首先我将表单数据发送到firebase,并在上面的函数sendEmailNotification的帮助下,我正在发送邮件。问题是我只能发送 1 个变量。 $data.email。但我想发送所有变量

标签: node.js firebase google-cloud-firestore google-cloud-functions nodemailer


【解决方案1】:

如 NodeMailer doc 中所述,您可以选择:

使用messagetext 元素发送“消息的明文版本”

使用messagehtml 元素发送“消息的HTML 版本”。


例如,如果您使用 HTML 选项,您可以使用如下的 HTML 列表:

//...
const htmlContent = `<ul><li>Email - ${data.email}</li><li>Address - ${data.address}</li></ul>`;

return authData.sendMail({
        from: 'xxx@gmail.com',
        to: 'xyz@gmail.com',
        subject: 'Appointment Info',
        html: htmlContent
    })
.then(res => {
       console.log('Succesfully Sent');
       return null;
})
.catch(err => {
       console.log(err);
       return null;
});

注意在代码中添加了几个returns,有关此关键方面的更多信息,请参阅https://firebase.google.com/docs/functions/terminate-functions

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-06-23
  • 2021-11-24
  • 2018-06-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-02
相关资源
最近更新 更多