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

问题描述 投票:0回答:1

我有另一个函数正在生成的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 protected]",
  from: "John Smith <[email protected]>",
  subject: "Sending Base64 as PDF",
  html: generatedHTMLTemplate,
  attachment: base64AttachmentString
});

是否可以通过Mailgun将Base64附件识别为PDF?我知道使用NodemailerSendGrid等其他邮件也是可能的。

javascript meteor base64 email-attachments mailgun
1个回答
0
投票

似乎流星的电子邮件要求您添加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 protected]",
  from: "John Smith <[email protected]>",
  subject: "Sending Base64 as PDF",
  html: generatedHTMLTemplate,
  attachments: [
    {
      path: base64AttachmentString
    }
  ]
});
© www.soinside.com 2019 - 2024. All rights reserved.