ruby-on-rails – 通过localhost在ROR应用程序中向注册用户发送确认电子邮件

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – 通过localhost在ROR应用程序中向注册用户发送确认电子邮件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在我的ROR应用程序中,我正在尝试在注册时向我的注册用户发送确认电子邮件,我的网站目前在localhost上.我收到此错误
  1. "undefined method `recipients' for #<UserMailer:0x3d841a0>"

这是我的代码;

development.rb

  1. config.action_mailer.delivery_method = :smtp
  2. config.action_mailer.default_url_options = { :host => "localhost:3000"}
  3. config.action_mailer.smtp_settings = {
  4. :address => "smtp.gmail.com",:port => "587",:authentication => :login,:user_name => "myemailid@gmail.com",:password => "myrealpassword"
  5. }

Users_controller.rb

  1. def new
  2. UserMailer.registration_confirmation(@user).deliver
  3. end
  4.  
  5. def create
  6. @user = User.new(params[:user])
  7. if @user.save
  8. UserMailer.registration_confirmation(@user).deliver
  9. sign_in @user
  10. flash[:success] = "Welcome!"
  11. redirect_to @user
  12. else
  13. render 'new'
  14. end
  15. end

user_mailer.rb

  1. class UserMailer < ActionMailer::Base
  2. def registration_confirmation(user)
  3. recipients user.email
  4. from "myemailid@gmail.com"
  5. subject "Thank you for registration"
  6. body :user => user
  7. end
  8. end

解决方法

** development.rb **
  1. config.action_mailer.delivery_method = :sendmail
  2. config.action_mailer.perform_deliveries = true
  3. config.action_mailer.raise_delivery_errors = true

** Users_controller.rb **

  1. def new
  2. UserMailer.registration_confirmation(@user).deliver
  3. end
  4. def create
  5. @user = User.new(params[:user])
  6. if @user.save
  7. UserMailer.registration_confirmation(@user).deliver
  8. sign_in @user
  9. flash[:success] = "Welcome!"
  10. redirect_to @user
  11. else
  12. render 'new'
  13. end
  14. end

** User_mailer.rb **

  1. def registration_confirmation(user)
  2. @message = 'whatever you want to say here!'
  3. mail(:from => "myemailid@gmail.com",:to => user.email,:subject => "Thank you for registration")
  4. end

** / app / views / user_mailer / registration_confirmation.text.erb *

  1. <%= @message %>

这就是我在开发模式中所做的,它的工作原理

猜你在找的Ruby相关文章