开发者

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 开发者_如何学Pythondatabase, and would want a form that emails everyone at once. thanks!


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 => "my_email@example.com", :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.


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


No gem that I know of too and building on @Krule's answer, here's a screencast of setting up mailers in Rails.

How to create, preview and send email from your rails app

I was looking for something similar when I found this. I think with a bit of customization, it can easily be used to create newsletter emails too.

Save money! Spend somewhere else.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜