Rails使用Mailgun發郵件的幾個方式

1.使用Rails內建的Action Mailer

因為Rails有內建Action Mailer,所以這個方法比較直接,只要更改Action Mailer的smtp設定

# enviroments/production.rb 或是 enviroments/development.rb 加上

ActionMailer::Base.smtp_settings = 
  { :port => 587, 
    :address => 'smtp.mailgun.org',  
    :user_name => 'postmaster@your.mailgun.domain', 
    :password => 'mailgun-smtp-password', 
    :domain => 'your.mailgun.domain', 
    :authentication => :plain }

ActionMailer::Base.delivery_method = :smtp 

再來新增 mailers/user_mailer.rb

# mailers/user_mailer.rb

class UserMailer < ActionMailer::Base 
  default from: " service@eliving.co"
  def confirm(email, params)
    @customer_name = params[:customer_name]
    @customer_phone = params[:customer_phone]
    @customer_email = params[:customer_email]
    @content = params[:content]
    @website_name = params[:website_name]

    mail to: email, subject: "ELIVING 客戶留言通知信"
  end
end 

再來在views/user_mailer/confirm.text.erb編輯郵件的template

假設為:

HI <%= @website_name %> 您好,有客戶在您的網站留言

相關資訊如下:

* * *

客戶名稱 / <%= @customer_name %>

聯絡電話 / <%= @customer_phone %>

電子郵件 / <%= @customer_email %>

服務內容 / <%= @content %>

* * *

如此要發送郵件時只要在controller裡面呼叫:

UserMailer.confirm(@website.email, inquiry_params).deliver

2.使用mail gem

這個也是使用smtp的方法,只是不使用Rails內建的Action Mailer

首先Gemgile加上 gem ‘mail’,然後bundle安裝

在 enviroments/production.rb 或是 enviroments/development.rb 加上

# enviroments/production.rb 或 enviroments/development.rb
Mail.defaults do
  delivery_method :smtp, {
    :port      => 587,
    :address   => "smtp.mailgun.com",
    :user_name => "",
    :password  => "",
  }
end

在controller呼叫以下指令,就可以發送mail了

mail = Mail.deliver do to 'bar@example.com' from 'foo@samples.mailgun.org' subject 'Hello'
  text_part do
    body 'Testing some Mailgun awesomness'
  end
end

3.使用mailgun gem

這個是使用mailgun的gem去直接呼叫mailgun的api

首先Gemgile加上 gem ‘mailgun’,然後bundle安裝

在 enviroments/production.rb 或是 enviroments/development.rb 加上

# enviroments/production.rb 或 enviroments/development.rb
# Initialize your Mailgun object:
Mailgun.configure do |config|
  config.api_key = 'your-api-key'
  config.domain  = 'your-mailgun-domain'
end

在controller呼叫以下指令,就可以發送mail了

@mailgun = Mailgun()

parameters = 
{ :to => "cooldev@your.mailgun.domain", 
  :subject => "missing tps reports", 
  :text => "yeah, we're gonna need you to come in on friday...yeah.", 
  :from => "lumberg.bill@initech.mailgun.domain" }

@mailgun.messages.send_email(parameters)

4.使用rest-client gem

這個是使用rest-client去呼叫mailgun api

首先Gemgile加上 gem ‘rest-client’,然後bundle安裝

然後在要發送的controller執行以下指令

RestClient.post 
"https://api:key-3ax6xnjp29jd6fds4gc373sgvjxteol0"
"@api.mailgun.net/v2/samples.mailgun.org/messages",
:from => "Excited User <me@samples.mailgun.org>",
:to => "bar@example.com, baz@example.com",
:subject => "Hello",
:text => "Testing some Mailgun awesomness!"

只是要記得將 api:後面的key換成自己的 還有將samples.mailgun.org換成自己的domain

參考資料:

https://github.com/HashNuke/mailgun http://documentation.mailgun.com/quickstart-sending.html

Leave a Reply

Your email address will not be published. Required fields are marked *

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax