繁体   English   中英

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

[英]Use Mailgun to send a pdf attachment from Base64 string

我有一个 pdf 由另一个 function 生成,它返回一个 Base64 字符串。 然后我想将它附加到 Mailgun email作为附件,它内置在 MeteorMailgun中。 我看到有很多从文件系统附加文件的示例,但我没有看到使用 Base64 的任何内容。

我有一种方法可以生成 Base64 字符串并用前缀连接,以便将 Base64 转换为 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? 我知道这对于其他邮件程序是可能的,例如NodemailerSendGrid

似乎流星的 Email 需要您添加attachments键,这应该是附件数组。

至于附件的选项 - 有多个

    {   // 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
    }
  ]
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM