9

I have tried all kinds of configurations but still I can't send an email in my development environment from rails.

I installed mailutils to try this from the command line and it worked, I received the email (in spam of course): echo test | mail -s Subject [email protected]

Here's my config:

# Don't care if the mailer can't send
config.action_mailer.raise_delivery_errors = true # still no logs about emails

config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true # I can't believe I have to add this option. Does it even exist? I found it on google.
config.action_mailer.smtp_settings = {
  :enable_starttls_auto => true,
  :address => "smtp.gmail.com",
  :port => 587,
  :domain => "gmail.com",
  :authentication => :login,
  :user_name => "[email protected]",
  :password => "abc123",
}

And here's the code in the mailer:

class UserMailer < ActionMailer::Base
  default :from => "root@ubuntu"

  def test_email
    Rails.logger.debug 'test_email'
    mail(:to => '[email protected]', :subject => "testing rails")
  end
end

The controller:

class PagesController < ApplicationController
  def home
    UserMailer.test_email
  end
end

development.log:

[2012-03-01 18:26:45.859] DEBUG  [bb44dee806d73eb60ab3ae16297f5c02] [127.0.0.1] [GET] [http://myapp:3000/] test_email
[2012-03-01 18:26:45.888]  INFO  [bb44dee806d73eb60ab3ae16297f5c02] [127.0.0.1] [GET] [http://myapp:3000/]   Rendered user_mailer/test_email (1.6ms)
[2012-03-01 18:26:45.898]  INFO  [bb44dee806d73eb60ab3ae16297f5c02] [127.0.0.1] [GET] [http://myapp:3000/]   Rendered pages/home.html.erb within layouts/application (1.1ms)
[2012-03-01 18:26:46.815]  INFO  [bb44dee806d73eb60ab3ae16297f5c02] [127.0.0.1] [GET] [http://myapp:3000/] Completed 200 OK in 455ms (Views: 112.4ms)

I also tried using the console:

root@ubuntu:/srv/www/myapp# rails c
Loading development environment (Rails 3.2.1)
irb(main):001:0> UserMailer.test_email
=> #<Mail::Message:32110400, Multipart: false, Headers: <To: [email protected]>, <Subject: testing rails>, <Mime-Version: 1.0>, <Content-Type: text/html>>
4
  • guides.rubyonrails.org/action_mailer_basics.html has an example for GMail configuration. It uses 'plain' for the authentication attribute, have you tried it? Commented Mar 1, 2012 at 19:06
  • yes I tried it and nothing happened. Without any logs this isn't going to work. Everything else in the request is logged correctly Commented Mar 1, 2012 at 19:13
  • What does the code that invokes the mailer look like? Commented Mar 1, 2012 at 19:34
  • @FrederickCheung I added the controller to my post Commented Mar 1, 2012 at 21:32

4 Answers 4

27
  UserMailer.test_email

Just creates a Mail::Message object. To actually send an email you need to do

  UserMailer.test_email.deliver

(or starting with rails 4.2 deliver_now / deliver_later)

Sign up to request clarification or add additional context in comments.

3 Comments

Ok this is embarrassing.. Thanks
Ow My God! I spent 3 hours, totally forgot about this method! Thanks
Note for rails 5: DEPRECATION WARNING: #deliver is deprecated and will be removed in Rails 5. Use #deliver_now
9

Regarding logging errors:

Just figured out by tinkering that a bang method counterpart exists for deliver, which throws an exception. Can be pretty useful to check if you just misspelled your username or password, or you just have some misconfigured settings.

UserMailer.test_email.deliver!

Comments

1

An updated answer for Rails 4.2 would be:

UserMailer.test_email.deliver_now!

The exclamatory mark is to raise an exception if there are any errors.

2 Comments

Exclamation also adds implied urgency :)
deliver_now! Delivers an email without checking perform_deliveries and raise_delivery_errors, so use with caution. api.rubyonrails.org/v4.2/classes/ActionMailer/…
0

If you suspect it's your settings, try taking out the :domain. It worked for me a while ago.( Net::SMTPAuthenticationError in rails 3.1.0.rc5 when connecting to gmail )

But I don't see a :body option in the mail function. Perhaps that's the issue. Try sending it from rails console and see what happens. http://api.rubyonrails.org/classes/ActionMailer/Base.html#method-i-mail

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.