class Hubspot::Mailer

Constants

SINGLE_SEND_PATH

Public Class Methods

new() click to toggle source
Calls superclass method
# File lib/hubspot/mailer.rb, line 127
def initialize
  super()
  @_mail_was_called = false
  @_message         = Message.new
end
single_send(mail) click to toggle source

Perform the delivery by calling Hubspot's API

Receives regular mail object that contains additional template details.

# File lib/hubspot/mailer.rb, line 37
def single_send(mail)
  # Format the request data
  data = single_send_params(mail)

  # Call the API
  response = Hubspot::Connection.post_json(SINGLE_SEND_PATH, params: {}, body: data)

  # Parse response and either raise or return event details
  parse_response(response)
end

Private Class Methods

parse_response(response) click to toggle source
# File lib/hubspot/mailer.rb, line 103
def parse_response(response)
  status_code = response["sendResult"]

  case status_code
    when "SENT", "QUEUED"
      response["eventId"]
    when "INVALID_TO_ADDRESS"
      raise RecipientAddressError.new(response), "The TO address is invalid: #{status_code}"
    when "INVALID_FROM_ADDRESS"
      raise SenderAddressError.new(response), "The FROM address is invalid: #{status_code}"
    when "BLOCKED_DOMAIN", "PORTAL_SUSPENDED"
      raise SendingError.new(response), "Message can't be sent: #{status_code}"
    when "PREVIOUSLY_BOUNCED", "PREVIOUS_SPAM"
      raise DeliveryError.new(response), "Message can't be delivered: #{status_code}"
    when "MISSING_CONTENT"
      raise InvalidTemplateError.new(response), "The emailId is invalid, or the emailId is an email that is not set up for Single Send: #{status_code}"
    else
      raise UnknownResponseError.new(response), "Unrecognized status code: #{status_code}"
  end
end
set_payload_for_mail(payload, mail) click to toggle source
# File lib/hubspot/mailer.rb, line 50
def set_payload_for_mail(payload, mail)
  payload[:mailer]     = name
  payload[:message_id] = mail.message_id
  payload[:subject]    = mail.subject
  payload[:to]         = mail.to
  payload[:from]       = mail.from     if mail.from.present?
  payload[:send_id]    = mail.send_id  if mail.send_id.present?
  payload[:reply_to]   = mail.reply_to if mail.reply_to.present?
  payload[:cc]         = mail.cc       if mail.cc.present?
  payload[:bcc]        = mail.bcc      if mail.bcc.present?
end
single_send_params(mail) click to toggle source
# File lib/hubspot/mailer.rb, line 62
def single_send_params(mail)
  raise MissingTemplateError, "Missing emailId parameter." unless mail.email_id.present?
  raise MissingRecipientError, "Missing recipient emaul."  unless mail.to.present?

  data = {
    emailId: mail.email_id,
    message: { to: mail.to.first }
  }

  data[:message][:from]   = mail.from.first if mail.from.present?
  data[:message][:cc]     = mail.cc         if mail.cc.present?
  data[:message][:bcc]    = mail.bcc        if mail.bcc.present?
  data[:message][:sendId] = mail.send_id    if mail.send_id.present?

  if mail.reply_to.present?
    if mail.reply_to.size > 1
      data[:message][:replyToList] = mail.reply_to
    else
      data[:message][:replyTo] = mail.reply_to.first
    end
  end

  # Copy subject from header to custom property
  if mail.subject.present? and not mail.custom_properties.try(:[], :subject)
    mail.custom_properties ||= {}
    mail.custom_properties[:subject] = mail.subject
  end

  if mail.contact_properties.present?
    data[:contactProperties] =
      Hubspot::Utils.hash_to_properties(mail.contact_properties, :key_name => :name)
  end

  if mail.custom_properties.present?
    data[:customProperties] =
      Hubspot::Utils.hash_to_properties(mail.custom_properties, :key_name => :name)
  end

  data
end

Public Instance Methods

mail(headers = {}, &block) click to toggle source
# File lib/hubspot/mailer.rb, line 146
def mail(headers = {}, &block)
  return message if @_mail_was_called && headers.blank? && !block

  headers = apply_defaults(headers)

  # Set configure delivery behavior
  wrap_delivery_behavior!(headers[:delivery_method], headers[:delivery_method_options])

  # Hubspot-specific attributes, e.g. emailId (template ID)
  assign_attributes_to_message(message, headers)

  # Default headers
  assign_headers_to_message(message, headers)

  @_mail_was_called = true

  message
end

Private Instance Methods

assign_attributes_to_message(message, headers) click to toggle source
# File lib/hubspot/mailer.rb, line 167
def assign_attributes_to_message(message, headers)
  hubspot_props = %i[email_id send_id contact_properties custom_properties]

  headers.slice(*hubspot_props).each { |k, v| message.try("#{k}=", v) }
  headers.except!(*hubspot_props)
end
assign_headers_to_message(message, headers) click to toggle source
# File lib/hubspot/mailer.rb, line 174
def assign_headers_to_message(message, headers)
  headers.except(:parts_order, :content_type, :body, :template_name,
    :template_path, :delivery_method, :delivery_method_options).each { |k, v| message[k] = v }
end