I've seen some discussions already in the mailing list about sending
Mass Emails in Rails, but they've been pretty unsatisfying.
I'm working with a non-profit with a low budget. They'd like to save
money on email communication by building a tool that will allow them
to send out graphic emails through their site. The hitch is that the
web host has an SMTP throttling limit of 50 recipients/blast and 500
recipients/hour. Their mailing list is just over 500 recipients.
Someone suggested I use a combination of BackgrounDrb and ar_mailer to
feed the emails into a database and cycle through a portion of them
every few minutes. But I'm an intermediate Rails developer and I
couldn't make heads or tails of some the documentation on those two
tools -- especially ar_mailer.
Can anybody provide a better solution or a tutorial/walkthrough on
using BackgrounDrb and ar_mailer together?
P.S. The answer is not to BCC everyone on the mailing list.
I’ve successfully implemented ar_mailer, and it works well. All emails get queued into a database table that looks like this:
create_table :emails do |t|
t.column :from, :string
t.column :to, :string
t.column :last_send_attempt, :integer, :default => 0
t.column :mail, :text
end
To get your emails going into this table, you subclass your mailer classes from ARMailer, like this:
class NotificationMailer < ActionMailer::ARMailer
end
Now, when you tell your rails app to deliver an email, it actually just gets queued into that table.
Now you need some sort of background process to check this table periodically. I handled this by making a little rake task to send out the emails from the queue, like this:
desc “Send queued emails”
task :send_queued_emails => :environment do
Dir.chdir(RAILS_ROOT) # change working directory to rails app root so ar_sendmail will work correctly
ActionMailer::ARSendmail.run
([‘-o’])
end
Then I setup a little cron job to run every 15 minutes which runs the rake task and send out the emails…
class NotificationMailer < ActionMailer::ARMailer
end
Hi,
could you give some same of NotificationMailer class?
bec'z i have sample of email.rb is:
class Email < ActionMailer::ARMailer
def testmail
recipients "mymailaddress@domain.com"
from "fromaddress@domain.com"
subject "TEST MAIL SUBJECT"
body "<br>TEST MAIL MESSAGE"
content_type "text/html"
end
end
and i am sending it through controller like:
mail = Email.create_testmail # => a tmail object
Email.deliver(mail)
but error is occurring:
undefined method `create' for Email:Class
and when i run ar_sendmail -o, it throw error like:
Unhandled exception undefined method `destroy_all' for
Email:Class(NoMethodError):
it means it does not consider mail as active record.