为什么我的电子邮件表单会发送电子邮件,有时则不会

时间:2013-12-20 06:36:29

标签: ruby-on-rails forms email heroku smtp

我创建了一个表单,一旦提交,就会发送一封电子邮件,其中包含已填写表单的内容。有时电子邮件发送,有时它不发送,为什么会这样?

notifcations_mailer.rb 

class NotificationsMailer < ActionMailer::Base
  default :from => "smile@tlcdentalsyr.com"
  default :to => "smile@tlcdentalsyr.com"

  def new_message(message)
    @message = message
    mail(:subject => "Appointment for #{message.date}")
  end
end

contact_controller.rb

class ContactController < ApplicationController

def new
  @message = Message.new
end

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

  if @message.valid?
    NotificationsMailer.new_message(@message).deliver
    redirect_to(contact_path, :notice => "Message was successfully sent.")
  else
    flash.now.alert = "Please fill all fields."
    redirect_to :back
  end
 end
end

message.rb:

class Message

 include ActiveModel::Validations
 include ActiveModel::Conversion
 extend ActiveModel::Naming

 attr_accessor :name, :currentpatient, :email, :phone, :calltime, :date, :apttime,  :treatment, :subject

 validates :name, :currentpatient, :phone, :calltime, :date, :apttime, :treatment, :presence => true
 validates_format_of :email, :with => /.+@.+\..+/i

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

def persisted?
  false
end
end

application.rb中:

config.action_mailer.smtp_settings = {
  :address              => "smtpout.secureserver.net",
  :port                 => 80,
  :domain               => "domain.com",
  :user_name            => "email@domain.com",
  :password             => "mypass",
  :authentication       => :plain,
  :enable_starttls_auto => true
}

config.action_mailer.default_url_options = {
  :host => "domain.com"
}   

heroku日志(这次没有发送电子邮件):

2013-12-20T06:28:54.321446+00:00 app[web.1]: Started POST "/contact" for 67.174.151.10 at          2013-12-20 06:28:54 +0000
2013-12-20T06:28:54.335613+00:00 app[web.1]: Completed 302 Found in 8ms (ActiveRecord: 0.0ms)
2013-12-20T06:28:54.335613+00:00 app[web.1]:   Parameters: {"utf8"=>"✓", "authenticity_token"=>"36gs7HfnaIbH0003rYZ+6mC3sFE36ut8iHHiBZgSAlo=", "message"=>{"name"=>"Eric Filkins", "email"=>"email@syr.edu", "phone"=>"8675309", "calltime"=>"Morning", "date"=>"Chrome test", "apttime"=>"Morning", "treatment"=>"Dream Smiles"}, "commit"=>"Submit"}
2013-12-20T06:28:54.335613+00:00 app[web.1]: Processing by ContactController#create as HTML
2013-12-20T06:28:54.335613+00:00 app[web.1]: Redirected to    http://www.tlcdentalsyr.com/contact
2013-12-20T06:28:54.339538+00:00 heroku[router]: at=info method=POST path=/contact  host=www.tlcdentalsyr.com fwd="67.174.151.10" dyno=web.1 connect=1ms service=22ms status=302 bytes=101
2013-12-20T06:28:54.440927+00:00 app[web.1]: Started GET "/contact" for 67.174.151.10 at 2013-12-20 06:28:54 +0000
2013-12-20T06:28:54.496993+00:00 app[web.1]: Processing by StaticPagesController#contact as HTML
2013-12-20T06:28:54.496993+00:00 app[web.1]: Completed 200 OK in 50ms (Views: 50.0ms | ActiveRecord: 0.0ms)
2013-12-20T06:28:54.496993+00:00 app[web.1]:   Rendered static_pages/contact.html.erb within layouts/application (39.8ms)

1 个答案:

答案 0 :(得分:2)

您缺少配置,请查看以下配置中的production.rb文件

#default url for mailer
config.action_mailer.default_url_options = { :host => 'domainname' }
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 = {
 :enable_starttls_auto => false,
 :address => "smtp.emailsrvr.com",
 :port => 587,
 :domain => "domainname",
 :authentication => :login,
 :user_name => 'smtp@domainname.com',
 :password => 'password',
}

快乐的编码。!!