在制作中,联系表格:ActionMailer不发送电子邮件。 ArgumentError(发送消息至少需要一个收件人(To,Cc或Bcc))

时间:2013-07-22 21:20:54

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

我创建了简单的联系表单(类似于:http://matharvard.ca/posts/2011/aug/22/contact-form-in-rails-3/),在我的开发中,它运行正常(使用开信刀) 但在我的制作中,它不发送邮件。 我搜索了一些解决方案,但我找不到解决方案。 为什么不在models/mailers/contact_mailer.rb中定义至:来发送电子邮件? 在heroku日志中,参数看起来很好,但找不到to: ...请帮助我。

整个回购 https://github.com/yhagio/yhagio

实际联系表单 http://yhagio.herokuapp.com/contact

配置/环境/ production.rb

config.action_mailer.default_url_options = { :host => "yhagio.herokuapp.com" }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
  :address              => "smtp.gmail.com",
  :domain               => 'yhagio.herokuapp.com',
  :port                 => 587,
  :user_name            => ENV['GMAIL_USERNAME'],
  :password             => ENV['GMAIL_PASSWORD'],
  :authentication       => :plain,
  :enable_starttls_auto => true,
}

模型/邮寄者/ contact_mailer.rb

class ContactMailer < ActionMailer::Base
  default to: ENV['MYGMAIL']

  def contact_message(message)
    @message = message
    mail from: message.email, subject: message.subject
  end
end

message.rb

class Message
  include ActiveModel::Validations
  include ActiveModel::Conversion
  extend ActiveModel::Naming

  validates_presence_of :subject, :body, :name
  validates :email,  presence: true, format: { with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i  }

  attr_accessor :name, :email, :body, :subject

  def initialize(attributes = {})
    attributes.each do |name, value|
      send("#{name}=", value)
    end
  end

  def persisted?
    false
  end
end

contact_mailer.rb

class ContactController < ApplicationController

  def new
    @message = Message.new
  end

  def create
    @message = Message.new(params[:message])

    if @message.valid?
      ContactMailer.contact_message(@message).deliver
      redirect_to root_path
      flash[:success] = "Message was successfully sent."
    else
      flash[:error] = "Please fill all fields."
      render :new
    end
  end
end

heroku日志

app[web.1]: Started POST "/contact" for 173.176.46.53 at 2013-07-22 23:14:50 +0000
Parameters: {"utf8"=>"✓", "authenticity_token"=>"czYfbExDwXdHvzKP7fa3LMV50CbucTGwxwV/sZ22E/c=", "message"=>{"name"=>"test", "email"=>"test@test.com", "subject"=>"hello", "body"=>"https://github.com/yhagio/yhagio/commit/c8a09d769df43a58f82959fc1e72e3c88b79821d"}, "commit"=>"Send"}
Processing by ContactController#create as HTML
Rendered contact_mailer/contact_message.html.haml (2.4ms)
at=info method=POST path=/contact host=yhagio.herokuapp.com fwd="173.176.46.53" dyno=web.1 connect=8ms service=76ms status=500 bytes=643
Sent mail to  (10ms)
Completed 500 Internal Server Error in 31ms
app/controllers/contact_controller.rb:11:in `create'
ArgumentError (At least one recipient (To, Cc or Bcc) is required to send a message):

更新:7月24日

我在 config / environments / production.rb中修改了Mailgun

config.action_mailer.smtp_settings = {
  :address        => 'smtp.mailgun.org',
  :domain         => 'yhagio.herokuapp.com',
  :port           => 587, 
  :user_name      => ENV['MAILGUN_SMTP_LOGIN'],
  :password       => ENV['MAILGUN_SMTP_PASSWORD'],
  :authentication => :plain,
  :enable_starttls_auto => true,
}

1 个答案:

答案 0 :(得分:0)

我认为域名应该是gmail.com而不是yhagio.herokuapp.com。