ruby-on-railsemail

Send email from rails console


I'm trying to send out some mails from the console on my production server, and they're not going out. I can't work out why. I have just your standard email setup with sendmail. When I call the Mailer.deliver_ method I get this back:

#<TMail::Mail port=#<TMail::StringPort:id=0x3fe1c205dbcc> bodyport=#<TMail::StringPort:id=0x3fe1c2059e00>>

Added some more info:

So, for example, I have this line in my controller when a new user signs up, to send them a "welcome" email:

 Mailer.deliver_signup(@user, request.host_with_port, params[:user][:password])

This works fine. I thought that I should be able to do the same thing from the console, eg

user = User.find(1)
Mailer.deliver_signup(user, "mydomainname.example", "password")

When I do this, I get the Tmail::StringPort object back, but the mail appears to not get sent out (i'm trying to send emails to myself to test this).

I'm on an Ubuntu server in case that helps. thanks - max


Solution

  • For Sending email from Rails Console first we have to execute this setting in console for action mailer settings.

    ActionMailer::Base.delivery_method = :smtp 
    ActionMailer::Base.smtp_settings = {
      address: 'smtp.gmail.com', 
      port: 587, 
      domain: 'gmail.com',
      authentication: 'plain', 
      enable_starttls_auto: true, 
      user_name: 'your@gmail.com',
      password: 'yourpassword'
    }
    

    After that If we execute email sending code it will deliver email.

    UserMailer.activation_instructions(@user).deliver_now