Sending confirmation emails to registered users in ROR app via localhost

天涯浪子 提交于 2019-11-28 06:12:16

问题


In my ROR app, I am trying to send confirmation emails to my registered users when they signup, my website is on localhost currently. I am getting this error:

"undefined method `recipients' for #<UserMailer:0x3d841a0>"

Here is my code;

development.rb

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

Users_controller.rb

def new
  UserMailer.registration_confirmation(@user).deliver
end

def create
  @user = User.new(params[:user])
  if @user.save
    UserMailer.registration_confirmation(@user).deliver
    sign_in @user
    flash[:success] = "Welcome!"
    redirect_to @user
  else
    render 'new'
  end
end

user_mailer.rb

class UserMailer < ActionMailer::Base   
  def registration_confirmation(user)
    recipients   user.email
    from         "myemailid@gmail.com"
    subject      "Thank you for registration"
    body         :user => user  
  end
end

回答1:


** development.rb**

config.action_mailer.delivery_method = :sendmail
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true

** Users_controller.rb **

def new
  UserMailer.registration_confirmation(@user).deliver
end
def create
  @user = User.new(params[:user])
  if @user.save
    UserMailer.registration_confirmation(@user).deliver
    sign_in @user
    flash[:success] = "Welcome!"
    redirect_to @user
  else
    render 'new'
  end
end

** User_mailer.rb **

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

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

<%= @message %>

That's what I've done in my development mode, and it works




回答2:


What version of Rails are you using?

Sending of email changed in version 3.2 (I believe)

http://api.rubyonrails.org/classes/ActionMailer/Base.html

Try:

UserMailer.registration_confirmation(@user).deliver




回答3:


This is your user_mailer.rb

user_mailer.rb

class UserMailer < ActionMailer::Base
  def registration_confirmation(user)
    recipients   user.email
    from         "myemailid@gmail.com"
    subject      "Thank you for registration"
    body         :user => user  
  end

Try instead:

class UserMailer < ActionMailer::Base
  default from: "myemailid@gmail.com"

  def registration_confirmation(user)
    mail(to: user.email, subject: "Thank you for registration")
  end
end

And set up the appropriate view in views/user_mailer e.g. registration_confirmation.html.erb



来源:https://stackoverflow.com/questions/10991932/sending-confirmation-emails-to-registered-users-in-ror-app-via-localhost

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!