Sendgrid 和 Node.js formData Body

问题描述

我在客户端生成一个 pdf 并将其发送到节点服务器。

let blob = pdf.output('blob');
let email = document.getElementById('email').value
        
const formData = new FormData();
formData.append('pdf',blob);
formData.append('email',email);
    
$.ajax('http://localhost:8080/test/test-1',{
     method: 'POST',data: formData,processData: false,contentType: 'application/x-www-form-urlencoded',success: function(data){console.log(data)},error: function(data){console.log(data)}
   });

我是我的节点控制器,我可以记录正文。

exports.sendForm = async (body) => {
  try {
    console.log(body);
  return;
  } catch (err) {
      return next(err);
  }
};

现在,在这个正文中有我的文件(blob?)和电子邮件输入的值。该文件需要附加到通过 SendGrid 发送的电子邮件中。我试过了,但肯定没用:

exports.sendForm = async (body) => {
  try {

    const msg = {
      to: 'test@gmail.com',from: 'test@gmail.com',subject: 'Sending with SendGrid is Fun',text: 'and easy to do anywhere,even with Node.js',attachments: [
        {
          content: body.toString("base64"),filename: "attachment.pdf",type: "application/pdf",disposition: "attachment"
        }
      ]
    }

    sgMail
      .send(msg)
      .then(() => {
          console.log("email sent");
      })
      .catch((error) => {
          console.error(error);
      });

    return;
  } catch (err) {
      return next(err);
  }
};

我正在阅读的教程使用具有特定路径的文件。我的文件是从前端发送的。 https://www.twilio.com/blog/sending-email-attachments-with-sendgrid

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)