Question

How to get smtp to work in production droplet

Hi all, I’m trying to set up an email form that uses Google’s smtp servers and ActionMailer that sends emails on form submission. I should start out by saying that this works fine in development on my local machine and in production on Heroku, but not from my digital ocean droplet.

Basically, when I submit the form, it sits for 30 seconds and returns an internal server error. I suspect the problem is in ActionMailer because if I remove the ActionMailer code from my controller, the form submits as expected (but obviously sends no email).

Code:

routes.rb

  resources :home_page, only: :index
  match '/email' => 'home_page#send_email_form', as: :email_form, via: :post
  root :to => 'home_page#index'

home_page_controller.rb

def send_email_form
        @message = Message.new(params[:message])

        if @message.valid?
        ReferralMailer.message_confirmation(@message).deliver
        ReferralMailer.send_mail(@message).deliver
        respond_to do |format|
            format.html{redirect_to root_path}
            format.js
        end

        else
          respond_to do |format|
            format.html{redirect_to root_path}
            format.js
        end
        end
 end

_form.html.erb

<%= form_for @message, url: email_form_path, remote:true do |f| %>

    <%= f.label :name, class: 'label' %>
    <%= f.text_field :name, class: 'input', placeholder: "Enter your name" %>  
    <%= f.label :email, class: 'label' %>
    <%= f.text_field :email, class: 'input', placeholder: "Enter your email" %>
    <%= f.label :phone, class: 'label' %>  
    <%= f.text_field :phone, class: 'input', placeholder: "Enter your phone number"%>
    <%= f.label :message, class: 'label' %>
    <%= f.text_area :body, class: 'input', placeholder: "Enter a brief message"%>
    <%=f.submit 'Send Message', id:'submit', class: "margin-top-20 alt-button button radius large"  %>

<% end %>

client-side-javascript

$('#new_message').submit(function(e){

       if($(this).valid()){
           $('.form-header').addClass('submitting');
           $('#submit').prop('disabled', 'disabled');
           $('#spinner').show();
           var target = document.getElementById('spinner');
           var opts = {
               lines: 13, // The number of lines to draw
               length: 10, // The length of each line
               width: 10, // The line thickness
               radius: 20, // The radius of the inner circle
               corners: 1, // Corner roundness (0..1)
               rotate: 0, // The rotation offset
               direction: 1, // 1: clockwise, -1: counterclockwise
               color: '#000', // #rgb or #rrggbb or array of colors
               speed: 1, // Rounds per second
               trail: 60, // Afterglow percentage
               shadow: false, // Whether to render a shadow
               hwaccel: false, // Whether to use hardware acceleration
               className: 'spinner', // The CSS class to assign to the spinner
               zIndex: 2e9, // The z-index (defaults to 2000000000)
               top: '50%', // Top position relative to parent
               left: '50%' // Left position relative to5 parent
           };
           var spinner = new Spinner(opts).spin(target);


      }
      else{
       $(this).prev().prev().addClass('label-error');
       e.preventDefault();
     }
});

send_email_form.js.erb

$(document).ready(function(){
    var form=$('#new_message');  
    var form_header=$('.form-header');
    <% if @message.valid? %>
        form_header.removeClass('submitting');
        $('#spinner').hide();
        form.children().hide();
        form.append('<%= j render(:partial=>"confirmation")%>');
    <%end%>
});

Is there anything special I need to set up in unicorn or in nginx to make the AJAX request work? Also FWIW, if I remove the remote:true from my form_for tag, it doesn’t redirect either which leads to believe that the problem is in the ActionMailer somewhere.

Anyone have any ideas? Thanks so much!

Subscribe
Share

Submit an answer
You can type!ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Hello,

Things look ok here. Have you checked any logs to see for any errors? Such as the web server logs, likely in /var/log/nginx/error.log

Also, you should reach out to support by opening a ticket as mail ports can be blocked on some accounts and they can help you get that removed. It is possible this is also part of the cause since it seems to stall. This suggests a timeout, which could be the system trying to connect.