Manspider
Manspider

Reputation: 353

Rails devise with gmail errors

I'm currently working on an application with a mailer system. It was working fine, sending a welcome email and sending instructions to reset password, but now and only when I try to send reset instructions I have this error.

ArgumentError (SMTP From address may not be blank: nil):

I'm using a custom domain like so [email protected] And here is my configuration

development.rb

 config.action_mailer.raise_delivery_errors = true
 config.action_mailer.perform_caching = false
 config.action_mailer.default_url_options = { host: 'localhost:3000' }

 config.action_mailer.delivery_method = :smtp
 config.action_mailer.smtp_settings = {
     address: 'smtp.gmail.com',
     port: '587',
     domain: 'gmail.com',
     authentication: :plain,
     enable_starttls_auto: true,
     user_name: Rails.application.secrets.mailer_username,
     password: Rails.application.secrets.mailer_password
}

Any idea ?

Edit

class UserMailer < ApplicationMailer
  default from: '[email protected]'

  def welcome_email(user)
    @user = user
    @url = 'http://localhost:3000/users/sign_in'
    mail(to: @user.email, subject: 'Bienvenue')
  end

  def generate_new_password_email
    user = User.find(params[:user_id])
    user.send_reset_password_instructions
  end

  def reset_password; end
end

Upvotes: 5

Views: 6684

Answers (5)

gsumk
gsumk

Reputation: 891

I stumbled upon the same problem. My solution was, I edited config/initializers/devise.rb changed a line from config.mailer_sender = '[email protected]' to config.mailer_sender = ENV["default_from_email"]

Upvotes: 0

MonR
MonR

Reputation: 121

You could try setting :from in your config, using the default_option like this,

config.action_mailer.default_options = { from: '[email protected]' }

Upvotes: 7

Somesh Sharma
Somesh Sharma

Reputation: 569

I had the same problem, and the reason behind that was i working in development but my mailer was searching the smtp_settings in the production, so to solve the issue you can change the mailer settings or you can copy the same smtp_settings into production.

Upvotes: 1

Manspider
Manspider

Reputation: 353

In devise.rb config.mailer_sender = '[email protected]' had been commented. Config.mailer_sender was never initialized and so always nil even if I set it with default from:

Upvotes: 2

Clemens Kofler
Clemens Kofler

Reputation: 1968

It looks like you don't have a From header in your email. A good practice would be to put the following line into your ApplicationMailer:

class ApplicationMailer
  default from: '[email protected]'

  # ...
end

To override this in your inheriting mailers, simply declare the same statement. To override it in individual mail methods, put it into the mail call like so:

def new_message(user, message)
  mail(
    to: user.email,
    subject: "New message from #{message.sender.name}",
    from: message.sender.email
  )
end

Hope that helps

Upvotes: 2

Related Questions