send mail in rails

Creating and Sending Mail


1.CREATE A Notice MAILER

rails g mailer NoticeMailer decomp_change

create   app/mailers/notice_mailer.rb

invoke   erb

create   app/views/notice_mailer

create   app/views/notice_mailer/decomp_change.text.erb


app/mailers/notice_mailer.rb

class NoticeMailer < ActionMailer::Base
  default from: "[email protected]"

  def decomp_change
    @greeting = "Hi"
    mail to: "[email protected]"
  end
end
 

Sending Attachments in Mail

class NoticeMailer < ActionMailer::Base
  default from: "[email protected]"

  def decomp_change (notice)
    @notice = notice
    @last_tweet = @notice.tweets.last
    attachments['z.pdf'] = File.read("#{Rails.root}/public/notice.pdf")
    mail to: @notice.email, subject: 'Your decomp stage has changed'
  end
end
 

2.MAILER VIEWS

app/views/notice_mailer/decomp_change.text.erb

Greetings <%= @notice.name %>,

  Your decomposition state is now <%= @notice.decomp %> and your
last tweet was: <%= @last_tweet.body %>

Good luck!
 

app/views/notice_mailer/decomp_change.html.erb

<h1>Greetings <%= @notice.name %>,</h1>

<p>Your decomposition state is now <%= @notice.decomp %> and your
last tweet was: <%= @last_tweet.body %></p>

<%= link_to "View yourself", notice_url(@notice) %>


3.SENDING MAIL

app/models/notice.rb

class Notice < ActiveRecord::Base
  after_save :decomp_change_notification, if: :decomp_changed?

  private
  def decomp_change_notification
    noticeMailer.decomp_change(self).deliver
  end
end
 

4.config

config/environments/development.rb

config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
    :address              => "smtp.gmail.com",
    :port                 => 587,
    :user_name            => 'poshboytl',
    :password             =>  ENV['GMAIL_PASS'],
    :authentication       => 'plain',
    :enable_starttls_auto => true
  }
 

reference:

http://railscasts-china.com/episodes/how-to-send-emails-in-rails

你可能感兴趣的:(Rails)