我无法在Node.js应用中使用SendGrid发送邮件(故障)

时间:2018-08-18 13:55:46

标签: sendgrid nodejs-8.11

我正在nodejs

中使用 SendGrid API

这是我的 server.js

var express = require('express');
var app = express();
var bodyParser = require("body-parser");
const sgMail = require('@sendgrid/mail');
require('dotenv').config();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

app.use(express.static('public'));

// http://expressjs.com/en/starter/basic-routing.html
app.get("/", (req, res)=> {
sgMail.setApiKey(process.env.SENDGRID_KEY);
const msg = {
  to: 'test@gmail.com',
  from: 'test@test.com',
  subject: 'Welcome',
  text: 'text',
  html: '  <body style="margin: 0; padding: 0; background: #2874f0"><style> @font-face {font-family: "Google Sans";font-style: normal;font-weight: 400;src: url("https://fonts.gstatic.com/s/googlesans/v6/4UaGrENHsxJlGDuGo1OIlL3Kwp5eKQtGBlc.woff2") format("woff2");unicode-range: U+0400-045F, U+0490-0491, U+04B0-04B1, U+2116;} @import url("https://fonts.googleapis.com/css?family=abc")</style><table align="center" cellpadding="0" cellspacing="0" width="100%" style="border-collapse: collapse;"><tr><td style="padding:20px; color: #fff; font-family: Google Sans"> Hello!</td></tr></table></body>',
};
sgMail.send(msg);
});
// listen for requests :)
var listener = app.listen(process.env.PORT, function() {
  console.log('Your app is listening on port ' + listener.address().port);
});

在我的 .env 文件

SENDGRID_KEY= MYKEY
  

我遇到错误env:第1行:SENDGRID_KEY:找不到命令

1 个答案:

答案 0 :(得分:0)

当我在 .env 文件中留一个空格时,我经常遇到类似的问题。仅当“它们在引号内” 时,变量值中才可以有空格。