加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 如何使用Mandrill发送和获取邮件?

发布时间:2020-12-17 02:40:10 所属栏目:百科 来源:网络整理
导读:我正在使用 Ruby on Rails,你能教我如何使用Mandrill发送和接收邮件. 我的问题是当用户输入文本并按提交时,我希望将消息发送到我的邮件. 我已阅读文档,但我不明白. 这是我的development.rb,与production.rb相同 ActionMailer::Base.smtp_settings = { :port
我正在使用 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

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

在控制器中:

def index
  UserMailer.welcome.deliver
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',:user_name =>      ENV['MANDRILL_USERNAME'],:password =>       ENV['MANDRILL_APIKEY'],:domain =>         'domain.com',:authentication => :plain
}

这基本上都是.现在您可以使用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" }

测试前重启您的应用

编辑2

如果您希望ActionMailer在提交按钮上单击发送电子邮件,则需要移动UserMailer.welcome.deliver以创建相应控制器的操作.

def create
   if @object.save
     UserMailer.welcome.deliver
   else
     render "new"
   end
end

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读