Question

I'm trying to send emails from my rails application using GMail's smtp server.

The emails I send appear like sent from my own gmail address, while I would like them to be coming from info@myapp.com.

I've configured the thing using the three ways authentication, and my app has got its unique password. Here's my production.rb

  config.action_mailer.default_url_options = {
    host: 'my ip', 
    protocol: 'https'
  }
  config.action_mailer.delivery_method = :smtp
  config.action_mailer.asset_host = "https://my ip"

and here's my initializers/smtp.rb

MyApp::Application.configure do
   config.action_mailer.smtp_settings = {
    enable_starttls_auto: "true",
    address: "smtp.gmail.com",
    port: "587",
    domain: "mydomain.com",
    authentication: :plain,
    user_name: 'myusername@gmail.com',
    password: 'mypassword'
  }
end

Is it possible? How could I do that?

Was it helpful?

Solution

I figured my solution,

I have two mailer controllers in my application, one is from devise and sends mail for user registration related stuff, and the other sends emails to me in the from the contacts page.

First let's see the configuration in google. Google allows to send emails with a different sender email, but it requires that you own the mail address, so there's a procedure to follow to verify your email address on google. It's listed here

I used that procedure to validate my email address: no_reply@myapp.com which is the one I want my emails to appear to be from

Once gmail has verified that you own the sender email address I configured devise, I changed in the config/initializers/devise.rb adding the line:

config.mailer_sender = "'myapp_noreply' <no_reply@myapp.com>"

Second I went to configure my app/mailers/notifications_mailer, by adding the line:

default :from => "no_reply@myapp.com"

I tested everything and it worked fine. If I check in the email's headers I can still see my gmail account appearing. These look like this:

Return-Path: <mygmail@gmail.com>
Received: from myapp.com ([2231:7120::f12c:912f:f12e:5123])
        by mx.google.com with ESMTPSA id gn1sm32851027wib.14.2014.04.01.05.20.26
        for <info@casamaroma.it>
        (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128);
        Tue, 01 Apr 2014 05:20:26 -0700 (PDT)
Sender: Giulio <mygmail@gmail.com>
Date: Tue, 01 Apr 2014 12:20:25 +0000
From: 'myapp_noreply' <no_reply@myapp.com>
Reply-To: 'myapp_noreply' <no_reply@myapp.com>
To: destination@email.com
Message-ID: <533aaf09c5237_2b0b123fe8c3341a@server.mail>
Subject: Confirmation instructions
Mime-Version: 1.0
Content-Type: text/html;
 charset=UTF-8
Content-Transfer-Encoding: 7bit

Because the from field is set to the one I need, I don't care too much for the rest.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top