使用ActionMailer发送GMAIL邮件的完整示例

3

我想测试我的ActionMailer类,但是没有SMTP服务器。我想使用Gmail发送这些电子邮件。能否提供一个示例,其中包含Google和应用程序中的所有必要配置文件的配置?

2个回答

5

4

我使用SSMTP来实现这个功能。它充当SMTP服务器并代理到真正的SMTP服务器。在Unix系统(例如Ubuntu hardy),它可以使系统的sendmail正常工作。

如果你也在使用Ubuntu,运行apt-get install ssmtp即可安装。

以下是基于我的配置文件的示例配置文件。

#
# Config file for sSMTP sendmail
#
# The person who gets all mail for userids < 1000
# Make this empty to disable rewriting.
root=postmaster

# The place where the mail goes. The actual machine name is required no 
# MX records are consulted. Commonly mailhosts are named mail.domain.com
#mailhub=aspmx.l.google.com
mailhub=smtp.gmail.com:587

# Where will the mail seem to come from?
rewriteDomain=example.com

# The full hostname
hostname=yourhostname.example.com

# Are users allowed to set their own From: address?
# YES - Allow the user to specify their own From: address
# NO - Use the system generated From: address
FromLineOverride=YES

# should turn on SSL & auth to google's SMTP server
# TODO change this user
UseTLS=YES
UseSTARTTLS=YES
AuthUser=yourgoogleuser@example.com.com
AuthPass=yourgooglepassword

您需要将以下内容添加到您的 environment.rbproduction/environment.rb 文件中:

ActionMailer::Base.delivery_method = :sendmail
ActionMailer::Base.raise_delivery_errors = true

1
我尝试了这个,但是出现了以下错误:在UsersController#create中的Errno::EPIPE 管道破裂我测试了sendmail和ssmtp,并且可以通过终端在rails之外发送电子邮件。您有什么见解可能导致这种情况或者它意味着什么?谷歌搜索没有成功的解决方案。 - Zach Alberico
感谢 Rails Casts,这个链接很有用:http://railscasts.com/episodes/206-action-mailer-in-rails-3 - Zach Alberico

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接