class Inkcite::Mailer

Constants

FIRST_PREVIEW

Name of the distribution list used on the first preview. For one client, they wanted the first preview sent to additional people but subsequent previews went to a shorter list.

Public Class Methods

send_test(email, test_address, opts) click to toggle source
# File lib/inkcite/mailer.rb, line 7
def self.send_test email, test_address, opts

  # Determine the number of times we've emailed to this list
  count = get_count(email, :test, opts)

  # Check to see if a specific version is requested or if unspecified
  # all versions of the email should be sent.
  versions = get_versions(email, opts)

  # Will hold the instance of the Mailer::Base that will handle the
  # actual sending of the email.
  mailer_base = get_mailer_base(email)

  versions.each do |version|

    # The version of the email we will be sending.
    view = email.view(:preview, :email, version)

    subject = "#{view.subject} (Test ##{count})"
    print "Sending '#{subject}' ... "

    mailer_base.send!({ :to => test_address }, subject, view.render!)

    puts 'Sent!'

  end

  # Increment the count now that we've successfully emailed this list
  increment email, :test

end
send_to_list(email, list, opts) click to toggle source
# File lib/inkcite/mailer.rb, line 39
def self.send_to_list email, list, opts

  # Determine the number of times we've emailed to this list
  count = get_count(email, list, opts)

  # Check to see if a specific version is requested or if unspecified
  # all versions of the email should be sent.
  versions = get_versions(email, opts)

  # Will hold the instance of the Mailer::Base that will handle the
  # actual sending of the email.
  mailer_base = get_mailer_base(email)

  # Get the email address from which the previews will be sent.
  from = mailer_base.get_from_address

  versions.each do |version|

    # The version of the email we will be sending.
    view = email.view(:preview, :email, version)

    # Subject line tag such as "Preview #3"
    tag = "#{opts[:tag]} ##{count}"

    subject = view.subject
    subject = "#{subject} (#{tag})" unless tag.blank?

    # Get the list of recipients for this version
    recipients = get_recipients(list, view, count, from)

    # Get the total number of recipients for this version
    total_recipients = recipients.inject(0) { |total, (k, v)| total + v.length }

    print "Sending '#{subject}' to #{total_recipients} recipient#{'s' if total_recipients > 1} ... "

    mailer_base.send! recipients, subject, view.render!

    puts 'Sent!'

  end

  # Increment the count now that we've successfully emailed this list
  increment email, list

end

Private Class Methods

comma_set_includes?(_set, value) click to toggle source
# File lib/inkcite/mailer.rb, line 92
def self.comma_set_includes? _set, value
  _set.blank? || _set.split(',').collect(&:to_sym).include?(value.to_sym)
end
get_count(email, sym, opts) click to toggle source
# File lib/inkcite/mailer.rb, line 96
def self.get_count email, sym, opts
  opts[:count] || email.meta(sym).to_i + 1
end
get_mailer_base(email) click to toggle source
# File lib/inkcite/mailer.rb, line 100
    def self.get_mailer_base email
      mailer_base = nil

      # Check to see if
      if config = email.config[:mailgun]
        mailer_base = MailgunMailer.new(config)
      elsif config = email.config[:smtp]
        mailer_base = SmtpMailer.new(config)
      else
        abort <<-USAGE.strip_heredoc

                Oops! Inkcite can't send this email because of a configuration problem.
                Please update the mailgun or smtp sections of your config.yml file.

                  smtp:
                    host: 'smtp.gmail.com'
                    port: 587
                    domain: 'yourdomain.com'
                    username: ''
                    password: ''
                    from: 'Your Name <email@domain.com>'

                Or send via Mailgun:

                  mailgun:
                    api-key: 'key-your-api-key'
                    domain: 'mg.sending-domain.com'
                    from: 'Your Name <email@domain.com>'

        USAGE
      end

      mailer_base
    end
get_recipients(list, view, count, from) click to toggle source
# File lib/inkcite/mailer.rb, line 135
def self.get_recipients list, view, count, from

  recipients = { :to => [], :cc => [], :bcc => [] }

  # Developer list always only sends to the original from address.
  if list == :developer
    recipients[:to] << from

  else

    # Always bcc the developer of the email
    recipients[:bcc] << from

    # Check to see if there is a TSV file which allows for maximum
    # configurability of the recipient list.
    recipients_file = view.email.project_file('recipients.tsv')
    if File.exist?(recipients_file)

      # Iterate through the recipients file and determine which entries match the
      # list, version and preview count...
      Util.each_line(recipients_file, false) do |line|

        # Skip comments
        next if line.start_with?('#')

        name, email, _list, delivery, min_preview, max_preview, versions = line.split("\t")
        next if name.blank? || email.blank?

        # Skip this recipient unless the distribution list matches the one
        # we're looking for.
        next unless _list.to_sym == list

        # Skip this recipient unless we've reached the minimum number of
        # earlier previews for this recipient - e.g. they only receive the
        # 2nd previews and beyond
        min_preview = min_preview.to_i
        next if min_preview > 0 && count < min_preview

        # Skip this recipient if we've already delivered the maximum number
        # of previews they should receive - e.g. they only receive the
        # first preview, no additional previews.
        max_preview = max_preview.to_i
        next if max_preview > 0 && count > max_preview

        # Skip this recipient unless
        next unless comma_set_includes?(versions, view.version)

        delivery = delivery.blank? ? :to : delivery.to_sym
        recipient = "#{name} <#{email}>"
        recipients[delivery] << recipient

      end

    else

      # Grab the array of recipients from the config.yml
      recipient_yml = view[:recipients]

      case list
        when :client
          first_preview = count == 1 ? recipient_yml[FIRST_PREVIEW] : nil
          clients = recipient_yml[:clients] || recipient_yml[:client]
          recipients[:to] = first_preview || clients
          recipients[:cc] = recipient_yml[:internal]
        when :developer
          recipients[:to] = from
        else # internal or any custom list
          recipients[:to] = recipient_yml[list]
      end

    end

  end

  return recipients
end
get_versions(email, opts) click to toggle source
# File lib/inkcite/mailer.rb, line 212
def self.get_versions email, opts
  Array(opts[:version] || email.versions)
end
increment(email, sym) click to toggle source
# File lib/inkcite/mailer.rb, line 216
def self.increment email, sym
  email.set_meta sym, get_count(email, sym, {})
end