简体   繁体   English

SendGrid模板事务性v3函数Firebase

[英]SendGrid template transactional v3 functions Firebase

I am using a function in Firebase to send an email every time there is a new record in Firestore, this worked fine, but it seems that SendGrid has been updated to a new version of transactional templates. 每当Firestore中有新记录时,我正在Firebase中使用一个函数来发送电子邮件,此方法工作正常,但似乎SendGrid已更新为事务模板的新版本。

What I had before in the body of my SendGrid transaction template was: 我之前在SendGrid事务模板的主体中拥有的是:

nombre: {{nombre}}
email: {{email}}
celular: {{celular}}
valorPropiedad: {{valorPropiedad}}

This worked correctly, that is, it sent the mail (every time there was a new record in Firestore) with the data of the new record, but now it only sends the mail but arrives without any data. 这可以正常工作,也就是说,它使用新记录的数据发送了邮件(每次在Firestore中都有新记录),但是现在它仅发送邮件,但是到达时没有任何数据。 I think something has changed in SendGrid? 我认为SendGrid发生了变化吗? or is it a theme of my function? 还是我功能的主题?

Before I used Angular 5 I am now using version 6. 在使用Angular 5之前,我现在使用的是版本6。

Here the code of my function index.js : 这是我的函数index.js的代码:

const functions = require('firebase-functions');

const admin = require('firebase-admin');
admin.initializeApp();

const SENDGRID_API_KEY = functions.config().sendgrid.key

const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(SENDGRID_API_KEY);

exports.firestoreEmail2 = functions.firestore
    .document('domiciliarios/{domiciliarioId}')
    .onCreate((snap, context) => {

        const domiciliarioId = context.params.domiciliarioId;
        const db = admin.firestore()
        return db.collection('domiciliarios').doc(domiciliarioId)
            .get()
            .then(doc => {

                const domiciliario = doc.data();

                const msg = {
                    from: 'mail1@mail.com',
                    to: 'mail2@mail.com',
                    subject: 'mySubject',
                    templateId: 'myTemplateId',
                    substitutionWrappers: ['{{', '}}'],
                    substitutions: {
                        nombre: domiciliario.nombre,
                        email: domiciliario.email,
                        celular: domiciliario.celular,
                        valorPropiedad: `US$ ${domiciliario.valorPropiedad}`,
                    }
                };

                return sgMail.send(msg)
            })
            .then(() => console.log('email sent!'))
            .catch(err => console.log(err))

});

After some brute force testing this combination worked. 经过一番蛮力测试后,这种组合奏效了。

 exports.firestoreEmail = functions.firestore .document( 'members/{memberId}' ) .onCreate( ( snap, context ) => { const member = snap.data(); mail.setApiKey( SENDGRID_API_KEY ); const msg = { to: member.email, from: "hello@angularfirebase.com", subject: "Welcome", templateId: "xxx", substitutions: { name: member.name, email: member.email, membershipId: member.memberId }, dynamic_template_data: { name: member.name, email: member.email, membershipId: member.memberId } }; return mail.send( msg ).then( () => console.log( 'email sent to receiver2!' ) ) .catch( err => console.log( err ) ); } ); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM