Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 417
Manspider Avatar asked Dec 19 '22 06:12

Manspider


2 Answers

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

config.action_mailer.default_options = { from: '[email protected]' }
like image 68
MonR Avatar answered Dec 29 '22 02:12

MonR


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

like image 40
Clemens Kofler Avatar answered Dec 29 '22 02:12

Clemens Kofler