Class: IronMailer::SendEmail

Inherits:
Object
  • Object
show all
Defined in:
lib/iron_mailer/send_email.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ SendEmail

Returns a new instance of SendEmail.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/iron_mailer/send_email.rb', line 6

def initialize(options={})    
  @iron_mq_client = IronMQ::Client.new(options['iron_mq'])
  @queue = @iron_mq_client.queue(options['iron_mq']['queue_name'])
  @iron_cache_client = IronCache::Client.new(options['iron_cache'])
  
  @cache = @iron_cache_client.cache(options['iron_cache']['cache_name'])
  @debug = options['debug']

    if options['delivery_method'].is_a?(Hash)
      delivery_method_hash = options['delivery_method']
      @delivery_method=delivery_method_hash.keys.first.to_sym
      @delivery_method_settings = {}

      params = delivery_method_hash.values.first.clone

      params.keys.each do |key|
        @delivery_method_settings[key.to_sym] = params[key]
      end
      
    else
      @delivery_method=options['delivery_method'].to_sym          
      @delivery_method_settings={}
    end

end

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



4
5
6
# File 'lib/iron_mailer/send_email.rb', line 4

def cache
  @cache
end

#debugObject (readonly)

Returns the value of attribute debug.



4
5
6
# File 'lib/iron_mailer/send_email.rb', line 4

def debug
  @debug
end

#delivery_methodObject (readonly)

Returns the value of attribute delivery_method.



4
5
6
# File 'lib/iron_mailer/send_email.rb', line 4

def delivery_method
  @delivery_method
end

#delivery_method_settingsObject (readonly)

Returns the value of attribute delivery_method_settings.



4
5
6
# File 'lib/iron_mailer/send_email.rb', line 4

def delivery_method_settings
  @delivery_method_settings
end

#iron_cache_clientObject (readonly)

Returns the value of attribute iron_cache_client.



4
5
6
# File 'lib/iron_mailer/send_email.rb', line 4

def iron_cache_client
  @iron_cache_client
end

#iron_mq_clientObject (readonly)

Returns the value of attribute iron_mq_client.



4
5
6
# File 'lib/iron_mailer/send_email.rb', line 4

def iron_mq_client
  @iron_mq_client
end

#queueObject (readonly)

Returns the value of attribute queue.



4
5
6
# File 'lib/iron_mailer/send_email.rb', line 4

def queue
  @queue
end

Instance Method Details

#cache_email(mail) ⇒ Object



64
65
66
67
68
# File 'lib/iron_mailer/send_email.rb', line 64

def cache_email(mail)
  field = mail.header['X-SMTPAPI'].is_a?(Array) ? mail.header['X-SMTPAPI'].first : mail.header['X-SMTPAPI']
  cache_key = JSON.parse(field.value)['unique_args']['cache_key']
  self.cache.put(cache_key, self.extract_cache_info(mail).to_json)
end

#executeObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/iron_mailer/send_email.rb', line 70

def execute
  messages = self.queue.get(:n=>100)
  
  errors = []
  messages.each do |message|
    begin
      source_email = Mail.new(message.body)
      puts "Processing: #{source_email.subject}" if self.debug
      puts self.delivery_method=>self.delivery_method_settings if self.debug
      puts source_email if self.debug
      mails = self.split_email(source_email)

      mails.each do |mail|
        mail.delivery_method self.delivery_method, self.delivery_method_settings
        mail.deliver
        cache_email(mail)
      end
      message.delete
    rescue Exception=>e 
      if self.debug
        puts e.message 
        puts e.backtrace
      end
      errors << message
    end
  end
  errors.each do |message|
    puts "Releasing: #{message.id}" if self.debug
    message.release
  end

end

#extract_cache_info(mail) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/iron_mailer/send_email.rb', line 55

def extract_cache_info(mail)
  {'subject'=>mail.subject,
    'date'=>mail.date,
    'from'=>mail.from,
    'to'=>mail.to,
    'mail'=>mail.to_s
  }
end

#split_email(source_email) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/iron_mailer/send_email.rb', line 32

def split_email(source_email)
  puts "split_email: #{source_email}" 
  tos = source_email.to
  tos = [tos] if tos.is_a?(String)
  
  tos.uniq.map{|destination|
    duplicate = Mail.new(source_email)
    duplicate.to = destination
    
    if destination==tos.first
      duplicate.message_id=source_email.message_id
      duplicate.bcc=source_email.bcc
      duplicate.cc=source_email.cc
    else
      duplicate.message_id = nil
      duplicate.bcc = nil
      duplicate.cc = nil
    end
    duplicate.header['X-SMTPAPI']={unique_args: {cache_key: UUIDTools::UUID.random_create.to_s}}.to_json
    duplicate
  }
end