NodeJS Basic tut 13:Gửi mail trong Node.js

NodeJS Basic tut 13:Gửi mail trong Node.js

Nguồn : toidicode.com Mail là một phần khá là quan trọng trong các trang Web, và hầu như ngôn ngữ […]

Learn more »

Nguồn : toidicode.com

Mail là một phần khá là quan trọng trong các trang Web, và hầu như ngôn ngữ nào nó cũng hỗ trợ chúng ta gửi mail. Và bài hôm nay mình sẽ giới thiệu với mọi người cách gửi mail trong Node.js.Mục Lục

1, Chuẩn bị.

Đầu tiên mọi người cần tạo cho mình một thư mục mới và trong thư mục này chứa một file main.js. Sau đó mọi người sử dụng cmd cd đến thư mục dự án mà mọi người vừa tạo và chạy lệnh:

npm install nodemailer

Lệnh trên là để cài đặt module nodemailer, chúng ta sẽ sử dụng module này để gửi mail trong Node.js.

2, Cấu hình và gửi email.

Sau khi đã cài đặt xong module nodemailer mọi người mở file main.js lên và thực hiện require modole.

// main.js var nodemailer = require('nodemailer'); 

Sau đó để có thể gửi email bạn cần cấu hình 1 số thông số theo cú pháp như sau:

// main.js var nodemailer = require('nodemailer');  const option = {     service: 'gmail',     auth: {         user: '[email protected]', // email hoặc username         pass: '*******' // password     } }; var transporter = nodemailer.createTransport(option); 

Và nếu như bạn muốn kiểm tra xem chương trình đã kết nối thành công đến email chưa thì sử dụng cú pháp sau:

transporter.verify(function(error, success) {     // Nếu có lỗi.     if (error) {         console.log(error);     } else { //Nếu thành công.         console.log('Kết nối thành công!');     } }); 

Nếu như đã kết nối thành công rồi thì chúng ta sẽ tiếp tục chuyển sang bước cấu hình email nhận thư.

var mail = {     from: '[email protected]', // Địa chỉ email của người gửi     to: '[email protected]', // Địa chỉ email của người gửi     subject: 'Thư được gửi bằng Node.js', // Tiêu đề mail     text: 'Toidicode.com', // Nội dung mail dạng text     html: '<h1>Toidicode.com</h1>' // Nội dung mail dạng html }; //Tiến hành gửi email transporter.sendMail(mail, function(error, info) {     if (error) { // nếu có lỗi         console.log(error);     } else { //nếu thành công         console.log('Email sent: ' + info.response);     } });

Bên cạnh đó bạn cũng có thể cấu hình thêm các thông số sau vào email nhận thư.

Field Mô Tả
sender Email sẽ xuất hiện trong sender
replyTo Email sẽ nhận thư trả lời.
encoding  Dạng encode text/html. Mặc định là utf-8.
attachments  Object chứa các file đính kèm (xem thêm).
…. xem thêm

Và nếu như bạn muốn gửi thư cho nhiều email một lúc, thì ở field to bạn chỉ cần ngăn các giữa các email bằng dấu ,.

VD:

 
var mail = {     from: '[email protected]', // Địa chỉ email của người gửi     to: '[email protected], [email protected]', // Địa chỉ email của người gửi     subject: 'Thư được gửi bằng Node.js', // Tiêu đề mail     text: 'Toidicode.com', // Nội dung mail dạng text     html: '<h1>Toidicode.com</h1>' // Nội dung mail dạng html }; 

3, Full source code.

Và đây là full source của bài này.

 
// main.js var nodemailer = require('nodemailer');  const option = {     service: 'gmail',     auth: {         user: '[email protected]', // email hoặc username         pass: '******' // password     } }; var transporter = nodemailer.createTransport(option);  transporter.verify(function(error, success) {     // Nếu có lỗi.     if (error) {         console.log(error);     } else { //Nếu thành công.         console.log('Kết nối thành công!');         var mail = {             from: '[email protected]', // Địa chỉ email của người gửi             to: '[email protected], [email protected]', // Địa chỉ email của người gửi             subject: 'Thư được gửi bằng Node.js', // Tiêu đề mail             text: 'Toidicode.com học lập trình online miễn phí', // Nội dung mail dạng text         };         //Tiến hành gửi email         transporter.sendMail(mail, function(error, info) {             if (error) { // nếu có lỗi                 console.log(error);             } else { //nếu thành công                 console.log('Email sent: ' + info.response);             }         });     } }); 

4, Lời kết.

Phần này mình chỉ giới thiệu qua với mọi người về cách gửi email trong Node.js thôi còn nếu như bạn nào muốn tìm hiểu thêm về các driver trong đó thì có thể tham khảo thêm tại đây.



Post Views:
23


Lượt xem : 268

Integrations
Users

Share Profile

Anyone at KeenThemes can view
Anyone with link can edit

Give Award

Anyone at KeenThemes can view
Anyone with link can edit

Report User

Let us know why you’re reporing this person
Don't worry, your report is completely anonymous; the person you're
reporting will not be informed that you've submitted it