使用 Mailgun 从 Base64 字符串发送 pdf 附件

Use Mailgun to send a pdf attachment from Base64 string

我有一个由另一个函数生成的 pdf,returns 一个 Base64 字符串。然后我想将它附加到 Mailgun 电子邮件 as attachment, which is built into Meteor and Mailgun. I see that there is a lot of examples of attaching a file from the ,但我没有看到任何使用 Base64 的内容。

我有一个生成 Base64 字符串并与前缀连接的方法,以便 convert Base64 to PDF:

//returns base64 string: looks like "YW55IGNhcm5hbCBwbGVhc3VyZQ=="
const base64AttachmentString = 'data:application/pdf;base64,' + generatePdfBase64();

import { Email } from "meteor/email";

Email.send({
  to: "email@example.com",
  from: "John Smith <johnsmith@example.com>",
  subject: "Sending Base64 as PDF",
  html: generatedHTMLTemplate,
  attachment: base64AttachmentString
});

有没有办法发送 Base64 附件,Mailgun 会将其识别为 PDF?我知道这对于其他邮件程序是可能的,例如 Nodemailer and SendGrid.

meteor 的邮件好像需要添加attachments键,应该是附件数组。

至于附件选项 - there are multiple:

    {   // utf-8 string as an attachment
        filename: 'text1.txt',
        content: 'hello world!'
    },
    {   // binary buffer as an attachment
        filename: 'text2.txt',
        content: new Buffer('hello world!','utf-8')
    },
    {   // file on disk as an attachment
        filename: 'text3.txt',
        path: '/path/to/file.txt' // stream this file
    },
    {   // filename and content type is derived from path
        path: '/path/to/file.txt'
    },
    {   // stream as an attachment
        filename: 'text4.txt',
        content: fs.createReadStream('file.txt')
    },
    {   // define custom content type for the attachment
        filename: 'text.bin',
        content: 'hello world!',
        contentType: 'text/plain'
    },
    {   // use URL as an attachment
        filename: 'license.txt',
        path: 'https://raw.github.com/andris9/Nodemailer/master/LICENSE'
    },
    {   // encoded string as an attachment
        filename: 'text1.txt',
        content: 'aGVsbG8gd29ybGQh',
        encoding: 'base64'
    },
    {   // data uri as an attachment
        path: 'data:text/plain;base64,aGVsbG8gd29ybGQ='
    }

具体来说,在您的示例中,您可以使用:

const base64AttachmentString = 'data:application/pdf;base64,' + generatePdfBase64();

import { Email } from "meteor/email";

Email.send({
  to: "email@example.com",
  from: "John Smith <johnsmith@example.com>",
  subject: "Sending Base64 as PDF",
  html: generatedHTMLTemplate,
  attachments: [
    {
      path: base64AttachmentString
    }
  ]
});