Sending mail from Rails works on the console, but not in my application ...?

I have a pretty simple Rails application and I want to send an email when creating a work request. I am new to Rails, so I found some details on how to do this on the Internet. Configure Mailer, configure it, etc. Customize my templates. Good.

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. 

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

At the server output, I see that the HTML version has been rendered, and I see the information about the email, and it all looks both heavy and dory. Since I have no errors, I do not understand how to do this.

+4
source share
2 answers

Ok, I'm officially an idiot. :-)

While working on another problem, I edited application.rb. I decided that I need to restart the server so that it sees these changes. Suddenly emails work inside the application.

D'ah! I did not understand (rookie error) that I needed to restart the server for the application in order to see the email configuration that I posted yesterday in the environment.rb file. I have never tried this for some reason.

Now I see that other components only start when the server starts. Therefore, of course, when I started the console, of course, it started all initializers, and therefore the email configuration was visible to her and emails were sent.

So the answer is: reboot your server, stupid. Well, anyway, at least now it works ... I'll take it where I can!

+2
source

I would start by porting your email to your WorkRequest model as the "after_create" action

 after_create :send_init_email def send_init_email PersonMailer.deliver_work_request_init_email(self) end 

See if this works or aleast gives you an error message.

0
source

Source: https://habr.com/ru/post/1343080/


All Articles