Send an email using only JavaScript

No server-side languages involved.

因為打造一個純JS+html的網站,網站有一個Email 功能,所以開始找方案,第一個找的是Gmail ,結果不行!! 另外又找

步驟如下:

1. register for Mandrill to get an API key
2. load jQuery
3. use $.ajax to send an emai

Step 1: Get an API key

Mandrill 上註冊. 當你完成了,就開始來取得到 API Key,按下GET API Keys

Image for post
Image for post

如果你跟我一樣第一次產生,就會看到下面,直接按 +Add API Key

Image for post
Image for post

在下一頁就可以看到你的 API Key

Image for post
Image for post

Step 2: Load jQuery

<script src="http://code.jquery.com/jquery-1.10.1.js"></script>

Step 3: Use the $.ajax function to send an email

使用時,請確認是用POST

$.ajax({
type: 'POST',
url: 'https://mandrillapp.com/api/1.0/messages/send.json',
data: {
'key': 'YOUR API KEY HERE',
'message': {
'from_email': 'kenneth_hu@livemail.tw',
'to': [
{
'email': 'RECIPIENT_1@EMAIL.HERE',
'name': 'RECIPIENT NAME (OPTIONAL)',
'type': 'to'
},
{
'email': 'RECIPIENT_2@EMAIL.HERE',
'name': 'ANOTHER RECIPIENT NAME (OPTIONAL)',
'type': 'to'
}
],
'autotext': 'true',
'subject': 'YOUR SUBJECT HERE!',
'html': 'YOUR EMAIL CONTENT HERE! YOU CAN USE HTML!'
}
}
}).done(function(response) {
console.log(response);
});

有關API相關參數,請參考下面網址

https://mandrillapp.com/api/docs/messages.html

你也可以下載我的SourceCode

https://github.com/kennethhutw/SendEmailByJS/

PS: 你的API KEY 任何人都可以使用喔!!

Written by

撰寫任何事情,O型水瓶混魔羯,咖啡愛好者,Full stack/blockchain developer,Founder of Blockchain&Dapps meetup and DeFi-Decentralized-Finance-SG meetup,Udemy teacher。

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store