0

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 'mycompany.noreply@gmail.com'

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 'mycompany.noreply@gmail.com'" instead of "Senders name 'sender_email_address'".

The code

class Notifications < ActionMailer::Base

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

  def no_reply(user)
    @user = user
    mail(to: @user.email, from: "My Company <mycompany.noreply@gmail.com>", 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            => 'mycompany.noreply@gmail.com',
    :password             => 'password',
    :authentication       => 'plain',
    :enable_starttls_auto => true  }

mail header from mailcatcher

Date: Sat, 13 Oct 2012 21:08:02 +0100
From: Joe Bloggs <joebloggs@gmail.com>
To: mycomany.noreply@gamil.com
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>
veritas1
  • 8,740
  • 6
  • 27
  • 37
  • Can you list the defaults specified for ActionMailer::Base in your config? Is a default :return_path specified there? Any chance of posting the headers from one of the mail messages? – mccannf Oct 13 '12 at 18:11
  • Also, bear in mind that if you are using Gmail's SMTP server, it won't allow you to send emails with a custom From address. See this question here: http://stackoverflow.com/questions/109520/rails-and-gmail-smtp-how-to-use-a-custom-from-address – mccannf Oct 13 '12 at 18:14
  • @mccannf - Thanks for your input. Have added extra info to the question and am looking at the link you provided. – veritas1 Oct 13 '12 at 20:16

2 Answers2

2

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 employee@mycompany.com 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.
Community
  • 1
  • 1
mccannf
  • 16,619
  • 3
  • 51
  • 63
  • I recommend using SendGrid. It integrates into ActionMailer nicely and you can leverage other features to ensure that your email is delivered. https://github.com/stephenb/sendgrid – scarver2 Oct 14 '12 at 00:51
0

Gmail does not allow name spoofing. There for it will not use the default from: but it will use the SMTP username.

LifterCoder
  • 142
  • 1
  • 9