指南:https://github.com/thomasklemm/email_form_rails rails 3.2.x
应用\模型\ 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
应用\寄件人\ 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
development.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开始发布“/ email”for 127.0.0.1 由HomeController处理#send_email_form作为HTML参数: {“utf8”=&gt;“√”,“authenticity_token”=&gt;“w39BLqCrjTMm4RRi / Sm5hZoEpcw46 npyRy / RS0h48x0 =“,”message“=&gt; {”name“=&gt;”anonymousxxx“, “email”=&gt;“anonymousxxx@yahoo.com”,“subject”=&gt;“测试”,“正文”=&gt;“发送 电子邮件“},”提交“=&gt;”创建消息“}重定向到 localhost:3000 / home / contact已完成302在1ms内发现 (ActiveRecord:0.0ms)
但是电子邮件(邮件)没有收到我的电子邮件,..
答案 0 :(得分:1)
如果您只想查看触发的电子邮件,可以使用操作邮件程序配置传送方法将电子邮件保存到本地文件系统,并指定要保存的位置。我通常保存到tmp / mail。
答案 1 :(得分:0)
如果日志在开发模式下说Completed
,则它不会告知电子邮件已发送。您需要在config.action_mailer.raise_delivery_errors
中设置true
config/environment/development.rb
值,如果电子邮件发送不正确,则会显示一些错误。
Rails.application.configure do
## other stuff
config.action_mailer.raise_delivery_errors = true
## other stuff
end