Created
November 16, 2019 15:38
-
-
Save dangerrg/358f763363cf1b4bd774e8f09ce165ab to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MessageMailer < ApplicationMailer | |
require 'sendgrid-ruby' | |
include SendGrid | |
def contact(message) | |
@message = message | |
from = Email.new(email: '[email protected]') | |
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