Put link in message body sent with Mailgun?

1.7k views Asked by At

Im using Mailgun to send emails with this function on my node server.

The link to my site is coming out as plain text rather than a clickable link. How can I make the link be an actual href in the email?

const emailWelcome = ({ name, email }) => {
  const data = {
    from: 'James <[email protected]>',
    to: email,
    subject: 'Welcome to My Site',
    text: `Hi ${name},

    Welcome to <a href="http://example.com/">My Site</a>

    James
    `,
  };

  mailgun.messages().send(data, function(error, body) {
    console.log(body);
  });
};
1

There are 1 answers

1
ficuscr On

Looking at the documentation for mailgun-js it says:

Sending messages in MIME format can be accomplished using the sendMime() function of the messages() proxy object.

https://www.npmjs.com/package/mailgun-js#sending-mime-messages

The documentation then provides this code example:

var domain = 'mydomain.org';
var mailgun = require('mailgun-js')({ apiKey: "YOUR API KEY", domain: domain });
var MailComposer = require('nodemailer/lib/mail-composer');
 
var mailOptions = {
  from: '[email protected]',
  to: '[email protected]',
  subject: 'Test email subject',
  text: 'Test email text',
  html: '<b> Test email text </b>'
};
 
var mail = new MailComposer(mailOptions);
 
mail.compile().build((err, message) => {
 
    var dataToSend = {
        to: '[email protected]',
        message: message.toString('ascii')
    };
 
    mailgun.messages().sendMime(dataToSend, (sendError, body) => {
        if (sendError) {
            console.log(sendError);
            return;
        }
    });
});

At the time of sharing this, the current version is 0.20.0.

As the code demonstrates, with a multi-part message you will need to provide both a plain text and HTML version of the message.