Why doesn't Action Mailer send an email template?

I use the "Action Mailer" in a Ruby on Rails application to send emails. I have the following mail action program:

class SecurityUserMailer < ActionMailer::Base default from: ' myemail@gmail.com ' def password_reset(security_user) @security_user = security_user mail to: security_user.email, subject: 'Password Reset' end def email_confirmation(security_user) @security_user = security_user mail to: security_user.email, subject: 'Account Created' end end 

I successfully send emails, but the second method (email_confirmation) does not use the appropriate template.

Email templates are located in the views / security_users_mailer folder and are named as follows:

  • email_confirmation.txt.erb
  • password_reset.txt.erb

Why is only the password_reset pattern used?

Please note: firstly, I thought that maybe something is wrong with the code in my template, but then I replace it with text content and it does not appear again.

+4
source share
3 answers

The problem was caused by the TYPO file extension. I have

email_confirmation. Txt .erb

and the mailing template should contain the text or html extension.

As you can see from the official docs - a template with the same mailer action is used by default, if it exists.

+4
source

Rails 4 I had the same problem, but mine was caused by an empty layout in layouts/mailer.html.erb

+3
source

I believe that an alternative would be to specify the template you want to display with the following example of how you can do this

 class SecurityUserMailer < ActionMailer::Base default from: ' myemail@gmail.com ' def password_reset(security_user) @security_user = security_user mail to: security_user.email, subject: 'Password Reset' end def email_confirmation(security_user) @security_user = security_user mail (:to => security_user.email, :subject => 'Account Created', :template_path => 'email_confirmation.txt.erb', :template_name => 'another') end end 

Take a look at the following for more information:

Mailer Views

or look

Api Ruby on Rails You will see an example where he says Or even render a special view , otherwise you could have the following inside your mail block:

 mail (:to => security_user.email, :subject => 'Account Created') do |format| format.html { render 'another_template' } format.text { render :text => 'email_confirmation.txt.erb' } end 

This should shed light on what you are trying to accomplish.

+2
source

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


All Articles