Last active
December 17, 2015 19:09
-
-
Save vderyagin/5658163 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class EmailSender | |
def self.send_emails(number_of_emails, job_que_id) | |
emails = EmailQue.where(send_status: 'NOT SENT', job_que_id: job_que_id) | |
emails = emails.limit(number_of_emails) unless number_of_emails.zero? | |
errors = [] | |
emails.each do |email| | |
email_params = { | |
from: 'email@example.com', # <== change this to appropriate value | |
to: email.to_field, | |
subject: email.subject_field, | |
body: email.body_field, | |
} | |
delivered = send_email(email_params) | |
status = delivered ? 'SENT' : 'ERROR' | |
email.update_attributes send_status: status | |
errors << "Failed to deliver email with id=#{email.id}." unless delivered | |
end | |
return errors unless errors.empty? | |
emails.size | |
end | |
# return true on successfull delivery, false otherwise | |
def self.send_email(params) | |
ActionMailer::Base.mail(params).deliver | |
rescue StandardError | |
false | |
else | |
true | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment