Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the best way to make a newsletter in rails3

Is there a gem to do this, and if not, what is the best approach. I'm assuming i'd store the emails in a newsletter database, and would want a form that emails everyone at once. thanks!

like image 777
montrealmike Avatar asked Feb 16 '11 15:02

montrealmike


2 Answers

No gem for this that I know of.

Actually, due to processing issues, the best way would be to use external bulk email service provider via provider's API.

However, building your own newsletter system is no different from building your regular MVC. Only addition are mailers and mailer views.

(1) So you create model that deals with registration data (:name, :email, :etc...) and model that deals with newsletter itself.

(2) Controller that deals with it (CRUD) + (:send). Send takes each recipient, sends data to mailer which creates email and then sends it.

def send
 @newsletter = Newsletter.find(:params['id'])
 @recipients = Recipient.all
 @recipients.each do |recipient|
   Newsletter.newsletter_email(recipient, @newsletter).deliver
 end
end

(3) Mailer builds an email, makes some changes in each email if you want to do something like that and returns it to controller action send for delivery.

class Newsletter < ActionMailer::Base
  default :from => "[email protected]", :content_type => "multipart/mixed"

  def newsletter_email(recipient, newsletter)
    # these are instance variables for newsletter view
    @newsletter = newsletter
    @recipient = recipient
    mail(:to => recipient.email, :subject => newsletter.subject)
  end
end

(4) Ofc, you need mailer views which are just like regular views. Well in multipart there is:

  • newsletter_email.html.erb (or haml)
  • newsletter_email.txt.erb

    When you want to send both html and text only emails. Instance variables are defined in mailer ofc.

And... that is it. Well you could use delayed job to send them since sending more than several hundred emails can take a while. Several ms times n can be a lot of time.

Have fun.

like image 83
Krule Avatar answered Sep 23 '22 21:09

Krule


Please check the maktoub gem there is a blog post over it.

like image 42
onurozgurozkan Avatar answered Sep 21 '22 21:09

onurozgurozkan