Search code examples
ruby-on-railsruby-on-rails-3gmailactionmailermail-gem

Rails 3 - ActionMailer/Mail gem - 'from' address is being over ridden


I have a simple contact form on a site.

There is an email account just to handle the mail being sent through the contact form. This is '[email protected]'

The problem

When a user fills in the form and sends it, two emails are sent. One confirmation email to the user of the site and the actual filled in form to the company.

The company wants it so when they receive the email from the user, they hit reply and the users email address is automatically in the 'to' field. But when they do that at the moment, what they see is "Senders name '[email protected]'" instead of "Senders name 'sender_email_address'".

The code

class Notifications < ActionMailer::Base

  def enquiry(user)
    @user = user
    mail(to: "[email protected]", subject: "website enquiry", from: "#{@user.name} <#{@user.email}>")
  end

  def no_reply(user)
    @user = user
    mail(to: @user.email, from: "My Company <[email protected]>", subject: "Message received at mycompany.com")
  end
end

Tests

The test for the from field passes.

require "spec_helper"

describe Notifications do

  let(:contact_form) { FactoryGirl.build(:contact_form) }

  context "enquiry" do

    let(:mail) { Notifications.enquiry(contact_form) }

    it "renders the headers" do
      mail.from.should eq([contact_form.email])
    end
  end
end

Also, I am using mailcatcher.

Mailcatcher also shows the correct user.email in the from field.

It only seems to be when the actual email is received (using gmail) that the wrong address appears.

Any help appreciated.

EDIT

Default mail settings:

 # mail config settings
  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = { 
    :address              => "smtp.gmail.com",
    :port                 => 587,
    :user_name            => '[email protected]',
    :password             => 'password',
    :authentication       => 'plain',
    :enable_starttls_auto => true  }

mail header from mailcatcher

Date: Sat, 13 Oct 2012 21:08:02 +0100
From: Joe Bloggs <[email protected]>
To: [email protected]
Message-ID: <5079ca229969b_67c23fc061434ed023056@Mark-Charless-iMac-2.local.mail>
Subject: website enquiry
Mime-Version: 1.0
Content-Type: multipart/alternative;
 boundary="--==_mimepart_5079ca226fb17_67c23fc061434ed02277b";
 charset=UTF-8
Content-Transfer-Encoding: 7bit



----==_mimepart_5079ca226fb17_67c23fc061434ed02277b
Date: Sat, 13 Oct 2012 21:08:02 +0100
Mime-Version: 1.0
Content-Type: text/plain;
 charset=UTF-8
Content-Transfer-Encoding: 7bit
Content-ID: <5079ca227ec80_67c23fc061434ed02288a@Mark-Charless-iMac-2.local.mail>

Solution

  • The problem is that you're using the Gmail SMTP server.

    As per this question: Rails and Gmail SMTP, how to use a custom from address, you cannot set custom From addresses unless that account is linked to your gmail account.

    Alternatives:

    1. Use another SMTP server that will allow you to set your own From addresses.
    2. Create a nice HTML-based email that is sent to [email protected] with a big button that is a mailto link to the email address of the user who filled in the form, and make sure they don't reply to the automated emails, but use this button within the email.