0

类似的问题 Rails 联系表格不起作用

指南:https ://github.com/thomasklemm/email_form_rails rails 3.2.x

应用\模型\消息.rb

class Message
  include ActiveAttr::Model
  include ActiveModel::Validations

  attribute :name
  attribute :email
  attribute :subject
  attribute :body
  attr_accessible :name, :email, :subject, :body

  validates_presence_of :name
  validates_presence_of :email
  validates :email, email_format: { message: "is not looking like a valid email address"}
  validates_presence_of :subject
  validates_length_of :body, maximum: 500
end

app\mailers\contact_form.rb

class ContactForm < ActionMailer::Base
  default from: "myemail@gmail.com"
  default to: "myemail@gmail.com"

  def email_form(message)
    @message = message
    mail subject: "#{message.subject} #{message.name}"
    mail body: "#{message.body}"
  end
end

发展.rb

  config.action_mailer.delivery_method = :smtp
  config.action_mailer.perform_deliveries = true
  config.action_mailer.smtp_settings = {
      :address              => "smtp.gmail.com",
      :port                 => 587,
      :domain               => "mydomain.com",
      :user_name            => "myemail@gmail.com",
      :password             => "mypassword",
      :authentication       => :plain,
      :enable_starttls_auto => true
  }

  config.action_mailer.default_url_options = {
      :host => "localhost:3000"
  }

命令输出

在 2012-09-04 22:10:40 +0700 开始 POST "/email" for 127.0.0.1 由 HomeController#send_email_form 作为 HTML 参数处理:{"utf8"=>"√", "authenticity_token"=>"w39BLqCrjTMm4RRi/ Sm5hZoEpcw46 npyRy/RS0h48x0=", "message"=>{"name"=>"anonymousxxx", "email"=>"anonymousxxx@yahoo.com", "subject"=>"Test", "body"=>" send email"}, "commit"=>"Create Message"} Redirected to localhost:3000/home/contact Completed 302 Found in 1ms (ActiveRecord: 0.0ms)

但是电子邮件(消息)没有收到我的电子邮件,..

4

2 回答 2

1

如果您只想看到电子邮件被触发,有一种方法可以使用操作邮件配置传递方法将电子邮件保存到本地文件系统并指定要保存它的位置。我通常保存到 tmp/mail。

于 2012-09-05T06:05:14.527 回答
0

如果日志说是Completed在开发模式下,它不会告诉电子邮件已发送。您需要在 中设置config.action_mailer.raise_delivery_errorstrueconfig/environment/development.rb如果电子邮件发送不正确,它将显示一些错误。

Rails.application.configure do
    ## other stuff
    config.action_mailer.raise_delivery_errors = true
    ## other stuff
end
于 2017-03-07T02:29:33.373 回答