Question

Am designing a simple rails 3 application, and having a post model

t.string :author
  t.string :title
  t.text :description  

what is to be achieved is, being able send emails to subscribers that is.. readers who are interested will be provided a form adding their email address, so that when a post is created, it sends an email to subscribers showing them the new post.... thanks, also kind of new to rails, please be quite descriptive

Was it helpful?

Solution

High Level Steps

  1. Create table to store email accounts
  2. Create form to collect email accounts
  3. Create mailer to distribute emails to subscribers

Step 1: Create Subscriber Table

rails g resource Subscriber email:string

Step 2: Create Form

In the controller action corresponding to the view that you want to collect emails on:

@subscriber = Subscriber.new

In your view where you want to collect the email addresses of your subscribers:

<% form_for(@subscriber) do |f| %>
  <%= f.text_field :email %>
  <%= f.submit %>
<% end %>

In your routes:

resources :subscribers

In app/controllers add the file subscribers_controller.rb and add the following (note that you will want to add additional logic to account for situations where the email is unable to be saved, like if you choose to put a validation on the Subscribers model to check the email is a valid format):

class SubscribersController < ApplicationController

  def create
    @subscriber = Subscriber.new(params[:subscriber])
    @subscriber.save
    redirect_to root_path
  end

end

Step 3: Mailer

See the docs at http://guides.rubyonrails.org/action_mailer_basics.html

It will go something like the following...

rails g mailer SubscriptionMailer

In mailers/subscription_mailer.rb add something like the following:

def send_email(email,post)    
  @post = post
  mail(to: email, subject: @post.title)
end

Find the folder app/views/subscription_mailer and add a file send_email.html.erb. This is the content that will be sent to your email subscribers.

In your development.rb file, set up the mailer. In the below instance, I used a tool called Mandrill: https://mandrillapp.com

  config.action_mailer.default_url_options = { :host => "localhost:3000" }

  config.action_mailer.smtp_settings = {
    address: "smtp.mandrillapp.com",
    port: 587,
    domain: "localhost:3000",
    authentication: "plain",
    enable_starttls_auto: true,
    user_name: ENV['email_username'],
    password: ENV['email_password']
  }

If you need help setting up ENV variables, check out the Figaro gem: https://github.com/laserlemon/figaro

Note that you can run the following method to send the email, given a subscriber and post:

SubscriptionMailer.send_email(@subscriber,@post).deliver

I would run the following from terminal to confirm your mailer is working correctly:

rails c
SubscriptionMailer.send_email("youremail@example.com",Post.last).deliver

If you get an email, then you know the mailer is working. The last step is to write the logic to fire the email. In this case, since you want to send the email after a post is created, you can make an after_create callback in the Post model.

In Post.rb:

after_create :send_email_to_subscribers

private

 def send_email_to_subscribers
  Subscriber.all.each do |subscriber|
   SubscriptionMailer.send_email(subscriber.email,self)
  end
 end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top