Meteor : Email a Template in Client using Mailgun

Question!

I have a Template in client

<template name="sendThis">
<img src="logo.png"><br>
<h3>Welcome to Meteor NewBie</h3>
Dear {{name}},
<p>You received this Email because you have subscribed to http://www.example.com</p>
</template>

I would like to send this Template(sendThis) as HTML body in my Email to subscribers.

I am using Mailgun as my Email Client. What are the steps I should take to make this happen as a subscriber clicks a button with an id "subscribe".

PS: I have multiple helpers in this template, multiple in the sense more than 20.

Thanks in advance. Mahesh B.



Answers

One way to solve this is to use Blaze.toHTMLWithData to render your template (with a context) to an HTML string. You can then call a method on your server which emails the user with the appropriate subject and address. Here's an example:

client

var sendSignupEmail = function() {
  // This assumes this first email address is the one you want.
  // In some cases you may want the first verified email, but not
  // during signup.
  var address = Meteor.user().emails[0].address;

  var subject = 'Thanks for signing up!';

  // Here I used username - replace this with the appropriate data
  // like Meteor.user().profile.firstName or something.
  var body = Blaze.toHTMLWithData(Template.sendThis, {
    name: Meteor.user().username
  });
  Meteor.call('sendEmail', address, subject, body);
};

server

Meteor.methods({
  sendEmail: function(to, subject, html) {
    check([to, subject, html], [String]);
    this.unblock();

    return Email.send({
      to: to,
      from: '[email protected]',
      subject: subject,
      html: html
    });
  }
});

Also make sure your MAIL_URL environment variable has been defined.



This video can help you solving your question :)
By: admin