Send custom data into mailgun

Hey devs!

I’m having some problems sending custom data to mailgun from the included package in Meteor.

I’ve looked at the documentation on https://documentation.mailgun.com/en/latest/user_manual.html#attaching-data-to-messages on how to add the data when sending with SMTP but for some reasons, I keep getting an empty user-variables in mailgun.

Anyone has an example where do something similar? :slight_smile:

I’ve tried multiple things, but for some reason i can only get the data send through using API, but would rather just use the package that we already use and have setup with all the templates… Here is my last try

return sendEmail({
    to: mail,
    from: `Eatie <test@test.dk>`,
    subject: `${subject} (${people} pers. - ${date} - ${time})`,
    headers: {
      'x-my-key': 'header value',
      headerName: JSON.stringify({ tracking: { level: 'full' } }),
    },
    languageCode,
    template,
})

Hi there!

Which package are you using to send emails? Right now we’re using Mailgun with custom email templates using the default emails package! So far we’re not running into any issues.

This is an example of using a non-built in email type:

adminMailer = function(to, from, subject, conObj, type, admin, message, reason){
  if(type == 'contact'){
    SSR.compileTemplate('htmlEmail', Assets.getText('emails/contactRequest.html'));
    var emailData = {
      admin: admin,
      userMessage: conObj.message,
      adminResponse: message,
    };
  }
  var html = SSR.render('htmlEmail', emailData)
  Email.send({
    to: to,
    from: from+' <'+from+'>',
    subject: subject,
    headers:{"sender":"APP Staff <staff@APP.com"},
    html: html
  })
}

And an example using a built-in type, in this case Reset Password:

Accounts.emailTemplates.resetPassword = {
  subject() {
    return `Password Reset Request`;
  },
  html(user, url) {
    SSR.compileTemplate('htmlEmail', Assets.getText('emails/resetPassword.html'));
    var emailData = {
      username: user.username,
      url: url,
    };
    return SSR.render('htmlEmail', emailData)
  }
};

Not sure if these examples help you or not, so sorry if they don’t!

Hey Superbird!

Thanks for the example!

Our app is using the cleverbeagle version of Meteor, called Pup. Which should also use the default emails package from Metoer. So my functions look like this:

return sendEmail({
    to: mail,
    from: `from <mail@test.dk>`,
    subject: `${subject} (${people} pers. - ${date} - ${time})`,
    headers: { sender: 'APP Staff <staff@APP.com' },
    languageCode,
    template,
    messageId,
    templateVars: {templateVars}
})

Which then call the Meteor method Email.send

const sendEmail = (options, { resolve, reject }) => {
  console.log('options:', options);
  try {
    Meteor.defer(() => {
      Email.send(options, {});
      resolve();
    });
  } catch (exception) {
    reject(exception);
  }
};

The wierd thing is when i console.log the options, i can see the headers inside it, but after it’s sent i can’t find the data anywhere on mailgun…

But thanks for the example mate!