1

I want to send a mail when a new reservation is made in firestore with some data from there for testing purposes it's only firstName, lastName and email for now but that doesn't change a thing. This should send a mail on creation of a document in firestore but it gives with sendgrid but how can I change this to do this with gmail instead of sendgrid?

import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
import * as sgMail from '@sendgrid/mail';

admin.initializeApp(functions.config().firebase);

const SENDGRID_API_KEY = functions.config().sendgrid.key;
sgMail.setApiKey(SENDGRID_API_KEY);

exports.firestoreEmail = functions.firestore
  .document('reservations/{reservationId}')
  .onCreate(event => {
    const reservationId = event.params.reservationId;
    const db = admin.firestore();

    return db.collection('reservations').doc(reservationId)
      .get()
      .then(doc => {
        const reservation = doc.data();

        const msg = {
          to: reservation.mail,
          from: 'my-email@gmail.com',
          templateId: 'my-key',
          substitutionWrappers: ['{{', '}}'],
          substitutions: {
            firstName: reservation.firstName,
            lastName: reservation.lastName
          }
        };
        return sgMail.send(msg)
      })
      .then(()=> console.log('email sent!'))
      .catch(err => console.log(err))
  });

To test this I create a new reservation in my firestore db enter image description here

How do I use gmail instead of sendgrid for instance to send mails or is there any other way that I can send mails from clientside as there is no backend possible?

Burst of Ice
  • 386
  • 2
  • 6
  • 23
  • Is your project on a paid plan? If not, the warning *does* apply to you and you won't be able to call the sendgrid API. See https://stackoverflow.com/questions/42774807/cloud-functions-for-firebase-getaddrinfo-enotfound/42775841 To find out how to send mail through gmail: https://www.google.com/search?q=node.js+call+gmail+to+send+mail – Frank van Puffelen Jun 17 '18 at 14:31
  • I don't think that's the case I switched to gmail-send and it gives the same warning about billing plan but sends the mail as unformatted text. – Burst of Ice Jun 17 '18 at 16:15
  • The warning will always be there if you're on the free/spark plan. But when you are trying to call sendgrid you're actually affected by what the message talks about. If you're having problems sending through gmail, open a new question with the [minimal-but-complete code that reproduces the problem](http://stackoverflow.com/help/mcve). – Frank van Puffelen Jun 17 '18 at 16:21
  • I'm not having any issues yet, thanks for the really quick replies! I'm now looking how to not plainly put my user and password. – Burst of Ice Jun 17 '18 at 16:31
  • You'll want to put those in environment configuration. See https://firebase.google.com/docs/functions/config-env – Frank van Puffelen Jun 17 '18 at 17:55
  • Already tried const firebaseConfig = JSON.parse(process.env.FIREBASE_CONFIG); const password = firebaseConfig.mail.pass; and then use the password in my code but that gives a syntax error Unexpected token u in JSON at position 0. So I assume it returns undefined somehow not yet sure how. – Burst of Ice Jun 17 '18 at 18:55

0 Answers0