sending an email using mail_form freeze with no errors rails

157 Views Asked by At

I have contact us form and im working in the development env with localhost, user fills name, email, and message and clicks on send message button and then i receive an email.

I implemented the solution using

Ruby v 2.4.1

Rails v 5.1.4

mail_form

I followed the steps on the below link https://gist.github.com/stevecondylios/e819a296167a31578d82fac881963789

Before this functionality was working for some email and now does not work at all, I dont get an error and no log shows in the email provider which is mailgun

here is the last logs that i get

Processing by HomepageController#create as JS
  Parameters: {"utf8"=>"✓", "homepage"=>{"name"=>"AMER Y BEARAT", "email"=>"[email protected]", "message"=>"klkllkklk", "nickname"=>""}, "commit"=>"Send Message"}
--->>> 1 create contact
--->>> 2 create contact
Test: {
  "name": "AMER Y BEARAT",
  "email": "[email protected]",
  "message": "klkllkklk",
  "nickname": ""
}
--->>> 3 create contact
--->>> 4 create contact
Started POST "/homepage" for ::1 at 2021-03-07 11:42:32 -0700
Processing by HomepageController#create as JS
  Parameters: {"utf8"=>"✓", "homepage"=>{"name"=>"AMER Y BEARAT", "email"=>"[email protected]", "message"=>"klkllkklk", "nickname"=>""}, "commit"=>"Send Message"}
--->>> 1 create contact
--->>> 2 create contact
Test: {
  "name": "AMER Y BEARAT",
  "email": "[email protected]",
  "message": "klkllkklk",
  "nickname": ""
}
--->>> 3 create contact
--->>> 4 create contact

\logs are coming from this controller homepage_controller.rb file

app/controllers/homepage_controller.rb

class HomepageController < ApplicationController
    def index
        @contact = Homepage.new(params[:homepage])
    end
    def create
        puts "--->>> 1 create contact"
        @contact = Homepage.new(params[:homepage])
        puts "--->>> 2 create contact"
        puts "Test: " + JSON.pretty_generate(JSON.parse(@contact.to_json))
        @contact.request = request
        puts "--->>> 3 create contact"
        respond_to do |format|
          puts "--->>> 4 create contact"
          if @contact.deliver
            puts "--->>> Pass create contact"
            # re-initialize Home object for cleared form
            @contact = Homepage.new
            format.html { render 'index'}
            format.js   { flash.now[:success] = @message = "Thank you for your message. I'll get back to you soon!" }
          else
            puts "--->>> Fails create contact"
            format.html { render 'index' }
            format.js   { flash.now[:error] = @message = "Message did not send." }
          end
        end
      end
end

this is partial view app/views/homepage/_contact_form.html.erb

<%= form_for @contact, url: homepage_index_path, remote: true do |f| %>
    <div class="contact-form-container">
        <div>
            <%= f.label :name %></br>
            <%= f.text_field  :name, required: true, class: "contact-form-text-area" %>
        </div>
        <div>
            <%= f.label :email %></br>
            <%= f.text_field :email, required: true, class: "contact-form-text-area" %>
        </div>
        <div>
            <%= f.label :message %></br>
            <%= f.text_area :message, rows: 8, cols: 40, required: true, class: "contact-form-text-area",
                            placeholder: "Send us a message"%>
        </div>
        <div class= "hidden">
            <%= f.label :nickname %>
            <%= f.text_field :nickname, :hint => 'Leave this field blank!' %>
        </div>
        <div class="btn-send-message">
            <%= f.submit 'Send Message', class: 'btn btn-send-message-primary' , remote: true %>
        </div>
    </div>
  <% end %>
  <div class="col-md-6" id="flash-message">
    <%= render 'flash' %>
  </div>

this is create.js.erb file app/views/homepage/create.js.erb

// Test for ajax success
console.log("This is the create.js.erb file");
// Render flash message
$('#contact').html("<%= j render 'contact_form' %>");
$('#flash-message').html("<%= j render 'flash' %>").delay(3000).fadeOut(4000);

here is development.rb file config/environments/development.rb

 config.action_mailer.perform_deliveries = true
  config.action_mailer.default_url_options = {  :host => 'localhost:3000'  }
  config.action_mailer.default :charset => "utf-8"
  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
    address: 'smtp.mailgun.org',
    port: 587,
    domain: '********.com',
    enable_starttls_auto: true,
    authentication: 'plain',
    user_name: 'postmaster@********.com',
    password: '***************************'
  }
  config.action_mailer.raise_delivery_errors = true

Note: other emails in the app works fine, so i dont think it's mailgun configuration issue

0

There are 0 best solutions below