类似问题导轨联系表不起作用
指南:https://github.com/thomasklemm/email_form_rails导轨 3.2.x
app\models\message.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 开始为 127.0.0.1 发布 "/email" 由 HomeController#send_email_form 作为 HTML 参数进行处理: {"utf8"=>"√", "authenticity_token"=>"w39BLqCrjTMm4RRi/Sm5hZoEpcw46 npyRy/RS0h48x0=", "message"=>{"name"=>"anonymousxxx", "电子邮件"=>"anonymousxxx@yahoo.com", "主题"=>"测试", "正文"=>"发送 email"}, "commit"=>"创建消息"} 重定向至 本地主机:3000/首页/联系我们 完成 302 在 1ms 内找到 (活动记录:0.0 毫秒(
但是电子邮件(消息(没有收到我的电子邮件,..
如果您只想查看电子邮件已触发,则有一种方法可以使用操作邮件程序配置传递方法将电子邮件保存到本地文件系统并指定要保存的位置。我通常保存到tmp/mail。
如果日志说Completed
在开发模式下,它不会告诉电子邮件已发送。您需要在config/environment/development.rb
中使用true
值设置config.action_mailer.raise_delivery_errors
,如果电子邮件未正确发送,它将显示一些错误。
Rails.application.configure do
## other stuff
config.action_mailer.raise_delivery_errors = true
## other stuff
end