Send email from rails console

Ruby on-RailsSendmail

Ruby on-Rails Problem Overview


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>>

EDIT: 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.com", "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

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

Quicker version:

ActionMailer::Base.mail(
  from: "[email protected]", 
  to: "[email protected]", 
  subject: "Test", 
  body: "Test"
).deliver_now

Solution 2 - Ruby on-Rails

I ran into a similar problem this morning on a Rails 3 app where I called:

UserMailer.activation_instructions(@user)

This gave me the data but did not send the e-mail out. To send, I called:

UserMailer.activation_instructions(@user).deliver

This did the trick. Hopefully this might work for you too!

Solution 3 - Ruby on-Rails

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: '[email protected]',
  password: 'yourpassword'
}

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

UserMailer.activation_instructions(@user).deliver_now

Solution 4 - Ruby on-Rails

if you want to send attachments

mailer = ActionMailer::Base.new
mailer.attachments["file.jpg"] = File.read("/dir/file.jpg")
mailer.attachments["file.txt"] = "some text"
mailer.mail(from: "[email protected]",
            to: "[email protected]",
            subject: "Email with attachments",
            body: "included the documents below\n\n")
mailer.message.deliver

mail has to come after the attachments, because it creates the headers.

Solution 5 - Ruby on-Rails

I am not 100% if I understand what you are trying to do.

If you try to send out e-mails to the Internet, your sendmail must be configured in a way to forward those e-mails to the proper e-mails server. Depending on which Ubuntu release you use you can just reconfigure the package to do this.

You also might think if you want to use procmail instead of sendmail.

You can reconfigure the e-mail configuration with

dpkg-reconfigure sendmail

of use procmail instead if you use that package. The configuration dialogue gives you some option where you can configure it to forward all mail to the appropriate e-mail server. However, you need to think if you need authentication or if that server just accepts e-mails from your server.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMax WilliamsView Question on Stackoverflow
Solution 1 - Ruby on-RailsjmgarnierView Answer on Stackoverflow
Solution 2 - Ruby on-RailssscirrusView Answer on Stackoverflow
Solution 3 - Ruby on-RailsDhirajView Answer on Stackoverflow
Solution 4 - Ruby on-RailsYoav EpsteinView Answer on Stackoverflow
Solution 5 - Ruby on-RailstxwikingerView Answer on Stackoverflow