Skip to content
This repository has been archived by the owner on Mar 23, 2021. It is now read-only.

Commit

Permalink
Add: full SendGrid integration with plain and HTML templates
Browse files Browse the repository at this point in the history
  • Loading branch information
seawolf committed Jun 13, 2017
1 parent 0e3402b commit 1c0eb5d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 21 deletions.
17 changes: 11 additions & 6 deletions app/mailers/contact_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ def contact_email message, name, email, subject
to = StaticContent.load("contact", "email")

mail = ::SendGridMailer.new(
"OddityAvenue E-Mail Robot <[email protected]>",
"[email protected]",
"OddityAvenue E-Mail Robot",
to,
"OddityAvenue Contact - #{@subject}",
@message
message_plain,
message_html
)
mail.send! if mail.valid?

Expand All @@ -24,10 +26,13 @@ def contact_email message, name, email, subject

private

def message_plain

template = File.read('./app/views/contact_mailer/contact_email.txt.erb')
puts output = ERB.new(template).result(binding)
def message_html
template = File.read('./app/views/contact_mailer/contact_email.html.erb')
return ERB.new(template).result(binding)
end

def message_plain
template = File.read('./app/views/contact_mailer/contact_email.txt.erb')
return ERB.new(template).result(binding)
end
end
6 changes: 3 additions & 3 deletions app/views/contact_mailer/contact_email.txt.erb
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
The following message was sent from the Contact page of <%= @env %> at <%= @time %>.

---------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------
From : <%= @from_name %> | <%=@from_email %>
Subject: <%= @subject %>
---------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------

<%= @message %>

---------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------

~ The OddityAvenue E-Mail Robot
39 changes: 27 additions & 12 deletions lib/send_grid_mailer.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
require 'sendgrid-ruby'

class SendGridMailer
attr_reader :valid, :from, :to, :subject, :body,
attr_reader :valid, :from_name, :from_email, :to, :subject, :body_plain, :body_html,
:response_code, :response_body, :response_headers

def initialize(from, to, subject, body)
@from, @to, @subject, @body = from, to, subject, body
def initialize(from_email, from_name, to, subject, body_plain, body_html)
@from_email, @from_name, @to, @subject, @body_plain, @body_html =
from_email, from_name, to, subject, body_plain, body_html
validate
end

Expand All @@ -15,7 +16,11 @@ def valid?

def send!
mail_obj = build_mail_obj
call_api(mail_obj)
Rails.logger.info "Sending email: #{mail_obj.pretty_inspect}"

results = call_api(mail_obj)
Rails.logger.error "SendGrid response:\n#{results.pretty_inspect}" unless success?
results
end

def success?
Expand All @@ -26,25 +31,35 @@ def success?

def validate
@valid = true
[ @from, @to, @subject, @body ].map do |attr|
[ @from_email, @to, @subject, @body_plain ].map do |attr|
@valid = false if attr.to_s.blank?
end
end

def build_mail_obj
SendGrid::Mail.new(
SendGrid::Email.new(email: @from),
@subject,
SendGrid::Email.new(email: @to),
SendGrid::Content.new(type: 'text/plain', value: @body)
)
obj = SendGrid::Mail.new
obj.from = SendGrid::Email.new(email: @from_email, name: @from_name)
obj.subject = @subject

personalization = SendGrid::Personalization.new
personalization.add_to SendGrid::Email.new(email: @to, name: @to)
obj.add_personalization personalization

obj.add_content SendGrid::Content.new(type: 'text/plain', value: @body_plain)
obj.add_content SendGrid::Content.new(type: 'text/html', value: @body_html)
obj
end

def call_api(mail)
api = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'] || 'NO_API_KEY_SET')
response = api.client.mail._('send').post(request_body: mail.to_json)
@response_code, @response_headers, @response_body =
response.status_code, response.headers, response.body
return success?
return {
success?: success?,
response_code: @response_code,
response_headers: @response_headers,
response_body: @response_body
}
end
end

0 comments on commit 1c0eb5d

Please sign in to comment.