【问题标题】:Can a rails application send emails from the local host?Rails 应用程序可以从本地主机发送电子邮件吗?
【发布时间】:2013-09-03 17:09:04
【问题描述】:

我想我已经成功设置了 ActionMailer,但是电子邮件没有到达他们的收件箱,尽管服务器声明不同:

Sent mail to julian@designimperial.com (2003ms)
Date: Fri, 30 Aug 2013 22:26:41 +0100
From: from@example.com
To: julian@gmail.com
Message-ID: <52210e11bedc9_16501d84f64257a@Julian-PC.mail>
Subject: Welcome to my awesome site!
Mime-Version: 1.0
Content-Type: text/html;
 charset=UTF-8
Content-Transfer-Encoding: 7bit

<!DOCTYPE html>
<html>
  <head>
    <meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
  </head>
  <body>
    <h1>Welcome to example.com, Julian</h1>
    <p>
      You have successfully signed up to example.com,
      your username is: gogo.<br/>
    </p>
    <p>
      To login to the site, just follow this link: http://example.com/login.
    </p>
    <p>Thanks for joining and have a great day!</p>
  </body>
</html>

我是否需要将我的 rails 应用程序上传到服务器或 heroku 才能发送电子邮件?我知道 PHP 邮件和 MAMP/XAMPP 就是这种情况

【问题讨论】:

标签: ruby-on-rails


【解决方案1】:

设置不正确可能会导致电子邮件无法发送而不会引发任何异常。假设您使用 SMTP 协议来传递 main,您应该确保您已正确设置您的 ActionMailer configuration。下面是使用 Gmail 的基本配置(通过Rails Guides):

config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
  address:              'smtp.gmail.com',
  port:                 587,
  domain:               'example.com',
  user_name:            '<username>',
  password:             '<password>',
  authentication:       'plain',
  enable_starttls_auto: true  }

【讨论】:

  • 我正在关注本教程,guides.rubyonrails.org/action_mailer_basics.html,并且没有提及这些参数。它们适用于 Rails 3 吗?感谢您说可以毫无例外地发送电子邮件!这可能是我在某个地方犯的一个愚蠢的错误......
  • 您引用的指南中明确说明了这些参数。在此处查看第 6.2 节:guides.rubyonrails.org/…
  • 啊。尴尬。现在是凌晨 2:00:P
  • 这是否有助于解决您的问题?如果是这样,您会考虑接受/投票赞成这个答案吗?
  • 我已经投票了!我明天会对此进行破解并标记为正确
【解决方案2】:

启用与要引发的操作邮件有关的传递错误对于使操作邮件正常工作非常有帮助。默认情况下它也被禁用。

要使其正常工作,请打开位于以下位置的开发环境文件:

yourappname/config/environments/development.rb

在第 17 行附近,有一种方法可以通过采用布尔值来启用和禁用错误报告:

  config.action_mailer.raise_delivery_errors = false

将其更改为“true”,然后重新启动 rails 服务器。您现在可以从 rails 获得出色的错误报告。

我也喜欢将我的 smtp 设置直接放在下面的同一个文件中,如下所示:

  #SMTP settings
  config.action_mailer.delivery_method = :smtp 
  config.action_mailer.smtp_settings = {
    address: "smtp.gmail.com",
    port: 587,
    domain: "gmail.com",
    user_name: "mrexample",
    password: "01password02"
  }  

记得将您的 smtp 设置也放入您的 production.rb 和 test.rb 环境配置文件中。如果您使用 gmail 作为电子邮件的发件人地址,此示例哈希也是 100% 正确的。您需要更改的唯一字符串是密码和用户名。

在 gmail 中不太明显的另一件事是,您的用户名是 @ 符号左侧的所有内容,而域是 @ 右侧的所有内容。

例如

mrexample@gmail.com

==

  config.action_mailer.smtp_settings = {
   # address: "smtp.gmail.com",
   # port: 587,
    domain: "gmail.com",
    user_name: "mrexample",
   # password: "01password02"
  }  

最后,如果您收到错误消息

"Errno::ECONNREFUSED (No connection could be made because the target machine actively refused it. - connect(2))"

您的应用程序唯一有问题的可能是您的 smtp 设置。可能是你的密码。

【讨论】:

    猜你喜欢
    • 2012-03-18
    • 1970-01-01
    • 2016-07-22
    • 2013-04-16
    • 2014-10-25
    • 2013-10-01
    相关资源
    最近更新 更多