我想测试我的ActionMailer类,但是没有SMTP服务器。我想使用Gmail发送这些电子邮件。能否提供一个示例,其中包含Google和应用程序中的所有必要配置文件的配置?
我想测试我的ActionMailer类,但是没有SMTP服务器。我想使用Gmail发送这些电子邮件。能否提供一个示例,其中包含Google和应用程序中的所有必要配置文件的配置?
GMail只支持SSL SMTP,因此您应该通过Net::SMTP创建一个SSL SMTP连接。
请参考以下文章:
我使用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.rb
或 production/environment.rb
文件中:
ActionMailer::Base.delivery_method = :sendmail
ActionMailer::Base.raise_delivery_errors = true