Class: MailCannon::Envelope

Inherits:
Object
  • Object
show all
Includes:
Adapter::SendgridWeb, Mongoid::Document, Mongoid::Timestamps
Defined in:
lib/mailcannon/envelope.rb

Overview

Where the magic happens, the Envelope is responsible for keeping the information necessary to send the email(s) and holding the Stamps related to mailing Events.

Instance Method Summary collapse

Instance Method Details

#after_sent(response) ⇒ Object

Callback to be run after the Envelope has been processed.



52
53
54
55
56
57
58
59
60
# File 'lib/mailcannon/envelope.rb', line 52

def after_sent(response)
  if response
    stamp!(MailCannon::Event::Processed.stamp)
    if MailCannon.config['auto_destroy']
      self.mail.destroy
      self.mail=nil # to avoid reload
    end
  end
end

#post_envelope!Object Also known as: post!

Post this Envelope!



29
30
31
32
33
34
35
36
37
38
# File 'lib/mailcannon/envelope.rb', line 29

def post_envelope!
  self.save if self.changed?
  raise "Envelope(#{self.id}) has no mail! Didn't you already send it?" unless self.mail
  if validate_xsmtpapi(self.xsmtpapi)
    jid = schedule_send_job
    self.save if self.changed?
  else
    raise 'Invalid xsmtpapi hash!'
  end
end

#posted?Boolean

Returns:

  • (Boolean)


62
63
64
65
66
67
68
# File 'lib/mailcannon/envelope.rb', line 62

def posted?
  if self.stamps.where(code: 0).count > 0
    true
  else
    false
  end
end

#processed?Boolean

Returns:

  • (Boolean)


70
71
72
73
74
75
76
# File 'lib/mailcannon/envelope.rb', line 70

def processed?
  if self.stamps.where(code: 1).count > 0
    true
  else
    false
  end
end

#stamp!(code, recipient = nil) ⇒ Object

Stamp this Envelope with code.



42
43
44
45
46
47
48
49
# File 'lib/mailcannon/envelope.rb', line 42

def stamp!(code,recipient=nil)
  self.class.valid_code_kind?(code)
  unless self.persisted?
    logger.warn "You're trying to save the Stamp with an unsaved Envelope! Auto-saving Envelope."
    self.save
  end
  self.stamps.create(code: MailCannon::Stamp.from_code(code).code, recipient: recipient)
end