mailgun / mailgun.js

Javascript SDK for Mailgun
https://www.npmjs.com/package/mailgun.js
Apache License 2.0
519 stars 110 forks source link

recipient-variables dont seem to be supported for emails with '+' characters in the local-part #413

Closed danielmcconville closed 4 months ago

danielmcconville commented 4 months ago

When sending batch emails using the recipient-variables field, if the local-part (username) of the email contains a + character (or some other allowed characters according to RFC 5321), the recipient-variables mapping fails.

Example

const formData = require('form-data');
const Mailgun = require('mailgun.js');

const mailgun = new Mailgun(formData).client({
  key: '',
  username: 'api',
  url: 'https://api.eu.mailgun.net',
});

const recipientVars = {
  'foo+bar@gmail.com': {
    subject: 'Test',
    html: '<h1>Hello</h1><br />This is a test email.',
  },
};

mailgun.messages
  .create('mg.somedomain.com', {
    from: 'Admin <postmaster@mg.somedomain.com>',
    html: '%recipient.html%',
    'recipient-variables': JSON.stringify(recipientVars),
    to: 'foo+bar@gmail.com',
    subject: '%recipient.subject%',
  })
  .then((res) => console.log(res));

The above will create an email with the following details:

To

%recipient%
From

Admin <postmaster@mg.somedomain.com>
Subject

%recipient.subject%

As you can see the template variables have not been replaced with the provided recipient-variables. The above example will work correctly if you replace the email address with one that does not contain a + character.

danielmcconville commented 4 months ago

Closing this issue as the email is delivered, the issue is with the Mailgun logging. The logging shows the template values rather than the replaced values leading to confusion. This happens with all emails sent in this way, not just ones with special characters in the local-part.