【问题标题】:Send an email through gmail but configure a different email address通过 gmail 发送电子邮件,但配置不同的电子邮件地址
【发布时间】:2014-04-01 21:34:14
【问题描述】:

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

我发送的电子邮件看起来像是从我自己的 gmail 地址发送的,而我希望它们来自 info@myapp.com。

我已经使用三种身份验证方式配置了这个东西,并且我的应用程序已经获得了唯一的密码。这是我的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"

这是我的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

有可能吗?我怎么能那样做?

【问题讨论】:

  • 所以您正在从 gmail 帐户接收电子邮件,但希望看起来像您的电子邮件 id..我认为在您的 info@myapp.com 中使用电子邮件转发器..发送到您的电子邮件然后它会发送到另一封电子邮件
  • 您认为无法直接从我的应用发送电子邮件,其中发件人看起来像 info@myapp.com?
  • support.google.com/mail/answer/22370?hl=en 它说可以从 gmail 界面.. 我敢打赌也可以从我的应用程序发送
  • 确实是重复的,谢谢你指点我这个问题,同时我设法找到了问题的解决方案,我应该在这里写还是关闭它并在那里回答?谢谢

标签: ruby-on-rails ruby email gmail actionmailer


【解决方案1】:

我想出了我的解决方案,

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

首先让我们看看google中的配置。 Google 允许使用不同的发件人电子邮件发送电子邮件,但它要求您拥有该邮件地址,因此需要遵循一个程序来验证您在 Google 上的电子邮件地址。它列出了here

我使用该程序来验证我的电子邮件地址:no_reply@myapp.com,这是我希望我的电子邮件显示为from的地址

一旦 gmail 验证您拥有我配置的发件人电子邮件地址,我更改了 config/initializers/devise.rb 添加以下行:

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

其次我去配置我的app/mailers/notifications_mailer,添加以下行:

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

我测试了一切,它运行良好。如果我检查电子邮件的标题,我仍然可以看到我的 gmail 帐户出现。这些看起来像这样:

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

因为from 字段设置为我需要的字段,所以我不太关心其余部分。

【讨论】:

  • 非常感谢这篇文章!这是我完全忘记的 gmail 别名功能。
猜你喜欢
  • 1970-01-01
  • 2012-01-07
  • 2023-03-19
  • 1970-01-01
  • 2016-03-10
  • 2012-06-26
  • 2014-04-11
相关资源
最近更新 更多