use*_*298 4 email ruby-on-rails actionmailer mandrill
我正在使用Ruby on Rails,你能教我如何使用Mandrill发送和接收邮件.
我的问题是当用户输入文本并按提交时,我希望将消息发送到我的邮件.我已阅读文档,但我不明白.
这是我的development.rb,与production.rb相同
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
Run Code Online (Sandbox Code Playgroud)
user_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
Run Code Online (Sandbox Code Playgroud)
在控制器中:
def index
UserMailer.welcome.deliver
end
Run Code Online (Sandbox Code Playgroud)
我的日志文件如下所示:
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)
Run Code Online (Sandbox Code Playgroud)
首先,您需要在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
}
Run Code Online (Sandbox Code Playgroud)
这基本上都是.现在您可以使用Mandrill发送电子邮件.
编辑
还尝试将这些行添加到您的环境文件中以执行交付并提高交付错误(如果有的话).还要在生产heroku.com中的开发localhost:3000中添加default_url_options
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default_url_options = { host: "localhost:3000" }
Run Code Online (Sandbox Code Playgroud)
测试前重启您的应用
编辑2
如果您希望ActionMailer在提交按钮上单击发送电子邮件,则需要移动UserMailer.welcome.deliver
以创建相应控制器的操作.
def create
if @object.save
UserMailer.welcome.deliver
else
render "new"
end
end
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3082 次 |
最近记录: |