Send email from rails console
Asked Answered
K

5

79

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

Kilroy answered 16/7, 2010 at 16:58 Comment(2)
Do you try to send e-mail to the Internet?Recently
That's pretty standard for a return value for a deliver method, there's probably something wrong with your sendmail or destination email.Proprietress
T
42

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
Torrent answered 18/11, 2014 at 12:22 Comment(0)
S
162

Quicker version:

ActionMailer::Base.mail(
  from: "[email protected]",
  to: "[email protected]",
  subject: "Test",
  body: "Test"
).deliver_now
Shortcut answered 20/10, 2015 at 13:16 Comment(1)
how would I use an existing template though? like a UserMailer.account_activation email..Excogitate
F
49

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!

Fate answered 25/10, 2011 at 16:38 Comment(1)
doesn't seem to work for me, do you have to explicit set the rails env so it picks up the right actiomailer environment config?Montelongo
T
42

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
Torrent answered 18/11, 2014 at 12:22 Comment(0)
L
5

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.

Lollard answered 24/2, 2021 at 17:12 Comment(2)
I had to force the private method #new with send to make it work.Priorate
@Priorate In Rails 7.1 it's not private: >> ActionMailer::Base.new => #<ActionMailer::Base:0x000000000137e0>Sarraute
R
-1

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.

Recently answered 16/7, 2010 at 17:5 Comment(1)
hi - sorry, i probably should have supplied some more details. I've edited my original post.Kilroy

© 2022 - 2024 — McMap. All rights reserved.