【问题标题】:How to attach files to POST request to Mailgun's API using Node.js and the Request library如何使用 Node.js 和请求库将文件附加到 Mailgun 的 API 的 POST 请求
【发布时间】:2012-12-06 06:51:13
【问题描述】:

我正在编写一些代码来通过Mailgun email service 发送带有附件的电子邮件。他们在API docs using CURL 中给出了以下示例,我需要弄清楚如何在 Node.js 中做同样的事情(最好使用 Request 库)。

curl -s -k --user api:key-3ax6xnjp29jd6fds4gc373sgvjxteol0 \
    https://api.mailgun.net/v2/samples.mailgun.org/messages \
    -F from='Excited User <me@samples.mailgun.org>' \
    -F to='obukhov.sergey.nickolayevich@yandex.ru' \
    -F cc='sergeyo@profista.com' \
    -F bcc='serobnic@mail.ru' \
    -F subject='Hello' \
    -F text='Testing some Mailgun awesomness!' \
    -F html='\<html\>HTML version of the body\<\html>' \
    -F attachment=@files/cartman.jpg \
    -F attachment=@files/cartman.png

我当前的代码(Coffescript)如下所示:

r = request(
  url: mailgun_uri
  method: 'POST'
  headers:
    'content-type': 'application/x-www-form-urlencoded'
  body: email
  (error, response, body) ->
    console.log response.statusCode
    console.log body
)
form = r.form()
for attachment in attachments
  form.append('attachment', fs.createReadStream(attachment.path))

【问题讨论】:

    标签: node.js request mailgun


    【解决方案1】:

    对于基本授权部分,您必须设置正确的标头并发送 base64 编码的用户名和密码。有关更多信息,请参阅此SO question。您可以为此使用headers 选项。

    request docs 中描述了如何发送带有表单字段的 POST 请求:

    var r = request.post('http://service.com/upload')
    var form = r.form()
    form.append('from', 'Excited User <me@samples.mailgun.org>') // taken from your code
    form.append('my_buffer', new Buffer([1, 2, 3]))
    form.append('my_file', fs.createReadStream(path.join(__dirname, 'doodle.png')) // for your cartman files
    form.append('remote_file', request('http://google.com/doodle.png'))
    

    npm 上还有一些支持 mailgun 的现有模块,例如

    nodemailer 的一个例子是

    var smtpTransport = nodemailer.createTransport("SMTP",{
        service: "Mailgun", // sets automatically host, port and connection security settings
        auth: {
            user: "api",
            pass: "key-3ax6xnjp29jd6fds4gc373sgvjxteol0"
        }
    });
    
    var mailOptions = {
      from: "me@tr.ee",
      to: "me@tr.ee",
      subject: "Hello world!",
      text: "Plaintext body",
      attachments: [
        {   // file on disk as an attachment
            fileName: "text3.txt",
            filePath: "/path/to/file.txt" // stream this file
        },
        {   // stream as an attachment
            fileName: "text4.txt",
            streamSource: fs.createReadStream("file.txt")
        },
      ]
    }
    
    transport.sendMail(mailOptions, function(err, res) {
      if (err) console.log(err);
      console.log('done');
    });
    

    没有测试它,因为我没有 mailgun 帐户,但它应该可以工作。

    【讨论】:

    • 我按照上述模式附加文件,Mailgun 发回一个错误,说“'from' 参数丢失”,这让我认为我做错了什么。 node-mailgun 不支持附件(或 html 电子邮件)我没想过使用 nodemailer。它确实支持附件和 Mailgun,虽然通过 SMTP 网关,这很好,但我仍然很好奇如何
    • 您是否将url = "https://api:key-3ax6xnjp29jd6fds4gc373sgvjxteol0@api.mailgun.net/v2/samples.mailgun.org/log"; 等授权参数添加到mailgun_uri?我在回答中为 nodemailer 添加了一些代码。
    • 是的——我没有显示我所有的代码。稍后我会试一试nodemailer。
    猜你喜欢
    • 1970-01-01
    • 2017-04-30
    • 1970-01-01
    • 2021-05-17
    • 2018-10-29
    • 1970-01-01
    • 1970-01-01
    • 2018-03-06
    • 1970-01-01
    相关资源
    最近更新 更多