Meteor 是一個(gè)可以讓初學(xué)者也能用非常短的時(shí)間構建高質(zhì)量 WebApps 的開(kāi)源代碼框架,推出不久即獲得千萬(wàn)美元融資,詳細說(shuō)明可見(jiàn) 36Kr 之前的 Meteor 報道;Mailgun 是一個(gè)簡(jiǎn)便高效的郵件發(fā)送云服務(wù),和 MailChimp 類(lèi)似,最近 Meteor 新版本集成了 Mailgun 的郵件服務(wù),無(wú)需任何配置即可實(shí)現發(fā)送郵件功能。
下面就是五分鐘教程,Meteor 巴黎興趣組成員 Vianney Lecroart,最早發(fā)布在他的博客上。教程實(shí)現的功能是,用戶(hù)訪(fǎng)問(wèn)你的應用地址,輸入電子郵件,點(diǎn)擊訂閱按鈕,然后就可以收到一封訂閱成功的歡迎郵件。
以下操縱在 Mac 電腦下進(jìn)行,Linux 環(huán)境相似。Windows 下可能需要使用 SSH 隧道連接 *UNIX 環(huán)境或使用虛擬機。我們開(kāi)始吧!
1)安裝 Meteor 開(kāi)發(fā)環(huán)境(正常網(wǎng)絡(luò )一分鐘左右安裝完畢):
$ curl https://install.meteor.com | sh
(已使用過(guò) Meteor 可升級新版或者跳過(guò),提示權限問(wèn)題可將命令末尾 sh 替換為 sudo su)
2)創(chuàng )建 Meteor 應用(kryptonemail換成你你想要的應用名稱(chēng)):
$ meteor create kryptonemail
3)進(jìn)入 Meteor 應用文件夾,添加 email 服務(wù):
cd meteoremail$ meteor add email
接下來(lái)有兩步驟,要修改 App 的 HTML 模板和 JS 文件,App 應用文件夾下默認創(chuàng )建了這兩文件,如我的是:kryptonemail.html 和 kryptonemail.js 。

HTML 修改:
<body> {{> email}}</body><template name="email"> {{<p>Email sent! Thank you for your interest in us.</p> {{else}} <label for="email">Enter your email address to receive news about us</label><br/> <input type="text" id="email" size="45" /> <input type="button" id="btn" value="Submit" /> {{/if}}</template> Javascrips 文件:
if(Meteor.isClient) { // on the client, we just have to track the DOM click event on the input. Template.email.events({ ‘click ">#if done}} <p>Email sent! Thank you for your interest in us.</p> {{else}} <label for="email">Enter your email address to receive news about us</label><br/> <input type="text" id="email" size="45" /> <input type="button" id="btn" value="Submit" /> {{/if}}</template> Javascrips 文件:
if(Meteor.isClient) { // on the client, we just have to track the DOM click event on the input. Template.email.events({ ‘click #btn‘: function () { // if someone click on the button ( tag), then we ask the server to execute the function sendEmail (RPC call) Meteor.call(‘sendEmail‘, $(‘#email‘).val()); Session.set(‘done‘, true); } }); Template.email.done = function () { return Session.equals(‘done‘, true); }}if(Meteor.isServer) { // on the server, we create the sendEmail RPC function Meteor.methods({ sendEmail: function(email) { // send the email! Email.send({to:email, from:‘a(chǎn)cemtp@gmail.com‘, subject:‘Thank you for signing up for our project‘, text:‘We will share with you some news about us in a near future. See you soon!‘}); } });}
以上,程序準備過(guò)程完畢~ 我自己感覺(jué)只用了 3 分鐘不到。最后就是程序的測試和部署了:
本地測試:
$ meteor
訪(fǎng)問(wèn) http://localhost:3000 查看運行結果,正常情況你將看到一個(gè)輸入框,輸入郵件地址確認,回到終端看是否有如下輸出:

測試正常,可以開(kāi)始部署到網(wǎng)絡(luò )上:
$ meteor deploy kryptonemail
部署之后,從終端輸出可見(jiàn) App 的訪(fǎng)問(wèn)地址類(lèi)似:
訪(fǎng)問(wèn)頁(yè)面,輸入地址,獲得郵件通知:

(上次教程的演示地址可以可以順便看一下: http://36kr.meteor.com/ )
好了,5 分鐘教程完畢,一個(gè) MVP 產(chǎn)品就這樣新鮮出爐了。之所以這么快,當然離不開(kāi) Meteor 和 Mailgun 構建好的開(kāi)發(fā)環(huán)境。更多資源見(jiàn)文末來(lái)源鏈接。
[消息來(lái)源:blog.mailgun.net, blog.mailgun.net]
聯(lián)系客服