2016年4月5日 星期二

Cloud9-RoR-Action Mailer

參考文章:https://ihower.tw/rails4/actionmailer.html
參考文章:http://rails.ruby.tw/action_mailer_basics.html
輔助文章:http://rails.ruby.tw/testing.html#testing-your-mailers
輔助文章:http://stackoverflow.com/questions/26224080/cloud9-with-ruby-on-rails-doesnt-send-emails

當現有管理者新增一位管理者時,我希望對方能收到通知信,所以本篇要介紹怎麼建立一個mailer。

*Mailer 和 Controller 非常類似。方法都叫做“動作”,用 View 來組織信件內容。但 Controller 是產生 HTML 回給客戶端;然而 Mailer 則是建立透過 email 寄出的信件(message)。

在指令處下
$ bin/rails generate mailer UserMailer

編輯app/mailers/application_mailer.rb,我打算使用gmail系統,所以本篇都會使用gmail當範例(xxx請改成自己的gmail帳號)
class ApplicationMailer < ActionMailer::Base
  default from: "xxx@gmail.com"
  layout 'mailer'
end

編輯app/mailers/user_mailer.rb(myweb.com記得改成自己的網址)
class UserMailer < ApplicationMailer
  default :from => "xxx@gmail.com"
  def admin_create(user)
    @url = "https://myweb.com/"
    @user = user
    mail( to: @user.email, subject: "Welcome to myweb")
  end
end

新增信件內容為html格式的檔案,admin_email.html.erb在app/views/user_mailer/底下
<!DOCTYPE html>
<html>
  <head>
    <meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
  </head>
  <body>
    <h1>Welcome to myweb.com, <%= @user.name %></h1>
    <p>
      You have successfully signed up to myweb.com,
      your username is: <%= @user.login %>.<br>
    </p>
    <p>
      To login to the site, just follow this link: <%= @url %>.
    </p>
    <p>Thanks for joining and have a great day!</p>
  </body>
</html>

在同個目錄下再新增一個文字格式的檔案admin_email.text.erb
Welcome to myweb.com, <%= @user.name %>
===============================================
 
You have successfully signed up to myweb.com
your username is: <%= @user.login %>.
 
To login to the site, just follow this link: <%= @url %>.
 
Thanks for joining and have a great day!

執行指令建立一個users_controller.rb
$ rails g controller users

開啟並編輯users_controller.rb
class UsersController < ApplicationController
  def create
    @user = User.new(params[:user])
 
    respond_to do |format|
      if @user.save
        # Tell the UserMailer to send a welcome email after save
        UserMailer.welcome_email(@user).deliver_later
 
        format.html { redirect_to(@user, notice: 'User was successfully created.') }
        format.json { render json: @user, status: :created, location: @user }
      else
        format.html { render action: 'new' }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end
end

增加gmail smtp的設定,開啟config/environments/development.rb檔並編輯
  # Mail SMTP for gmail
  config.action_mailer.delivery_method = :smtp
  config.action_mailer.default_url_options = { host: "https://myweb.com" }
  config.action_mailer.smtp_settings = config_for(:email).symbolize_keys

在config下建立一個新檔名為email.yml
development:
  address: 'smtp.gmail.com'
  port: '2587'
  domain: 'gmail.com'
  user_name: 'xxx@gmail.com'
  password: '<gmailpassword>'
  authentication: 'plain'
  enable_starttls_auto: true
  
production:
  address: 'smtp.gmail.com'
  port: '2587'
  domain: 'gmail.com'
  user_name: 'xxx@gmail.com'
  password: '<gmailpassword>'
  authentication: 'plain'
  enable_starttls_auto: true
  
test:
  address: 'smtp.gmail.com'
  port: '2587'
  domain: 'gmail.com'
  user_name: 'xxx@gmail.com'
  password: '<gmailpassword>'
  authentication: 'plain'
  enable_starttls_auto: true

試寄了一個信箱後會看到以下訊息
Sent mail to xxx@gmail.com (30004.4ms)

不過實際上並沒有收到任何mail...這問題卡了我一、兩個星期了,還是找不到正確的解決辦法,所以決定先到此為止,繼續下一個部份。

沒有留言:

張貼留言