class Tolliver::Services::Methods::Email::Mailgun

Public Class Methods

new(params = {}) click to toggle source
# File lib/tolliver/services/methods/email/mailgun.rb, line 18
def initialize(params = {})
  require 'mailgun-ruby'
  if params[:api_key].blank? || params[:domain].blank?
    raise Tolliver::Errors::StandardError.new('Please provide API key and domain in e-mail provider params.')
  end
  @client = ::Mailgun::Client.new(params[:api_key])
  @domain = params[:domain]
end

Public Instance Methods

deliver(notification, notification_receiver) click to toggle source
# File lib/tolliver/services/methods/email/mailgun.rb, line 27
def deliver(notification, notification_receiver)

  # Sender
  raise Tolliver::Errors::StandardError.new("Please specify e-mail sender.") if Tolliver.email_sender.nil?

  # Build message
  message = ::Mailgun::MessageBuilder.new
  message.from(Tolliver.email_sender, {'full_name' => Tolliver.email_sender_name})
  message.add_recipient(:to, notification_receiver.receiver_email.to_s)
  message.reply_to(notification_receiver.notification_delivery.sender_email.to_s) unless notification_receiver.notification_delivery.sender_email.blank?
  message.subject(notification.subject)
  message.body_text(ActionController::Base.helpers.strip_tags(notification.message.to_s))
  message.body_html(notification.message)
  notification.notification_attachments.each do |notification_attachment|
    message.add_attachment(StringIO.new(notification_attachment.read), notification_attachment.name) if notification_attachment.read
  end

  # Request API
  response = @client.send_message(@domain, message)
  if response.code != 200
    raise Tolliver::Errors::StandardError.new(response.body)
  end

  true
end