在生产环境中发送邮件

时间:2015-02-17 14:42:27

标签: ruby-on-rails gmail actionmailer production-environment

我正在尝试在生产环境中发送邮件,如下所示:

在user-mailer.rb中:

class UserMailer < ActionMailer::Base
  default to: "a.b@gmail.com"
  def feedBack_mail(email,message)
    @mess=message
    @url  = 'http://www.google.com'
    mail(from: email, subject: 'new_feedback')
  end
end

在feedback_mail.text.erb

<%= @mess %>

在user_controller.rb

def create  
    respond_to do |format|
      @client= params[:client]
      if UserMailer.feedBack_mail(params[:email],params[:message]).deliver
        format.json { render json: @client, status: :created, location: @client }
      else 
        format.json {render json: @client.errors, status: :unprocessable_entity}
      end
    end
end
生产中的

.rb我添加了这个配置:

if config.respond_to?(:action_mailer)
    # config.action_mailer.raise_delivery_errors = false
    config.action_mailer.default_url_options = { :host => 'monsite.com' }
    # ActionMailer Config
    # Setup for production - deliveries, no errors raised
    config.action_mailer.delivery_method = :smtp
    config.action_mailer.perform_deliveries = true
    config.action_mailer.raise_delivery_errors = false
    config.action_mailer.default :charset => "utf-8"
    config.action_mailer.smtp_settings = {
        address: "smtp.gmail.com",
        port: 25,
        domain: "monsite.com",
        authentication: "plain",
        enable_starttls_auto: true,
        user_name: ENV["GMAIL_USERNAME"],
        password: ENV["GMAIL_PASSWORD"]
    }
  end

我在production.log中得到了什么

I, [2015-02-17T15:38:05.436159 #2003552]  INFO -- : Started POST "/feedback/send.json" for 197.14.10.26 at 2015-02-17 15:38:05 +0100
I, [2015-02-17T15:38:05.473826 #2003552]  INFO -- : Processing by UsersController#create as JSON
I, [2015-02-17T15:38:05.473943 #2003552]  INFO -- :   Parameters: {"email"=>"c.d@gmail.com", "message"=>"qerfsertq", "user"=>{"email"=>"c.d@gmail.com", "message"=>"qerfsertq"}}
I, [2015-02-17T15:38:05.524701 #2003552]  INFO -- :   Rendered user_mailer/feedBack_mail.text.erb (0.5ms)
I, [2015-02-17T15:38:08.673302 #2003552]  INFO -- : 
Sent mail to a.b@gmail.com (755.5ms)
I, [2015-02-17T15:38:08.674260 #2003552]  INFO -- : Completed 201 Created in 3200ms (Views: 0.3ms | ActiveRecord: 0.0ms)

但我没有收到任何邮件发送到a.b@gmail.com,甚至不是垃圾邮件

我真的无法理解问题在哪里

1 个答案:

答案 0 :(得分:1)

我通过创建Mandrill帐户并更改配置来解决问题,如下所示:

config.action_mailer.smtp_settings = {
        :port =>           '587',
        :address =>        'smtp.mandrillapp.com',
        :user_name =>      'xxxxxxxx',
        :password =>       'xxxxxx',
        :domain =>         'domain.com',
        :authentication => :plain
    }
    config.action_mailer.perform_deliveries = true
    config.action_mailer.raise_delivery_errors = true
    config.action_mailer.default_url_options = { host: "monsite.com" }