简体   繁体   中英

Why isn't rails action mailer working properly? The page isn't rendering any errors

I'm trying to use action mailer to send a simple email to a user whenever they sign up, but action mailer doesn't seem to be sending any emails at all. The funny thing is that the redirect code works and the page renders properly on the local host, but on heroku it says "Sorry something went wrong".

Here's what I'm doing in my users controller:

if @user.save    
     UserMailer.welcome_email(@user).deliver
     flash[:success] = "Congratulations, you've created your band app account!"
     redirect_to root_path
else
  render 'new'
end

The welcome_email method referred to in the controller:

class UserMailer < ActionMailer::Base
  default from: "admin@something.com"

  def welcome_email(user)
    @user = user
    @url = signin_path
    mail(to: user.email, subject: "Your band app account has been created" )
  end
end

and finally the code for the email view (welcome_email.html.erb in app/views/user_mailer:

<!DOCTYPE html>
<html>
  <body>
    <h1>Welcome to the band app, <%= @user.name %></h1>
    <p>
      Your band app account has been created.<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>

Thank you in advance for any help!

Did you set up your environment file?

As it says here http://guides.rubyonrails.org/action_mailer_basics.html#action-mailer-configuration-for-gmail , you should set your development.rb or the file related to the ENV you are working in with the following settings:

config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
  :address              => "smtp.gmail.com",
  :port                 => 587,
  :domain               => 'baci.lindsaar.net',
  :user_name            => '<username>',
  :password             => '<password>',
  :authentication       => 'plain',
  :enable_starttls_auto => true  }

I know that the link says it's an example for Gmail, but you have to set up those configurations on your ENV file, like the email you are using( :user_name ), the password, etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM