Node.js 如何使用NodeJs(sendgird)从url附加pdf

Node.js 如何使用NodeJs(sendgird)从url附加pdf,node.js,sendgrid,Node.js,Sendgrid,我想将url附加到电子邮件中,我使用了一些解决方案,例如通过请求获取pdf url或将其作为文件读取,但不起作用 const sgMail = require('@sendgrid/mail'); sgMail.setApiKey("XXXXXXXXXXXXXXXXXXXXXXXXXXXXX"); var array = [], temp = []; array.push('https://www.antennahouse.com/XSLsample/pdf/sample- link

我想将url附加到电子邮件中,我使用了一些解决方案,例如通过请求获取pdf url或将其作为文件读取,但不起作用

const sgMail = require('@sendgrid/mail');
sgMail.setApiKey("XXXXXXXXXXXXXXXXXXXXXXXXXXXXX");

var array = [],
    temp = [];

array.push('https://www.antennahouse.com/XSLsample/pdf/sample-
link_1.pdf');

array.push('https://www.antennahouse.com/XSLsample/pdf/sample-
link_1.pdf');

array.forEach(function(data) {
   temp.push({
      path: data,
      filename: 'hello.pdf'
  });

  const msg = {
    to: 'example@example.com',
    from: 'example@example.com',
    subject: 'Test',
    text: 'this is test',
    html: '<strong>Hello World</strong>',
    attachments: temp,
  };

  if ( temp.length == array.length )
      sgMail.send(msg);
  });
const sgMail=require('@sendgrid/mail');
sgMail.setApiKey(“xxxxxxxxxxxxxxxxxxxxxxxx”);
变量数组=[],
温度=[];
array.push('https://www.antennahouse.com/XSLsample/pdf/sample-
链接_1.pdf');
array.push('https://www.antennahouse.com/XSLsample/pdf/sample-
链接_1.pdf');
array.forEach(函数(数据){
临时推力({
路径:数据,
文件名:“hello.pdf”
});
常数msg={
致:'example@example.com',
发件人:'example@example.com',
主题:“测试”,
文本:“这是测试”,
html:“你好,世界”,
附件:临时工,
};
if(temp.length==array.length)
sgMail.send(msg);
});
我们有来自的示例。注意:内容必须是字符串base64编码的

const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
const msg = {
  to: 'recipient@example.org',
  from: 'sender@example.org',
  subject: 'Hello attachment',
  html: '<p>Here’s an attachment for you!</p>',
  attachments: [
    {
      content: 'Some base 64 encoded attachment content',
      filename: 'some-attachment.pdf',
      type: 'application/pdf',
      disposition: 'attachment',
      contentId: 'mytext'
    },
  ],
};
const sgMail=require('@sendgrid/mail');
sgMail.setApiKey(process.env.SENDGRID\u API\u KEY);
常数msg={
致:'recipient@example.org',
发件人:'sender@example.org',
主题:“你好,附件”,
html:“这是给你的附件!

”, 附件:[ { 内容:“一些基本64编码的附件内容”, 文件名:“some attachment.pdf”, 键入:“application/pdf”, 处置:'附件', contentId:“mytext” }, ], };
这是我的有效解决方案。您可以使用请求npm模块从任何源读取pdf文件,但不要错过{encoding:null}

const sgMail = require('@sendgrid/mail');
var request = require('request');
request.get('https://someurl/output.pdf',{ encoding: null },(err,res) => {
  console.log(res);
  var base64File = new Buffer(res.body).toString('base64');

  sgMail.setApiKey('yourSendgridApiKey');
  const msg = {
    to: 'to@mail.com',
    from: 'from@mail.com',
    subject: 'Sending with SendGrid is Fun',
    text: 'and easy to do anywhere, even with Node.js',
    attachments: [
      {
        content: base64File,
        filename: 'some.pdf',
        ContentType: 'application/pdf',
        disposition: 'attachment',
        contentId: 'mytext'
      },
    ]
  }
  sgMail.send(msg, (error, data) => {
    if (error) {
      console.log('error', error)
    } else {
      console.log('successfully sent email' + data);
    }
  });
})

根据,似乎您想要的(附加url)不受支持。(请查看
附件
部分)。您可以使用
content
property下载文件并附加。如果您需要有关下载文件并附加其内容的帮助,请告诉我。
const sgMail = require('@sendgrid/mail');
const pdf2base64 = require('pdf-to-base64'); 
sgMail.setApiKey(apiKey); 
const body = await sgMail.send({
      to: email,
      from,
      subject: "subject",
      html: "hello welcome",
      ...(
        attachments && attachments.length && {
          attachments:attachments.map(attachment=>({
            content: attachment,
            filename: "attachment.pdf",
            type: "application/pdf",
            disposition: "attachment"
          }))
        }
      )
    });
     return body;