Class: VisitWidgetEmail::Commands::SendDynamicTemplateEmail
- Inherits:
-
Object
- Object
- VisitWidgetEmail::Commands::SendDynamicTemplateEmail
- Includes:
- SendGrid
- Defined in:
- lib/visit_widget_email/commands/send_dynamic_template_email.rb
Instance Method Summary collapse
- #execute ⇒ Object
-
#initialize(template_id, to, from = nil, dynamic_data = {}, locale = "en", attachments = []) ⇒ SendDynamicTemplateEmail
constructor
A new instance of SendDynamicTemplateEmail.
Constructor Details
#initialize(template_id, to, from = nil, dynamic_data = {}, locale = "en", attachments = []) ⇒ SendDynamicTemplateEmail
Returns a new instance of SendDynamicTemplateEmail.
8 9 10 11 12 13 14 15 |
# File 'lib/visit_widget_email/commands/send_dynamic_template_email.rb', line 8 def initialize(template_id, to, from = nil, dynamic_data = {}, locale = "en", =[]) @template_id = template_id @to = to @from = from @dynamic_data = dynamic_data @locale = locale @attachments = end |
Instance Method Details
#execute ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/visit_widget_email/commands/send_dynamic_template_email.rb', line 17 def execute validate() data = { "personalizations": [ { "to": [ { "email": @to } ], "dynamic_template_data": @dynamic_data } ], "template_id": @template_id } if @from.present? data[:from] = { "email": @from } end if @attachments.present? data[:attachments] = @attachments end send_grid = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY']) response = send_grid.client.mail._("send").post(request_body: data) = "none" if (response.body != "") error = response.parsed_body[:errors].length > 0 ? response.parsed_body[:errors][0] : nil = error.nil? ? "" : error[:message] end # issue in certain environments with the api key, include small partial to verify key used Rails.logger.info("Email response - to: #{@to}, status: #{response.status_code}, "\ "error message: #{}, key: #{ENV['SENDGRID_API_KEY'][0...8]}, "\ "from: #{@from}, template_id: #{@template_id}") return response end |