icecreamrabbit
icecreamrabbit

Reputation: 207

Email won't send for contact form?

I'm trying to implement a contact form. I don't get any errors, and after I fill out the form it gives the success alert, except the message isn't sending. I assume this has something to do with the way I'm setting up the SMTP? I used the MailForm gem and that's it.

Form view:

<div class="container">
<h1>Contact</h1>
 <%= form_for @contact do |f| %>
    <%= f.text_field  :name, placeholder: "Name", :required => true %><br>
    <%= f.text_field  :email, placeholder: "Email", :required => true %><br>
    <%= f.text_area :message, placeholder: "Message", :as => :text, :required => true %>
    <div class= "hidden">
      <%= f.text_field  :nickname, :hint => 'Leave this field blank!' %>
    </div>
    <div>
      </br>
      <%= f.submit 'Send', :class=> "btn btn-primary" %>
    </div>
  <% end %>

  </div>

Controller:

class ContactsController < ApplicationController
  def new
    @contact = Contact.new
  end

  def create
  @contact = Contact.new(params[:contact])
    @contact.request = request
    if @contact.deliver
      flash.now[:notice] = 'Thank you for your message. I will get back to you shortly!'
    else
      flash.now[:error] = 'Cannot send message.'
      render :new
    end
  end

  private

 def contact_params
   params.require(:contact).permit(:name, :email, :comments)
 end

  end

Model:

class Contact < MailForm::Base
  attribute :name,      :validate => true
  attribute :email,     :validate => /\A([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})\z/i
  attribute :message
  attribute :nickname,  :captcha  => true

  # Declare the e-mail headers. It accepts anything the mail method
  # in ActionMailer accepts.
  def headers
    {
      :subject => "Enquiry",
      :to => "[email protected]",
      :from => %("#{name}" <#{email}>)
    }
  end
end

Config > Environments > development.rb:

  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
  :user_name => '3878643a38ed2536d',
  :password => '1e4c3e5ef5e1ca',
  :address => 'mailtrap.io',
  :domain => 'mailtrap.io',
  :port => '2525',
  :authentication => :cram_md5
}

config > environment.rb

# Load the Rails application.
require File.expand_path('../application', __FILE__)

# Initialize the Rails application.
Rails.application.initialize!

ActionMailer::Base.delivery_method = :smtp

Thank you for your help!

Upvotes: 0

Views: 103

Answers (1)

Zenithian
Zenithian

Reputation: 82

Do you have this on a hosted server? If not that is why, most normally unless you set it up, you cant send emails from your website. I cant think of the proper way to phrase that but im sure you get the idea. If someone would improve what im saying.

Upvotes: 1

Related Questions