Class: Mailgun::Sendmail

Inherits:
Object
  • Object
show all
Defined in:
lib/mailgun-sendmail.rb,
lib/mailgun-sendmail/version.rb

Constant Summary collapse

VERSION =
"0.0.2"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key, domain) ⇒ Sendmail

Returns a new instance of Sendmail.



20
21
22
23
# File 'lib/mailgun-sendmail.rb', line 20

def initialize(api_key, domain)
  @api_key = api_key
  @domain = domain
end

Class Method Details

.mail(*args) ⇒ Object



14
15
16
17
18
# File 'lib/mailgun-sendmail.rb', line 14

def self.mail(*args)
  config = setup
  sendmail = Sendmail.new(config['api_key'], config['domain'])
  sendmail.mail(*args)
end

.setupObject



7
8
9
10
11
12
# File 'lib/mailgun-sendmail.rb', line 7

def self.setup
  Pit.get("mailgun-sendmail", :require => {
    "api_key" => "Your mailgun API Key",
    "domain" => "Your mailgun domain",
  })
end

Instance Method Details

#mail(mail_from, mail_to, title, body, attachment = nil) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/mailgun-sendmail.rb', line 25

def mail(mail_from, mail_to, title, body, attachment=nil)
  api_url  = "https://api:#@api_key@api.mailgun.net/v2/#@domain/messages"

  options = {
    :from => mail_from,
    :to => mail_to,
    :subject => title,
    :text => body,
  }
  options[:attachment] = File.new(attachment, "rb") if attachment

  RestClient.post(api_url, options)
end