Ruby on rails 如何使用Mandrill发送和获取邮件?
我正在使用RubyonRails,您能教我如何使用Mandrill发送和接收邮件吗 我的问题是,当用户输入文本并按submit时,我希望消息被发送到我的邮件中。 我读过文档,但不理解 这是我的development.rb,与production.rb相同Ruby on rails 如何使用Mandrill发送和获取邮件?,ruby-on-rails,email,actionmailer,mandrill,Ruby On Rails,Email,Actionmailer,Mandrill,我正在使用RubyonRails,您能教我如何使用Mandrill发送和接收邮件吗 我的问题是,当用户输入文本并按submit时,我希望消息被发送到我的邮件中。 我读过文档,但不理解 这是我的development.rb,与production.rb相同 ActionMailer::Base.smtp_settings = { :port => '587', :address => 'smtp.mandrillapp.com',
ActionMailer::Base.smtp_settings = {
:port => '587',
:address => 'smtp.mandrillapp.com',
:user_name => 'myemail@gmail.com',
:password => 's82SlRM5dPiKL8vjrJfj4w',
:domain => 'heroku.com',
:authentication => :plain
}
ActionMailer::Base.delivery_method = :smtp
用户_mailer.rb:
class UserMailer < ActionMailer::Base
default from: "from@example.com"
def welcome
mail(to: "morumotto26@gmail.com", subject: "Welcome", body: "Have a nice day")
end
end
我的日志文件如下所示:
Started GET "/users/" for 127.0.0.1 at 2014-01-21 16:14:10 +0700
Processing by UsersController#index as HTML
Sent mail to morumotto26@gmail.com (2008.0ms)
Date: Tue, 21 Jan 2014 16:14:10 +0700
From: myemail@gmail.com
To: morumotto26@gmail.com
Message-ID: <52de3a62c89b2_5e8c9ea1881631@tnkjapan10.mail>
Subject: Welcome
Mime-Version: 1.0
Content-Type: text/plain;
charset=UTF-8
Content-Transfer-Encoding: 7bit
Have a nice day
Rendered text template (0.0ms)
Completed 200 OK in 2018ms (Views: 1.0ms | ActiveRecord: 0.0ms)
首先,您需要在mandrill.com上创建帐户 登录后,选择集成类型:SMTP或API。SMTP将在大多数情况下适合您 单击SMTP,然后创建API密钥 然后在development.rb或production.rb文件中 添加以下行:
config.action_mailer.smtp_settings = {
:port => '587',
:address => 'smtp.mandrillapp.com',
:user_name => ENV['MANDRILL_USERNAME'],
:password => ENV['MANDRILL_APIKEY'],
:domain => 'domain.com',
:authentication => :plain
}
基本上就这些。现在你可以用Mandrill发送电子邮件了
编辑
另外,尝试将这些行添加到您的环境文件中,以执行传递并提出传递错误(如果有)。还可以在development localhost:3000中的production heroku.com中添加默认的_url_选项
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default_url_options = { host: "localhost:3000" }
测试前重新启动应用程序
编辑2
如果您希望ActionMailer在单击提交按钮时发送电子邮件,则需要移动UserMailer.welcome.deliver以创建相应控制器的操作
def create
if @object.save
UserMailer.welcome.deliver
else
render "new"
end
end
谢谢,我试过了,但还是没有收到任何邮件。默认值来自:myemail@gmail.comdef欢迎电子邮件发送至:blahblah@gmail.com,主题:欢迎访问我的网站。您是否使用了正确的用户名和密码?暂时删除ENV[],只插入普通字符串。还要确保您的环境.rb文件中有config.action\u mailer.delivery\u method=:smtp。当您打开索引操作时,您的日志文件是什么样子的?我发布了该文件,我已经尝试发送邮件两天了,但仍然无法做到:将ActionMailer::Base重命名为config.action\u mailer,如我的回答所示。另外,您可以尝试将ActionMailer::Base.smtp_settings={}和ActionMailer::Base.delivery_方法直接移动到用户_mailer.rb
def create
if @object.save
UserMailer.welcome.deliver
else
render "new"
end
end