【问题标题】:Use Mailgun to send a pdf attachment from Base64 string使用 Mailgun 从 Base64 字符串发送 pdf 附件
【发布时间】:2020-07-15 22:06:47
【问题描述】:

我有一个由另一个函数生成的 pdf,它返回一个 Base64 字符串。然后我想将它附加到 Mailgun 电子邮件as attachment,即built into MeteorMailgun。我看到有很多从file system 附加文件的示例,但我没有看到任何使用 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?我知道这对于其他邮件程序是可能的,例如 NodemailerSendGrid

【问题讨论】:

    标签: javascript meteor base64 email-attachments mailgun


    【解决方案1】:

    似乎流星的电子邮件要求您添加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
        }
      ]
    });
    

    【讨论】:

    • 发送附件时是否需要包含任何多部分/表单数据编码?
    • 很确定你不需要那个。请检查并更新。
    • 它没有按原样工作 - 只是发送没有附件。但是,字符串正在记录。
    • @maudulus,仔细检查 - 请参阅答案中的更新
    • 试过并得到:TypeError: Path must be a string. Received &lt;Buffer ...。还尝试传递 base64 字符串,但得到:Exception while invoking method 'saveClaim' { Error: ENAMETOOLONG: name too long, open '。我认为它可能需要一个文件路径
    猜你喜欢
    • 1970-01-01
    • 2017-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-22
    • 2016-04-14
    • 2012-12-23
    相关资源
    最近更新 更多