Skip invalid emails when sending newsletter
We were seeing an exception when sending some emails due to having invalid format With this commit we are skipping this invalid formatted emails when sending the newsletter
This commit is contained in:
@@ -20,9 +20,41 @@ class Newsletter < ActiveRecord::Base
|
||||
sent_at.nil?
|
||||
end
|
||||
|
||||
def deliver
|
||||
run_at = first_batch_run_at
|
||||
list_of_recipient_emails_in_batches.each do |recipient_emails|
|
||||
recipient_emails.each do |recipient_email|
|
||||
Mailer.delay(run_at: run_at).newsletter(self, recipient_email)
|
||||
if valid_email?(recipient_email)
|
||||
end
|
||||
end
|
||||
run_at += batch_interval
|
||||
end
|
||||
end
|
||||
|
||||
def batch_size
|
||||
10000
|
||||
end
|
||||
|
||||
def batch_interval
|
||||
20.minutes
|
||||
end
|
||||
|
||||
def first_batch_run_at
|
||||
Time.current
|
||||
end
|
||||
|
||||
def list_of_recipient_emails_in_batches
|
||||
list_of_recipient_emails.in_groups_of(batch_size, false)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def validate_segment_recipient
|
||||
errors.add(:segment_recipient, :invalid) unless valid_segment_recipient?
|
||||
end
|
||||
|
||||
def valid_email?(email)
|
||||
email.match(/\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i)
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user