มาลองทำระบบส่ง Email ด้วย NodeJS กันเถอะ 😍😍

W.Songsak
W.Songsak
Nov 8 · 2 min read

จำได้ว่ามีเพื่อนคนนึงถามว่า “เป็กเราสามารถเขียนอีเมลเซิฟเวอร์ของเราเองได้ไหม?” ตัวเองก็เลยได้ลองค้นหาข้อมูลก็พบว่า เราสามารถให้บัญชีอีเมลของเราในการส่งจาก Backend ได้ วันนี้ก็เลยจะมาเขียนไว้เผื่อเป็นประโยชน์กับผู้อ่าน

เริ่ม!!!

วิธีการก็แสนจะง่าย

  1. สร้างโปรเจค NodeJS และ
  2. ติดตั้ง package ที่ใช้ในการส่งอีเมล แค่นี้เราก็ได้เมลเซิฟเวอร์อย่างง่ายแล้ว จบ…

  • ขั้นแรกให้เราสร้างโปรเจค NodeJS ขึ้นมาด้วยคำสั่ง
mkdir nodejs-mailer // สร้างโฟลเดอร์
cd nodejs-mailer
npm init -y // สร้าง package.json เพื่อระบุว่านี้คือโปรเจค NodeJS
npm install nodemailer // ติดตั้ง package ที่ใช้ในการส่งอีเมล
  • ขั้นตอนถัดมา คือ เราจะทำการเปิด Less secure app access (จิ้ม -> ตรงนี้) เพื่อให้เราสามารถเข้าสู่ระบบด้วย Gmail ได้จาก Backend ได้
  • เริ่มการเขียนโค๊ดกัน(ใช้คำว่าก๊อปวางจะถูกมากกว่านะ 555+) เริ่มจากการสร้างไฟล์ในโปรเจคของเราในที่นี้ผมจะใช้ชื่อไฟล์ว่า index.js หลังจากนั้นให้ทุกคนเอาโค๊ดข้างไปแปะไว้ในไฟล์ได้เลยครับ
'use strict';const nodemailer = require('nodemailer');// async..await is not allowed in global scope, must use a wrapperasync function main() {// สร้างออปเจ็ค transporter เพื่อกำหนดการเชื่อมต่อ SMTP และใช้ตอนส่งเมลlet transporter = nodemailer.createTransport({ host: 'smtp.gmail.com', port: 587, secure: false, // true for 465, false for other ports auth: { // ข้อมูลการเข้าสู่ระบบ   user: 'xxxxx@gmail.com', // email user ของเรา   pass: 'xxxx' // email password }});// เริ่มทำการส่งอีเมลlet info = await transporter.sendMail({from: '"Fred Foo 👻" <xxxx@gmail.com>', // อีเมลผู้ส่งto: 'yyyyy@gmail.com', // อีเมลผู้รับ สามารถกำหนดได้มากกว่า 1 อีเมล โดยขั้นด้วย ,(Comma)subject: 'Hello ✔', // หัวข้ออีเมลtext: 'Hello world?', // plain text bodyhtml: '<b>Hello world?</b>' // html body});// log ข้อมูลการส่งว่าส่งได้-ไม่ได้console.log('Message sent: %s', info.messageId);}main().catch(console.error);

หลังจากนั้นรันโค๊ดด้วยคำสั่ง node index.js จะได้ผลการรันตามรูป

ลองเข้าไปเช็คที่อีเมล 📧📩

ตัวอย่าง Fields ที่สามารถให้งานได้ 🕵️‍♀️

  • from อีเมลของคนส่ง
  • to อีเมลของผู้รับ ใส่ได้หลายคน โดยคั่นด้วย , (Comma)
  • cc อีเมลของผู้รับที่จะแสดงใน Cc:ใส่ได้หลายคน โดยคั่นด้วย , (Comma) โดย Cc ต้องการเพียงให้ทราบเรื่องเท่านั้น ไม่ได้มีความตั้งใจจะส่งให้เราโดยตรง
  • bcc อีเมลของผู้รับที่จะแสดงใน Bcc:ใส่ได้หลายคน โดยคั่นด้วย , (Comma) โดย Bcc เป็นการส่งสำเนาลับ คนที่ได้รับเมลจะเห็นว่าเราส่งไปหาเขาคนเดียว แต่อันที่จริงเราส่งไปหาหลายคน
  • subject หัวเรื่องของอีเมล
  • text เพลนแท็ก รองรับการใส่ emoji ที่เป็น unicode
  • html สามารถเขียนด้วย html ได้ แต่ข้อควรระวังคือ รองรับเฉพาะ inline style
  • attachments ไฟล์แนบ ดูตัวอย่างการใช้งานด้านล่างครับ
{   
// file on disk as an attachment
filename: 'text3.txt',
path: '/path/to/file.txt' // stream this file
},
{
// use URL as an attachment
filename: 'license.txt',
path: 'https://raw.github.com/nodemailer/nodemailer/master/LICENSE' },
{
// encoded string as an attachment
filename: 'text1.txt',
content: 'aGVsbG8gd29ybGQh',
encoding: 'base64'
},

ประมาณนี้ครับ เอาคร่าวๆ เชื่อว่าทุกคนน่าจะเอาไปต่อยอดได้เนาะ 😍🥰😘


สามารถอ่าน Document เพิ่มเติมได้ที่ https://nodemailer.com/about/ ครับ

W.Songsak

Written by

W.Songsak

สายเลือดสีเขียวบริสุทธิ์ Android, Vue, NodeJs

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade