0
votes

similar problem Rails contact form not working

guides: https://github.com/thomasklemm/email_form_rails 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: "[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"
  }

output in command

Started POST "/email" for 127.0.0.1 at 2012-09-04 22:10:40 +0700 Processing by HomeController#send_email_form as HTML Parameters: {"utf8"=>"√", "authenticity_token"=>"w39BLqCrjTMm4RRi/Sm5hZoEpcw46 npyRy/RS0h48x0=", "message"=>{"name"=>"anonymousxxx", "email"=>"[email protected]", "subject"=>"Test", "body"=>"send email"}, "commit"=>"Create Message"} Redirected to localhost:3000/home/contact Completed 302 Found in 1ms (ActiveRecord: 0.0ms)

but email (message) no receive my email,..

2

2 Answers

1
votes

If you only want to see the email been triggered there is a way to save the email to local file system using action mailer config delivery method and specifying where you want to save it. I normally save to tmp/mail.

0
votes

If log say's Completed on development mode, it doesn't tell the email was sent. You need to set config.action_mailer.raise_delivery_errors with true value in config/environment/development.rb, it will display some error if email not sending correctly.

Rails.application.configure do
    ## other stuff
    config.action_mailer.raise_delivery_errors = true
    ## other stuff
end