2013-08-30 95 views
2

我想我已经组建的ActionMailer成功,但邮件没有在他们到达收件箱时,尽管服务器说明不同:Rails应用程序可以从本地主机发送电子邮件吗?

Sent mail to [email protected] (2003ms) 
Date: Fri, 30 Aug 2013 22:26:41 +0100 
From: [email protected] 
To: [email protected] 
Message-ID: <[email protected]> 
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的情况。

+1

你envt中的你的smtp设置是什么? – Raghu

+0

在发布之前做一些研究:http:// stackoverflow。com/questions/1789032/send-email-from-localhost –

+0

我使用链接Bruno Ferreira提供的从本地主机发送邮件(通过Gmail),工作得很好:) –

回答

3

如果设置不正确,可能会导致电子邮件发送失败,但不会引发任何异常。假设您使用SMTP协议提供主协议,则应确保您已正确设置了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 } 
+0

我正在关注本教程http:// guides。 rubyonrails.org/action_mailer_basics.html,并没有提到那些参数。他们是否为Rails 3?感谢您说电子邮件可以毫无例外地发送!这可能是我的某个地方的一个愚蠢的错误...... – Starkers

+0

这些参数在您引用的指南中明确说明。看看这里的第6.2节:http://guides.rubyonrails.org/action_mailer_basics.html#action-mailer-configuration-for-gmail – zeantsoi

+0

啊。尴尬。现在是凌晨2:00:P – Starkers

2

启用有关行动邮件待提高投递错误中得到行动邮件工作荒谬很有帮助。它也被默认禁用。

得到它的工作,打开你的开发环境的文件,位于:

yourappname /配置/环境/ development.rb

周围17行,有使的方法和通过采取布尔禁用错误报告:

config.action_mailer.raise_delivery_errors = false 

其更改为“真”,并重新启动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 settins在production.rb和test.rb环境配置的文件。如果您使用Gmail作为电子邮件的发件人地址,则此示例哈希也是100%正确的。唯一需要更改的字符串是密码和用户名。

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

[email protected] 

==

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))" 

机会是唯一不对您应用程序是你的mtp设置。可能是你的密码。

相关问题