mailgun.js inline image attachments
Asked Answered
T

1

5

I want to embed inline images with mailgun, however the documentation for sending attachments doesnt seem to match with the latest mailgun.js NOT mailgun-js

https://www.npmjs.com/package/mailgun.js

the mentioned docs in this readme do not exist: https://documentation.mailgun.com/api-sending.html#sending << 404's

The readme docs for this version say this: image

But where do I even add an attachment? How do I set mailgun to use multipart/form-data encoding? Where/ what parameter do I use to add an attachment? There are no good type definitions either

If i have this code below, how do I just add an attachment? What's the object schema? Does it even go here? I have no clue

mg.messages.create('sandbox-123.mailgun.org', {
    from: "Excited User <[email protected]>",
    to: ["[email protected]"],
    subject: "Hello",
    text: "Testing some Mailgun awesomness!",
    html: "<h1>Testing some Mailgun awesomness!</h1>"
  })

Even once set as an attachment, how do I reference it in the email to use in <img> ?

Anyone that has done with the latest mailgun.js please share your reference code. The documentation is outdated

Tali answered 6/5, 2021 at 18:18 Comment(0)
F
7

The documentation of mailgun.js can be a bit confusing, at least for node JS, since their website refers to the old version SDK whereas their github page points to the new one. That said, their github page does offer examples for including figures in the email. There are two ways to do so

  • Sent as attachment files but NOT displayed in the email.
  • Sent as inline figure and displayed in the email.

Below shows an example (original code available here) of sending figures both as attachment and inline. Notice that for inline figures to display, one must anchor the file name with a prefix cid: in an HTML template.

To send multiple attachments or inline figures, include all files in a list (the example below uses a list of one file for both inline and attachment).

/**
Copied from https://github.com/mailgun/mailgun-js/blob/master/examples/send-email.js
*/

const fs = require('fs');
const domain = 'sandbox-123.mailgun.com';
const fromEmail = 'Excited User <[email protected]>';
const toEmails = ['[email protected]'];

const mailgunLogo = fs.createReadStream(`${__dirname}/mailgun.png`);
const rackspaceLogo = fs.createReadStream(`${__dirname}/rackspace.png`);

mg.messages.create(domain, {
  from: fromEmail,
  to: toEmails,
  subject: 'Hello',
  html: '<img src="cid:mailgun.png" width="200px"><br><h3>Testing some Mailgun awesomness!</h3>',
  text: 'Testing some Mailgun awesomness!',
  inline: [mailgunLogo],
  attachment: [rackspaceLogo]
})
  .then((msg) => console.log(msg))
  .catch((err) => console.log(err));
Flushing answered 5/11, 2021 at 2:10 Comment(1)
Thank you! You just saved me hours of frustration.Rosalynrosalynd

© 2022 - 2024 — McMap. All rights reserved.