如何在meteor项目中添加包含嵌入html代码的电子邮件模板

问题描述:

我是流星js的新手。您能否请我建议我如何在流星项目中添加包含嵌入式html代码的电子邮件模板。我曾尝试过几个软件包,但它不能正常工作。请您举例说明一下。如何在meteor项目中添加包含嵌入html代码的电子邮件模板

注:当用户注册到我的网站。我们两个人(新用户和我)都必须收到电子邮件。两者都包含不同的嵌入式html内容。

昨天我很幸运地实施了这个要求。下面是使用电子邮件包发送HTML的方向。

执行命令;

meteor add email

meteor add meteorhacks:ssr

创建HTML,这将是访问由服务器

下面

是在项目的HTML代码示例/ email.html添加电子邮件包

<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> 

SERVER/main.js文件,你需要使用导入的包,

import { Email } from 'meteor/email'; 

Meteor.startup(() => { 

    //fill EMAIL_ID, PASSWORD 
    process.env.MAIL_URL = "smtp://EMAIL_ID:[email protected]:465"; 

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

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

    Email.send({ 
     to: "[email protected]", 
     from: "email_ID", 
     subject: "Example Email", 
     html: SSR.render('htmlEmail', emailData) 
    }); 
}); 

上面的代码是用于启用gmail SMTP配置的电子邮件。 查收电子邮件和Voila .. !!!!有用。请享用!快乐的编码!

+0

嗨!我测试了你的代码,但是我的错误:无法在我的控制台上找到模块'./email.html':(相对路径是正确的 –

+0

你的目录结构是什么? –

这是一个基本的例子,假设你使用火焰:

在模板事件:

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); 

邮件模板:

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

里面你的方法:

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

你只有ne ed电子邮件包(控制台中的meteor add email或将email添加到您的软件包中)并配置SMTP以使其起作用。文档的电子邮件包和使用率here

SMTP配置示例(服务器!)

Meteor.startup(function() { 
    smtp = { 
    username: '[email protected]', 
    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; 
}); 

如果你得到什么的这段代码怎么回事,你可以很容易地用它玩和发送使用不同的数据不同的电子邮件不同的html模板