Rails 보전 의 제6 1 식:Sending Email

1876 단어 FlashRubyRails
이것 은 Rails 로 이메일 을 보 내 는 간단 한 지침 입 니 다.
1,config/environments/development.rb 수정,smtp 설정

config.action_mailer.raise_delivery_errors = true

# set delivery method to :smtp, :sendmail or :test
config.action_mailer.delivery_method = :smtp

# these options are only needed if you choose smtp delivery
config.action_mailer.smtp_settings = {
  :address           => 'smtp.example.com',
  :port              => 25,
  :domain            => 'www.example.com',
  :authentication    => :login,
  :user_name         => 'www',
  :password          => 'secret'
}

production 환경 에서 위 설정 을 environment.rb 로 옮 깁 니 다.
2,mailer 생 성

ruby script/generate mailer user_mailer

3,UserMailer 수정

class UserMailer < ActionMailer::Base
  def registration_confirmation(user)
    recipients  user.email
    from        "[email protected]"
    subject     "Thank you for Registering"
    body        :user => user
  end
end

4,수정 이메일 템 플 릿 views/usermailer/registration_confirmation.rhtml

Hi <%= @user.name %>,
...

5,Users Controller 에서 UserMailer 사용

class UsersController < ApplicationController
  def create
    @user = User.new(params[:user])
    if @user.save
      UserMailer.deliver_registration_confirmation(@user)
      flash[:notice] = "Successfully registered"
      redirect_to user_path(@user)
    else
      render :action => 'new'
    end
  end

# ...
end

좋은 웹페이지 즐겨찾기