類似的問題 Rails contact form not workingRuby on Rails的 - 而不是發展方式發送電子郵件
指南:https://github.com/thomasklemm/email_form_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: "[email protected]"
default to: "[email protected]"
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 => "[email protected]",
:password => "mypassword",
:authentication => :plain,
:enable_starttls_auto => true
}
config.action_mailer.default_url_options = {
:host => "localhost:3000"
}
在命令輸出
開始POST爲127.0.0.1 「/電子郵件」 在2012年9月4日22時10分四十秒0700 處理通過的HomeController#send_email_form如HTML參數 {「utf8」=>「√」,「authenticity_token」=>「w39BLqCrjTMm4RRi/Sm5hZoEpcw46 npyRy/RS0h48x0 =」,「message」=> {「name」=>「anonymousxxx」, 「email」= >「[email protected]」,「subject」=>「Test」,「body」=>「send email」},「commit」=>「Create Message」}重定向到 本地主機:完成302 3000 /首頁/聯繫發現在1毫秒 (ActiveRecord的:0.0ms)
但電子郵件(消息)沒收到我的電子郵件,..