Rails 4 controller won't call my actionmailer class

In my rails 4.2.5 (ruby 2.2.1) app I have an actionmailer class that is so simple. It doesn't even send mail, just does a printf:

class UserTommail < ActionMailer::Base

  def joe
  printf("\n***** In Emails.joe")
  end

end

But when my controller calls this function, it never does the printf!

def contact_us
  printf("\n***** TOMS EMAILLER")
  UserTommail.joe()
  redirect_to(root_path(), :notice =>  "Your Contact Us message has been successfully sent.")
  printf("\n**** TOMS END")
end

The two printfs in the controller actually print their message, but the one in joe() never does. No errors or anything.

If I sabotage joe() to say joe() in the file user_tommail.rb, I get an error that the function can't be found, so I know the controller knows about it.

What am I doing wrong?

Answers


For Rails 4 to send a an email

UserTommail.joe.deliver_now

or

UserTommail.joe.deliver_later #Enqueues the email to be delivered through Active Job. When the job runs it will send the email using deliver_now.

For Rails 3 to send a an email

UserTommail.joe.deliver

You should call deliver_now to fire the mailer to send the email:

UserTommail.joe.deliver_now

See the full list of available methods in docs.


The problem here is that ActionMailer tries to be "clever" about what it does, and won't actually call your mailer method until its return value is needed.

Your mailer method joe will return an ActionMailer::MessageDelivery object, which wraps a Mail::Message object (even though you haven't specifically said you want to send an email). The Mail::Message gets lazily evaluated, meaning it won't be instantiated (and your method won't be called) until it's needed.

One way of forcing the evaluation would be to try and send the returned email with deliver_now or deliver_later, but another way would simply be to inspect the message.

if you had my_email = UserTommail.joe() and then called my_email.message, it would force the method to be run and you would see your printf in the console.


Need Your Help

get gps location from mobile phone

jquery google-maps gps latitude-longitude

I would like to know What is the best way to get the gps locations off multiple phones on to a googlemaps on a website. I just downloaded googlemaps with latitude on my phone.

vertical text in d3 (not rotated)

svg d3.js vertical-text

I am trying to get text to display vertically in svg using d3. I do not want to rotate it, however: I want the letters to remain horizontal, but one above the other. Setting writing-mode=tb does no...