csmu
(Keith John Hutchison - Ceiteach Seán Mac Úistin)
1
Our shared mailgun server is being blocked on Microsoft controlled domains. I want to send a message to those people in the group with Microsoft email addresses.
I’m looking for some ruby code to find all users within a group with email addresses within certain domains.
This happens to us sometimes, but usually clears up on its own as they deal with the offending sender(s). A few times I’ve asked Mailgun support to move us to a quieter IP address and they did.
2 Likes
csmu
(Keith John Hutchison - Ceiteach Seán Mac Úistin)
3
csmu
(Keith John Hutchison - Ceiteach Seán Mac Úistin)
5
This what I wrote to extract the email addresses for users with email addresses within a set of domains and the user belongs within a set of groups.
def extract_users_email_addresses_by_named_domains_and_group_names(domain_names=[],group_names=[],output_path=nil,verbose=false)
# TODO change SQL calls to use parameters
results = []
user_ids = []
group_ids = []
users_email_addresses = []
group_names_sql = 'select * from groups where '
group_names_where = ''
user_emails_sql = 'select user_id from user_emails where '
user_emails_where = ''
if domain_names.length > 0 and group_names.length > 0 then
domain_names.each do |domain_name|
if user_emails_where.length == 0 then
user_emails_where = sprintf("email like '%%%s%%'", domain_name)
else
user_emails_where = sprintf("%s or email like '%%%s%%'", user_emails_where, domain_name)
end
end
user_emails_sql = sprintf("%s %s",user_emails_sql,user_emails_where)
if verbose then
puts "UserEmail.find_by_sql(\"#{user_emails_sql}\")"
end
user_emails = UserEmail.find_by_sql(user_emails_sql)
user_emails.each do |user_email|
user_ids.push(user_email.user_id)
end
group_names.each do |group_name|
if group_names_where.length == 0 then
group_names_where = sprintf("name like '%%%s%%'", group_name)
else
group_names_where = sprintf("%s or name like '%%%s%%'", group_names_where, group_name)
end
end
group_names_sql = sprintf("%s %s",group_names_sql,group_names_where)
if verbose then
puts "Group.find_by_sql(\"#{group_names_sql}\")"
end
groups = Group.find_by_sql(group_names_sql)
groups.each do |group|
group_ids.push(group.id)
end
group_users = GroupUser.where(group_id:group_ids.to_set,user_id:user_ids.to_set)
user_ids = []
group_users.each do |group_user|
user_ids.push(group_user.user_id)
end
users = User.where(id:user_ids.to_set)
users.each do |user|
users_email_addresses.push(sprintf("%s", user.email))
end
if output_path then
file = File.open(output_path,"w")
first = true
users_email_addresses.each do |users_email_address|
if first then
first = false
file.write(users_email_address)
else
file.write(sprintf(",%s",users_email_address))
end
end
file.write("\n")
file.close()
end
end
return {group_ids:group_ids.to_set,user_ids:user_ids.to_set,users_email_addresses:users_email_addresses}
end
domain_names = ['outlook.com','hotmail','live.com']
group_names = ['irish-club-members']
output_path = 'irish-club-microsoft-email-list.txt'
verbose = false
extract_users_email_addresses_by_named_domains_and_group_names(domain_names=domain_names,group_names=group_names,output_path=output_path,verbose=verbose)