Send an email through gmail but configure a different email address

左心房为你撑大大i 提交于 2019-12-23 03:21:53

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/22784259/send-an-email-through-gmail-but-configure-a-different-email-address

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