Class: MailgunTool

Inherits:
Object
  • Object
show all
Defined in:
MailGunTool.rb

Instance Method Summary collapse

Constructor Details

#initialize(list, api_key, domain_name, of = 'out.csv') ⇒ MailgunTool

Returns a new instance of MailgunTool



4
5
6
7
8
9
10
11
12
13
14
# File 'MailGunTool.rb', line 4

def initialize( list, api_key, domain_name, of = 'out.csv')
	# init vars
@outfile = File.new( of, "a")
 @list_address = list
 @api_key = api_key
 @domain_name = domain_name

 @mg_client = Mailgun::Client.new @api_key
 @email_adressen = Array.new
@first_run = true
end

Instance Method Details

#add_list_members(f) ⇒ nil

Adds email Addresses read from txt file to mailing list.

Returns:

  • (nil)


77
78
79
80
81
82
83
# File 'MailGunTool.rb', line 77

def add_list_members(f)
  File.open f, 'r' do |file|
    file.each do |email|
      p @mg_client.post("/lists/#{@list_address}/members", :subscribed => true, :address => email.chomp, :upsert => true )
    end
  end
end

#del_bounces_from_listnil

Delete bounced email addresses

Returns:

  • (nil)


146
147
148
149
150
151
152
153
154
155
156
# File 'MailGunTool.rb', line 146

def del_bounces_from_list
  bounces = self.get_bounces
  bounces["items"].each do |item|
    if ( item["code"] == '554')
      puts item["address"]
      a =  item["address"].to_s
      puts "Deleting ", a
      @mg_client.delete("/members/#{a}")
    end
  end
end

#delete_list_members(f) ⇒ nil

Deletes email Addresses read from txt file from mailing list.

Returns:

  • (nil)


87
88
89
90
91
92
93
94
95
96
97
98
# File 'MailGunTool.rb', line 87

def delete_list_members(f)
  File.open f, 'r' do |file|
    file.each do |email|
      begin
        p @mg_client.delete("/lists/#{@list_address}/members/#{email.chomp}")
      rescue
        puts "--------\n#{email} Not found..NEXT\n"
        next
      end
    end
  end
end

#get_adresses(email = nil) ⇒ Array

Returns list of email Adresses.

Parameters:

  • email (String) (defaults to: nil)

Returns:

  • (Array)

    list of email Adresses.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'MailGunTool.rb', line 20

def get_adresses( email = nil )
 	if @first_run == true
  	result = @mg_client.get("lists/#{@list_address}/members/pages", {:subscribed => 'yes'})
  	@first_run = false
  else
  	unless email == nil
  		result = @mg_client.get("lists/#{@list_address}/members/pages", {:subscribed => 'yes', :address => email , :page => 'next'})
  	else 
  		return "No Email Adress ??"
  	end
  end

	my_hash = result.to_h
	self.extract_email_addresses(my_hash)
	
	# check if more
	unless my_hash['items'].length == 0
		email =  @email_adressen.last
		self.get_adresses(email)
	else
		"Fertig"
		return @email_adressen
	end


end

#get_bouncesArray

Fetches list of bounced Addresses.

Returns:

  • (Array)

    list of bounced email.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'MailGunTool.rb', line 50

def get_bounces
 	if @first_run == true
  	result = @mg_client.get("#{@domain_name}/bounces")
  	@first_run = false
  else
  	unless email == nil
  		result = @mg_client.get("#{@domain_name}/bounces", { :address => email , :page => 'next'})
  	else 
  		return "No Email Adress ??"
  	end
  end

	my_hash = result.to_h
	self.extract_email_addresses(my_hash)
	
	# check if more
	unless my_hash['items'].length == 0
		email =  @email_adressen.last
		self.get_adresses(email)
	else 
		p "------------ Fertig --------------"
		return @email_adressen
	end
end

#get_logsArray

Get logs of rejected OR failed recipients

Returns:

  • (Array)

    of email addresses



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'MailGunTool.rb', line 127

def get_logs
	em = Array.new()
   resp = JSON::parse( RestClient.get "https://api:#{@api_key}@api.mailgun.net/v3/#{@domain_name}/events",
		:params => {:"event" => 'rejected OR failed' } )

	unless resp['items'].length == 0
		resp["items"].each { |item| 
			puts item["recipient"]
			em.push( item["recipient"] )
		}

		return em
	else 
		p " No failed or rejected recipients ...."
	end
end

#send_messagenil

TODO:

email parameters

Send message

Returns:

  • (nil)


116
117
118
119
120
121
122
123
# File 'MailGunTool.rb', line 116

def send_message
  p @mg_client.post("#{@domain_name}/messages",
  :from => "<mail@example.com>",
  :to => @list_address,
  :subject => "TEST",
  :text => "TEST TEST TEST, %unsubscribe_url% ",
  :html => 'TEST TEST TEST, %unsubscribe_url% ' )
end

#send_scheduled_messagenil

TODO:

email parameters

Send scheduled message

Returns:

  • (nil)


103
104
105
106
107
108
109
110
111
# File 'MailGunTool.rb', line 103

def send_scheduled_message
  @mg_client.post("#{@domain_name}/messages",
  :from => "<mail@example.com>",
  :to => @list_address,
  :subject => "Toller Test",
  :text => "Testing some Mailgun awesomeness!  %mailing_list_unsubscribe_url% ",
  :html => "<h1>html supi test</h1>",
  "o:deliverytime" => "Mon, 10 Jun 2019 17:15:00 +0200")
end