Rails 3: action mailer not usng the :from => param

2020-03-30 02:51发布

问题:

Hi guys I'm trying to get emails running for a "Contact" page and the emails are coming through just fine but the problem is that the "from" field won't show my :from => "" that's inside my mailer method. The same thing happens with my recover password function which is below. The email which persists in the 'from' field is "myusername@gmail.com" from the smtp settings. Really appreciate the any help with this!

I'm using Gmail and setup my smtp settings like this:

config/initializers/mailer_setup

ActionMailer::Base.smtp_settings = {
     :address              => "smtp.gmail.com",
     :port                 => 587,
     :domain               => "mydomain.com",
     :user_name            => "myusername@gmail.com",
     :password             => "mypassword",
     :authentication       => "plain",
     :enable_starttls_auto => true
     }

mailer/user_mailer.rb

   def reset_password_instructions(user)
     @user = user
     @url  = "http://localhost:3000"
     mail(:to => user.email,
          :from => "test@mydomain.com",
          :subject => "recover your password"
          )
   end
end

views/mailer/rest_password_instructions.html.erb

<p>Hello <%= @resource.email %>!</p>

<p>Someone has requested a link to change your password, and you can do this through the link below.</p>

<p><%= link_to 'Change my password', edit_password_url(@resource, :reset_password_token => @resource.reset_password_token) %></p>

<p>If you didn't request this, please ignore this email.</p>
<p>Your password won't change until you access the link above and create a new one.</p>

回答1:

Gmail typically doesn't let you set the from field for sending emails. For this reason, a lot of people switch to other mail services like SendGrid.

If you google "Rails gmail from field" you'll unfortunately see all the other people who have struggled with this in the past.