最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

尝试使用带有Angular和Node的SendGrid

运维笔记admin13浏览0评论

尝试使用带有Angular和Node的SendGrid

尝试使用带有Angular和Node的SendGrid

使用Angular和SendGrid,我正在尝试发送电子邮件。我正确安装了NPM包,但是在实现代码方面遇到了问题。我生成了一个API密钥并将其存储在目录中

echo "export SENDGRID_API_KEY='YOUR_API_KEY'" > sendgrid.env
echo "sendgrid.env" >> .gitignore
source ./sendgrid.env

打字稿是:

sgemail(){
  const sgMail = require('@sendgrid/mail'); //ERROR: Cannot find name 'require'.
  sgMail.setApiKey(process.env.SENDGRID_API_KEY); //ERROR: Cannot find name 'process'.
  const msg = {
    to: '[email protected]',
    from: '[email protected]',
    subject: 'Sending with SendGrid is Fun',
    text: 'and easy to do anywhere, even with Node.js',
    html: '<strong>and easy to do anywhere, even with Node.js</strong>',
  };
  console.log(msg);
  sgMail.send(msg);
}

我按下按钮即可解雇。

Sendgrid在他们的网站上没有关于导入包的信息,比如你必须使用import { Vibration } from '@ionic-native/vibration';来使用Ionic的振动包。

回答如下:

您可以尝试使用fetch手动发送POST请求到他们的Send Mail API。并且不要忘记Authorization Headers。下面是一个相同的未经测试的JavaScript代码段。填写YOUR_API_KEY并将电子邮件更新到您的某封电子邮件。

  var payload = {
    "personalizations": [
      {
        "to": [
          {
            "email": "[email protected]"
          }
        ],
        "subject": "Hello, World!"
      }
    ],
    "from": {
      "email": "[email protected]"
    },
    "content": [
      {
        "type": "text/plain",
        "value": "Hello, World!"
      }
    ]
  };
  var myHeaders = new Headers({
    "Content-Type": "application/json",
    "Authorization": "Bearer YOUR_API_KEY",
  });
  var data = new FormData();
  data.append( "json", JSON.stringify( payload ) );
  fetch("https://api.sendgrid/v3/mail/send",
  {
      method: "POST",
      headers: myHeaders,
      body: data
  })
  .then(function(res){ return res.json(); })
  .then(function(data){ console.log( JSON.stringify( data ) ) })
发布评论

评论列表(0)

  1. 暂无评论