07/09/2018, 16:14

Mail functions with Mandrill App

Mandrill là dịch vụ SMTP miễn phí được tạo ra bởi MailChimp, một nhà cung cấp dịch vụ Email Marketing nổi tiếng trên thế giới. Dịch vụ mail của Mandrill là dạng transactional email. Nó được ứng dụng rộng rãi trong việc gửi mail thông báo đến người dùng trong các dịch vụ xã hội, marketing, ...

Mandrill là dịch vụ SMTP miễn phí được tạo ra bởi MailChimp, một nhà cung cấp dịch vụ Email Marketing nổi tiếng trên thế giới.

Dịch vụ mail của Mandrill là dạng transactional email. Nó được ứng dụng rộng rãi trong việc gửi mail thông báo đến người dùng trong các dịch vụ xã hội, marketing, campaign mail,...

Trong bài viết này, mình sẽ đề cập đến việc sử dụng Mandrill App để thực hiện các chức năng liên quan đến gửi, nhận, tracking email trong Ruby on Rails.

Mandrill cung cấp API làm việc với Ruby, Python, NodeJS, PHP và JavaScript.
Với Ruby, chúng ta sẽ sử dụng gem 'mandrill-api'.
Bạn có thể thêm dòng sau vào Gemfile:

gem 'mandrill-api'

Hoặc install mandrill bằng lệnh:

gem install mandrill-api



OK, như vậy chúng ta đã có thư viện Mandrill trong project của mình, giờ thì sử dụng nó thôi.
Trước tiên, ta sẽ include library module và tạo một instance của Mandrill::API

require 'mandrill'
mandrill = Mandrill::API.new 'YOUR_MANDRILL_API_KEY'



Để lấy thông tin của user, chúng ta có thể sủ dụng method sau:

mandrill = Mandrill::API.new 'YOUR_MANDRILL_API_KEY'
user_info = mandrill.users.info



Mandrill cung cấp cho chúng ta các Messages Calls methods, được sử để thao tác với email.
Các method thường được sử dụng gồm có: send, send_template và info.

Ta sử dụng messages.send để gửi một transactional email.

mandrill = Mandrill::API.new 'YOUR_MANDRILL_API_KEY'
message = {
 from_email: "[email protected]",
 from_name: "Example Name",
 subject: "Subject",
 to: [{
   name: "Recipient Name",
   type: "to",
   email: "[email protected]"}],
 merge_vars: [{
    rcpt: "[email protected]",
    vars: [{
      name: "merge2",
      content: "merge2 content"}]
    }]
}
async = false
ip_pool = "Main Pool"
send_at = "example send_at"
result = mandrill.messages.send message, async, ip_pool, send_at



Ngoài ra, Mandrill hỗ trợ việc send email sử dụng các template được tạo sẵn. Điều này cực kỳ tiện lợi khi chúng ta gửi một lượng lớn email có nội dung cầu kỳ như gửi campaign khuyến mãi sản phẩm,...

mandrill = Mandrill::API.new 'YOUR_MANDRILL_API_KEY'
template_name = "example template_name"
template_content = [{
  content: "example content",
  name: "example name"}]
message = {
 from_email: "[email protected]",
 from_name: "Example Name",
 subject: "Subject",
 to: [{
   name: "Recipient Name",
   type: "to",
   email: "[email protected]"}],
 merge_vars: [{
    rcpt: "[email protected]",
    vars: [{
      name: "merge2",
      content: "merge2 content"}]
    }]
}
async = false
ip_pool = "Main Pool"
send_at = "example send_at"
result = mandrill.messages.send_template template_name, template_content, message, async, ip_pool, send_at

Để sử dụng template cho việc send mail, chúng ta có thể tạo template trực tiếp trên Mandrill App, ngoài ra chúng ta có thể thao tác bằng các method đã được Mandrill cung cấp trong API.
Ví dụ:

Thêm Template:

def add_mandrill_template
  from_email = "[email protected]"
  from_name = "Example Name"
  subject = "Subject"
  name = "Template Name"
  code = "<div>example code</div>"
  text = "Example text content"
  publish = true

  mandrill_client.templates.add name, from_email, from_name, subject, code, text, publish
end

Xóa template:

def delete_mandrill_template template_name
  mandrill_client.templates.delete template_name
end



Đối với Mail marketing, ngoài việc gửi và nhận email, thì việc tracking email cũng là rất quan trọng. Chúng ta sử dụng messages.info để lấy thông tin các transactional email đã được gửi đi thông qua Mandrill.

mandrill = Mandrill::API.new 'YOUR_MANDRILL_API_KEY'
id = "abc123abc123abc123abc123"
result = mandrill.messages.info id
  # {"subject"=>"example subject",
  #  "sender"=>"sender@example.com",
  #  "smtp_events"=>[{"diag"=>"250 OK", "type"=>"sent", "ts"=>1365190001}],
  #  "opens_detail"=>
  #    [{"ua"=>"Linux/Ubuntu/Chrome/Chrome 28.0.1500.53",
  #      "ts"=>1365190001,
  #      "location"=>"Georgia, US",
  #      "ip"=>"55.55.55.55"}],
  #      "clicks_detail"=>
  #    [{"ua"=>"Linux/Ubuntu/Chrome/Chrome 28.0.1500.53",
  #      "ts"=>1365190001,
  #      "location"=>"Georgia, US",
  #      "ip"=>"55.55.55.55",
  #      "url"=>"http://www.example.com"}],
  #  "template"=>"example-template",
  #  "email"=>"recipient.email@example.com",
  #  "ts"=>1365190000,
  #  "metadata"=>{"website"=>"www.example.com", "user_id"=>"123"},
  #  "clicks"=>42,
  #  "_id"=>"abc123abc123abc123abc123",
  #  "state"=>"sent",
  #  "opens"=>42,
  #  "tags"=>["password-reset"]}

Ngoài ra, chúng ta có thể xem trực tiếp các thống kê liên quan đến việc gửi, nhận và tracking email trên Mandrill App. Từ đó đưa ra các giải pháp phù hợp cho bài toán Mail Marketing của mình.

Trên đây, mình đã điểm qua một số thao tác với Mandrill trong việc thực hiện các chức năng liên quan đến Mail trong Ruby on Rails.
Các bạn có thể tìm hiểu thêm các methods khác và cách dùng trong Mandrill API docs:
https://mandrillapp.com/api/docs/

0