通过gmail发送电子邮件,但配置不同的电子邮件地址 [英] Send an email through gmail but configure a different email address

查看:302
本文介绍了通过gmail发送电子邮件,但配置不同的电子邮件地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用GMail的smtp服务器从我的rails应用程序发送电子邮件。



我发送的电子邮件似乎是从我自己的Gmail地址发送的,而我希望他们来自info@myapp.com。



我已经使用三种身份验证配置了这个东西,我的应用程序已经获得了唯一的密码。这是我的 production.rb

  config.action_mailer.default_url_options = {
主机:'我的ip',
协议:'https'
}
config.action_mailer.delivery_method =:smtp
config.action_mailer.asset_host =https: /我的ip

这里是我的 initializers / smtp.rb

  MyApp :: Application.configure do 
config.action_mailer.smtp_settings = {
enable_starttls_auto:true,
地址:smtp.gmail.com,
端口:587,
域:mydomain.com,
身份验证: ,
user_name:'myusername@gmail.com',
密码:'mypassword'
}
end

可以吗?我该怎么办?

解决方案

我想到了我的解决方案,



我有两个邮件控制器在我的应用程序中,一个来自设计,并发送用于用户注册相关内容的邮件,另一个从联系人页面发送到我的电子邮件。



首先让我们看看google中的配置。 Google允许使用不同的发件人电子邮件发送电子邮件,但要求您拥有邮件地址,因此,请在Google上验证您的电子邮件地址。 此处列出



我使用该过程验证我的电子邮件地址: no_reply@myapp.com 这是我希望我的电子邮件似乎是从一旦gmail已经验证您拥有我配置的发件人电子邮件地址,我更改了配置/初始化程序中的

/devise.rb 添加行:

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

第二个我去配置我的 app / mailers / notifications_mailer ,添加以下行:

  default:from => no_reply@myapp.com

我测试了一切,工作正常。如果我查看电子邮件的标题,我仍然可以看到我的Gmail帐户出现。这些如下所示:

 返回路径:< mygmail@gmail.com> 
收到:从myapp.com([2231:7120 :: f12c:912f:f12e:5123])
由mx.google.com与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);
星期二,2014年4月01日05:20:26 -0700(PDT)
发件人:Giulio< mygmail@gmail.com>
日期:星期二,2014年4月1日12:20:25 +0000
从:'myapp_noreply'< no_reply@myapp.com>
回覆:myapp_noreply< no_reply@myapp.com>
至:destination@email.com
Message-ID:< 533aaf09c5237_2b0b123fe8c3341a@server.mail>
主题:确认说明
Mime版本:1.0
内容类型:text / html;
charset = UTF-8
Content-Transfer-Encoding:7bit

因为来自字段的字段设置为我需要的,我不在乎其余的太多。


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?

解决方案

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.

这篇关于通过gmail发送电子邮件,但配置不同的电子邮件地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆