通过Google Cloud Functions发送的电子邮件会触发不稳定的语法错误

时间:2019-01-12 09:45:42

标签: google-cloud-functions sendgrid

在这些示例之后,我正在尝试使用Google云功能来发送电子邮件: https://github.com/firebase/functions-samples/tree/master/quickstarts/email-usershttps://angularfirebase.com/lessons/sendgrid-v3-nodejs-transactional-email-cloud-function/ 然后我必须进行一些调整以适应开发中的技术,结果是:

const functions = require('firebase-functions');
const cors = require('cors')({ origin: true });
const sendGrid = require('@sendgrid/mail');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
const db = admin.firestore();
const SENDGRID_API_KEY="***";
sgMail.setApiKey(SENDGRID_API_KEY);
sendGrid.setApiKey(functions.config().sendgrid.apikey);
sendGrid.setSubstitutionWrappers('{{', '}}');
exports.httpEmail = functions.https.onRequest((req, res) => {
cors( req, res, () => { 

const name  = req.body.toName;
const email = req.body.Email;
const id = req.body.ID;

const msg = {
    to: email,
    from: mail@homepage.com',
    subject: id,
    text: `Hello {{name}}`,
    html: `<h1>Hello {{name}}</h1>`
};

return sgMail.send(msg)

    .then(() => res.status(200).send('email sent!') )
    .catch(err => res.status(400).send(err) )

});});

现在,这给了我一个语法错误

  

/user_code/index.js:20发送至:{{email}},^语法错误:意外令牌

{
  "name": "email",
  "description": "sending emails",
  "version": "0.0.1",
    "dependencies": {
    "sendgrid/mail": "^6.3.1",
    "cors": "^2.8.1",
    "moment": "^2.17.1",
    "firebase-admin": "~5.11.0",
    "firebase-functions": "^1.0.0"
  },
  "scripts": {
    "lint": "./node_modules/.bin/eslint --max-warnings=0 .",
    "serve": "firebase serve --only functions",
    "shell": "firebase experimental:functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "private": true
}

这给了我一个解析错误:

  

无法解析package.json,第7行第6列错误

这两个错误对我来说都是没有道理的,我必须进行4种不同的调整,这使我怀疑我的做法是正确的。任何人都可以使用的示例或教程,包括cors,带变量的文本和sendgrid?还是有人可以为我解决错误消息?

1 个答案:

答案 0 :(得分:0)

  1. 第一个错误是由于mail@homepage'
  2. 中没有引号引起的
  3. 秒可能是sendgrid/mail之前没有'@',或者"dependencies"下没有缩进

虽然sendgrid是一个不错的选择,但您也可以看看nodemailer。它不需要API密钥。两种方法的工作方式相同,如下面的示例所示。

app.yaml

runtime: nodejs8

package.json

{
  "dependencies": {
    "nodemailer": "^4.0.1",
    "@sendgrid/mail": "^6.3.1"
  }
}

app.js

const nodemailer = require('nodemailer');
const sendgridmail = require('@sendgrid/mail');

exports.sendEmail = (req, res) => {

  // destination email passed as argument
  var email = req.query.message;

  // sender email config (mailserver)
  var gmailEmail = process.env.EMAIL;
  var gmailPassword = process.env.PASSWORD;


  // Nodemailer config
  var mailTransport = nodemailer.createTransport({
    service: 'Gmail',
    auth: {
      user: gmailEmail,
      pass: gmailPassword
    }
  });

  var msg = {
    from: gmailEmail,
    to: email,
    subject: 'Nodemailer',
    text: 'Sent with Nodemailer'
  };

  mailTransport.sendMail(msg);

  // Sendgrid config
  sendgridmail.setApiKey(process.env.GRIDKEY);

  var msg = {
    from: gmailEmail,
    to: email,
    subject: 'Sendgrid',
    text: 'Sent with Sendgrid/mail'
  };

  sendgridmail.send(msg);

  return `Sent two emails with Sendgrid and Nodemailer`;
};

Nb。。当我使用Gmail作为邮件服务器时,需要执行两个附加步骤才能从中发送电子邮件:

  1. Allow Less Secure Apps
  2. Allow access to your Google account