Send an email through gmail but configure a different email address

雨燕双飞 提交于 2019-12-06 16:45:51

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!