我创建了简单的联系表格(类似于: http: //matharvard.ca/posts/2011/aug/22/contact-form-in-rails-3/),在我的开发中,它工作正常(使用开信刀)但是在我的生产中,它不发送邮件。我搜索了一些解决方案,但找不到解决方案。为什么不发送电子邮件,即使to:定义在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
消息.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,
}