purge old sendinblue mails

This commit is contained in:
Christophe Robillard 2021-08-14 12:46:56 +02:00
parent 602c71f94d
commit ddea0b9a45
2 changed files with 24 additions and 0 deletions

View file

@ -0,0 +1,9 @@
class Cron::PurgeOldSibMailsJob < Cron::CronJob
self.schedule_expression = "every day at midnight"
def perform
sib = Sendinblue::API.new
day_to_delete = (Time.zone.today - 31.days).strftime("%Y-%m-%d")
sib.delete_events(day_to_delete)
end
end

View file

@ -55,6 +55,21 @@ class Sendinblue::API
[]
end
def delete_events(day, opts = {})
client = ::SibApiV3Sdk::TransactionalEmailsApi.new
event_opts = { start_date: day, end_date: day, limit: 100 }.merge(opts)
while (events = client.get_email_event_report(event_opts).events).present?
message_ids = events.map(&:message_id).uniq
message_ids.each do |message_id|
client.smtp_log_message_id_delete(message_id)
end
end
true
rescue ::SibApiV3Sdk::ApiError => e
Rails.logger.error e.message
false
end
def unblock_user(email_address)
client = ::SibApiV3Sdk::TransactionalEmailsApi.new
client.smtp_blocked_contacts_email_delete(email_address)