【问题标题】:Email via Google Cloud Functions trigger erratic syntax error通过 Google Cloud Functions 发送的电子邮件触发语法错误
【发布时间】:2019-06-07 01:50:17
【问题描述】:

在这些示例之后,我正在尝试使用谷歌云功能发送电子邮件: 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 to: {{email}}, ^ SyntaxError: Unexpected token

{
  "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?或者有人可以为我解决错误信息吗?

【问题讨论】:

    标签: google-cloud-functions sendgrid


    【解决方案1】:
    1. 第一个错误是由于mail@homepage' 中没有开头引号引起的
    2. 第二个可能是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`;
    };
    

    注意。因为我使用 Gmail 作为邮件服务器,所以需要两个额外的步骤才能从它发送电子邮件:

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

    【讨论】:

      猜你喜欢
      • 2017-11-10
      • 1970-01-01
      • 2021-06-06
      • 1970-01-01
      • 2021-11-20
      • 2012-07-20
      • 1970-01-01
      • 2021-04-18
      • 2016-08-29
      相关资源
      最近更新 更多