Ramazan Zor
Ramazan Zor

Reputation: 209

Rails send mail with gmail development mode

I didn't figure out how I send mail from gmail in development environment.It didn't send email. I didn't understand the rails guide, and also I wonder if the production env is the same ?

config/development.rb

config.action_mailer.raise_delivery_errors = true
config.action_mailer.default_url_options = { :host => 'something.com' } 
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address:              'smtp.gmail.com',
port:                 587,
domain:               'mail.google.com',
user_name:            '[email protected]',
password:             'mypassword',
authentication:       'plain',
enable_starttls_auto: true  }

mailer/user_mailer.rb

 default :from => 'something.com'

 def welcome_email(user)
   @user = user
   @url  = 'http://something.com'
   mail(to: @user.email, subject: 'Welcome')
 end

edit

where I call, in users create method,

UserMailer.welcome_email(@user).deliver_now

Upvotes: 2

Views: 610

Answers (2)

Rokibul Hasan
Rokibul Hasan

Reputation: 4156

You can call your mailer methods in two way to send email,

  1. In UsersController's create action

    def create
        #your codes and logics to save user...
        UserMailer.welcome_email(@user).deliver_now
        #your codes goes here ...  
    end  
    
  2. In User model after_create callback

     class User < ActiveRecord::Base
       after_create :send_email
    
       def send_email
          UserMailer.welcome_email(self).deliver_now
       end  
     end
    

I would prefer second one for sending email, use model callback rather in controller.

Upvotes: 0

Prakash Laxkar
Prakash Laxkar

Reputation: 854

config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true

Try this in development.rb It will either send mail or raise delivery error in console.

Upvotes: 2

Related Questions