Sha256: 5f6d2fe5c8b5b2e8f6dc8a26fbd0af305d415aeb8d01edc64061b5663fa1bb87

Contents?: true

Size: 1.52 KB

Versions: 1

Compression:

Stored size: 1.52 KB

Contents

require 'sendgrid-actionmailer/version'
require 'sendgrid-actionmailer/railtie' if defined? Rails

require 'tempfile'

require 'sendgrid-ruby'

module SendGridActionMailer
  class DeliveryMethod
    def initialize(params)
      @client = SendGrid::Client.new do |c|
        c.api_user = params[:api_user]
        c.api_key  = params[:api_key]
      end
    end

    def deliver!(mail)
      email = SendGrid::Mail.new do |m|
        m.to      = mail[:to].addresses
        m.from    = mail[:from]
        m.subject = mail[:subject]
      end

      # TODO: This is pretty ugly
      case mail.mime_type
      when 'text/plain'
        # Text
        email.text = mail.body.decoded
      when 'text/html'
        # HTML
        email.html = mail.body.decoded
      when 'multipart/alternative'
        # Text and HTML
        email.text = mail.text_part.decoded
        email.html = mail.html_part.decoded
      when 'multipart/mixed'
        # Text and/or HTML and Attachment
        if mail.text_part.nil?
          email.html = mail.html_part.decoded
        elsif mail.html_part.nil?
          email.text = mail.text_part.decoded
        else
          email.text = mail.text_part.decoded
          email.html = mail.html_part.decoded
        end

        # This needs to be done better
        mail.attachments.each do |a|
          t = Tempfile.new("sendgrid-actionmailer#{rand(1000)}")
          t.binmode
          t.write(a.read)
          email.add_attachment(t, a.filename)
        end

      end
      @client.send(email)
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sendgrid-actionmailer-0.0.5 lib/sendgrid-actionmailer.rb