How to add email template containing embed html co

2019-09-08 09:17发布

问题:

I am new to meteor js. Could you please suggest me how to add email template containing embed html code in meteor project. I had tried few packages,but its not working.Could you please explain with a sample example.

Note: When a user register into my site. Both of us (new user and me) have to get the email. Both contains different embed html content.

回答1:

Here is a basic example assuming you use blaze:

In your template events:

var email = emailAddress; //define emailAddress
var dataContext = {
    yourname: getDataFromSomewhere; //whatever you define in dataContext is used in email template
};
var html = Blaze.toHTMLWithData(Template.mailTemplateName, dataContext);
Meteor.call('sendMail', yourname, email, html); 

Mail template:

<template name="mailTemplateName">
    <h3>Hello {{yourname}}</h3>
</template>

Inside your methods:

sendMail: function(yourname, email, html) {
    this.unblock();
    var options = {
      from:"from@from.com",
      to: email,
      subject: 'Mail subject field',
      html: html
    };
    Email.send(options)
}

You only need email package (meteor add email in console or add email to your packages) and configure SMTP for this to work. Documentation for email package and usage here

SMTP configuration example (in server!)

Meteor.startup(function () {
    smtp = {
    username: 'someMail@something.com',
    password: 'password here',  
    server:   'server.something.com',
    port: 25 //change with correct port
}

process.env.MAIL_URL = 'smtp://' + encodeURIComponent(smtp.username) + ':' + encodeURIComponent(smtp.password) + '@' + encodeURIComponent(smtp.server) //+ ':' + smtp.port;
});

If you get whats going on in this code, you can easily play with it and send different emails with different data using different html templates



回答2:

I luckily implemented this requirement yesterday. So below is the direction to send HTML using email package.

execute command;

meteor add email

meteor add meteorhacks:ssr

Create an HTML which will be accessible by SERVER.

below is the sample code for HTML at PROJECT/email.html to add email package

<template name="email">
  <table>
    <tr>
      <td></td>
      <td width="600">
    <table>
      <tr>
        <th>Name</th>
        <td>{{name}}</td>
      </tr>
      <tr>
        <th>Age</th>
        <td>{{age}}</td>
      </tr>
    </table>
      </td>
    </tr>
  </table>
</template>

In SERVER/main.js file you need to import the package using,

import { Email } from 'meteor/email';

Meteor.startup(() => {

  //fill EMAIL_ID, PASSWORD
  process.env.MAIL_URL = "smtp://EMAIL_ID:PASSWORD@smtp.gmail.com:465";

  SSR.compileTemplate( 'htmlEmail', Assets.getText( 'email.html' ) );

    var emailData = {
      name: "Jishnu S",
      age: "25"       
    };

  Email.send({
      to: "abc@gmail.com",
      from: "email_ID",
      subject: "Example Email",
      html: SSR.render( 'htmlEmail', emailData )
    });
});

above code is for gmail SMTP configuration enabled emailing. check email and Voila..!!!! it works. Enjoy! Happy coding!