mailgun-helper/MailGunTool.rb
2019-06-10 18:45:27 +02:00

170 lines
4.1 KiB
Ruby

require 'mailgun-ruby'
class MailgunTool
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
# Fetches list of subscribers.
#
# @param email [String]
# @return [Array] list of email Adresses.
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
# Fetches list of bounced Addresses.
#
# @return [Array] list of bounced email.
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
# Adds email Addresses read from txt file to mailing list.
# @return [nil]
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
# Deletes email Addresses read from txt file from mailing list.
# @return [nil]
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
# Send scheduled message
# @return [nil]
# @todo email parameters
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
# Send message
# @return [nil]
# @todo email parameters
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
# Get logs of rejected OR failed recipients
# @return [Array] of email addresses
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
# Delete bounced email addresses
# @return [nil]
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
protected
# Extracts email Adresses from server response
# @return [Array]
def extract_email_addresses(my_hash)
a = my_hash['items']
a.each{ |elem | @email_adressen.push(elem.fetch("address"))}
return @email_adressen
end
end