Created
March 28, 2011 14:03
-
-
Save cgallagher/890511 to your computer and use it in GitHub Desktop.
Send Emails from db to multiple users via Amazon SES
This file contains hidden or 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 MailshotController < ApplicationController | |
def init | |
due_emails = EmailQueue.find(:all, :conditions => ["scheduled_at < ? AND is_complete = ?", DateTime.now, false]) | |
for email in due_emails | |
users = get_users_by_locale(email.locale) | |
user_emails = get_user_emails(users) | |
options = { | |
:from => email.from_email_address, | |
:to => user_emails, | |
:subject => email.subject, | |
:aws_access_key => AWS_SES_ACCESS_KEY, | |
:aws_secret_key => AWS_SES_SECRET_KEY, | |
:template => File.new("app/views/emails/#{email.template}").read } | |
e = AmazonEmail.new(options) | |
res = e.send | |
email.update_attributes(:is_complete => true, :send_count => user_emails.length, :completed_at => DateTime.now) | |
end | |
end | |
private | |
def get_users_by_locale(locale) | |
users = User.find_all_by_locale(locale) | |
users | |
end | |
def get_user_emails(users) | |
user_emails = Array.new | |
for user in users | |
user_emails << user.email | |
end | |
user_emails | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment