【问题标题】:Why does my email form send emails and sometimes doesn't为什么我的电子邮件表单会发送电子邮件,有时却没有
【发布时间】:2014-01-08 23:14:57
【问题描述】:

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

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

应用程序.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)

【问题讨论】:

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


    【解决方案1】:

    您缺少配置,请查看下面的 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',
    }
    

    编码愉快。!!

    【讨论】:

      猜你喜欢
      • 2019-11-07
      • 2017-08-07
      • 2015-03-15
      • 2019-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-06
      • 2014-05-30
      相关资源
      最近更新 更多