Skip to content

Instantly share code, notes, and snippets.

@EldonMcGuinness
Last active December 31, 2025 19:31
Show Gist options
  • Select an option

  • Save EldonMcGuinness/5b241d8c65f80d3a1c551eb76a3b6157 to your computer and use it in GitHub Desktop.

Select an option

Save EldonMcGuinness/5b241d8c65f80d3a1c551eb76a3b6157 to your computer and use it in GitHub Desktop.
Find tickets that are spam and write them to files
# the tag to look for, your spam should have this tag
include_tag = "spam"
exclude_tag = "ham"
# Where to store the eml files
# !!! Note that there is not a trailing slash, do not put one !!!
destination = "/opt/zammad/#{include_tag}_tickets"
# Make the dir if not exists
Dir.mkdir(destination) unless File.exist?(destination)
#find tickets with a spam tag
puts "Looking for #{include_tag}\r\n"
#for ticket in Ticket.all do
Ticket.find_each do |ticket|
# If the ticket is tagged appropriately
if ticket.tag_list.include?(include_tag) && !ticket.tag_list.include?(exclude_tag)
# Get the current time and calculate the look_back
current_time = Time.zone.now
look_back = current_time - 6.weeks
# Make sure the email is in the needed time frame
if ticket.created_at > look_back && ticket.created_at <= current_time
# Tick to show work being done
print '.'
begin
# Make sure the article is not nil
raw_article = ticket.articles.first.as_raw
if not raw_article.nil? then
# Write the email to an eml file for later use
File.open("#{destination}/zammad_ticket_#{ticket.id}.eml", "wb") do |file|
file.write("#{raw_article.content}")
end
end
rescue => e
# Let me know if there is an error, but don't die
puts "Error with ticket #{ticket.id}: #{e}"
end
end
end
end
puts ""
puts("Done!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment