ruby-on-railsherokusendgridsendgrid-ruby

Rails 5 contact Me form not sending email using sendgrid via heroku


I've been following this blog post regards creating a 'contact me' form and mailer. I've got far enough that my form displays Messaged Received but I never receive an email, not in Span folder either. I have sendgrid working fine with Devise for user authentication so I know that aspect is working.

Messages/new.html.erb

<%= form_for @message, url: create_message_url do |f| %>
  <%= notice %>
  <%= @message.errors.full_messages.join(', ') %>

  <%= f.text_field :name, placeholder: 'name' %>
  <%= f.email_field :email, placeholder: 'email' %>
  <%= f.text_area :body, placeholder: 'body' %>
  <%= f.submit 'Send' %>
<% end %>

controllers/messages_controller.rb

class MessagesController < ApplicationController

def new
  @message = Message.new
end

def create
  @message = Message.new message_params

  if @message.valid?
    redirect_to new_message_url, notice: "Message received, thanks!"
  else
    render :new
  end
end

private

def message_params
  params.require(:message).permit(:name, :email, :body)
end

end

mailers/message_mailer.rb

class MessageMailer < ApplicationMailer
  def contact_me(message)
    @body = message.body

    mail to: "<mydomainemail>", from: message.email
  end
end

Testing throws no errors, and I can also preview the email ok.

Anything else I'm missing?


Solution

  • It does not look to me like you are actually triggering the send event in your controller. If I am understanding your process correctly your create method should have the .deliver method on the mailer and message.

    def create
      @message = Message.new message_params
    
      if @message.valid?
        MessageMailer.contact_me(@message).deliver <-- This
        redirect_to new_message_url, notice: "Message received, thanks!"
      else
        render :new
      end
    end