Dan Barron
Dan Barron

Reputation: 1104

Sending mail from Rails works at console but not in my application...?

I have a pretty simple Rails app and I want send an e-mail when a work request is created. I'm pretty new to Rails so I found some details on how to do it online. Set up a Mailer, configured it, etc. Set up my templates. Fine.

In my work_requests_controller.rb I have:

  def create
    @work_request = WorkRequest.new(params[:work_request])

    respond_to do |format|
      if @work_request.save
        # Tell the mailer to send a welcome Email after save
        PersonMailer.work_request_init_email(@work_request).deliver

        format.html... etc.

In know mailing is working because if I go to the Rails console, create WorkRequest object, and use that exact same line (PersonMailer.work...) it sends the mail just fine. But when I create a work request in my application no mail is received, no error is displayed in the browser or in logs/development.log.

In the server output I see the HTML version was rendered, and I see the info about the e-mail and it all looks both hunky and dory. Since I get no errors I'm at a loss as to how to proceed.

Upvotes: 4

Views: 2786

Answers (2)

Dan Barron
Dan Barron

Reputation: 1104

OK, I am officially an idiot. :-)

Working on a different problem, I was editing application.rb. I figured I needed to restart the server to make it see those changes. Suddenly, the e-mails work from inside the app.

D'oh! I did not realize (rookie mistake) that I needed to restart the server for the app to see the e-mail configuration I had placed in environment.rb yesterday. I never tried that for some reason.

I see now that and other components are run only when the server starts up. So of course, when I started up console of course it runs all the initializers and so the e-mail configuration was visible to it and the e-mails were sent.

So the answer is, restart your server, stupid. Well, anyway, at least it's working now...I'll take it where I can get it!

Upvotes: 2

David Barlow
David Barlow

Reputation: 4974

I would start with moving your email command to your WorkRequest model as a 'after_create' action

after_create :send_init_email


def send_init_email
  PersonMailer.deliver_work_request_init_email(self)
end

See if that works, or aleast gives you a better error msg.

Upvotes: 0

Related Questions