Skip to content

Instantly share code, notes, and snippets.

@dangerrg
Created November 16, 2019 15:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dangerrg/358f763363cf1b4bd774e8f09ce165ab to your computer and use it in GitHub Desktop.
Save dangerrg/358f763363cf1b4bd774e8f09ce165ab to your computer and use it in GitHub Desktop.
class MessagesController < ApplicationController
def new
@message = Message.new
end
def create
@message = Message.new(message_params)
respond_to do |format|
if @message.valid?
MessageMailer.contact(@message).deliver_now
format.html { redirect_to new_message_url, notice: 'We have received your message and will be in touch soon!' }
else
format.html { render :new, notice: 'Ops.. there was an error sending your message. Please try again.' }
end
end
end
private
def message_params
params.require(:message).permit(:name, :email, :phone_number, :body)
end
end
class MessageMailer < ApplicationMailer
require 'sendgrid-ruby'
include SendGrid
def contact(message)
@message = message
from = Email.new(email: 'no-reply@casineros-cubanos.com')
to = Email.new(email: @message.email)
subject = 'casineros cubanos Blog'
content = Content.new(type: 'text/plain', value: "Hi dear #{@message.name}! Thanks for your massage, we will get back to you soon.")
mail = SendGrid::Mail.new(from, subject, to, content)
sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'])
begin # to read error messages
response = sg.client.mail._("send").post(request_body: mail.to_json)
rescue Exception => e
puts e.message
end
puts response.status_code
puts response.body
puts response.headers
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment