简体   繁体   中英

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.

What I had before in the body of my SendGrid transaction template was:

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. I think something has changed in SendGrid? or is it a theme of my function?

Before I used Angular 5 I am now using version 6.

Here the code of my function 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 ) ); } ); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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