I am trying to set something up in my app where a daily e-mail is sent out to all users with the news posts created for their account each day.
I already have the CRON task setup, but I don't know enough about ActionMailer to figure out how to send specific info to each user...
In my app, Users belong to accounts, accounts have many users, and users have many ideas and accounts have many ideas through users.
So if Account 1 has 3 users, Every day I want all three users to receive the new ideas in the last day via e-mail.
I know this is complicated, but if anyone can help with a starting point it would be greatly appreciated.
Cheers
edited to include code from model:
class Account < ActiveRecord::Base
has_many :users
has_many :ideas, :through => :users
class User < ActiveRecord::Base
belongs_to :account
has_many :ideas, :dependent => :destroy
I basically want to send this feed from the index page to each user in a daily e-mail:
<strong>Here are some of your colleagues recent ideas:</strong>
<% @ideas.each do |idea| %> #@ideas = Account.ideas
<% if idea.created_at > 4.hours.ago %>
<table class="microposts" summary="User microposts">
<tr>
<td class="micropost">
<span class="ideatitle"><%= idea.title %></span><br />
<span class="ideacontent"><%= idea.content %></span>
<span class="timestamp">
Conceived <%= time_ago_in_words(idea.created_at) %> ago.
</span>
</td>
<td>
Thanks again!
It's going to be something as follows (can't get specific without code):
daily.rake
- your rake task called through cron
daily
namespace :daily do
desc "run daily tasks"
task :daily, :needs => :environment do
Account.each do |account|
account.users.each do |user|
UserMailer.deliver_daily_email(user)
end
end
end
user_mailer.rb
- Your action mailer to send the actual email
UserMailer < ActiveMailer::Base
def daily_email(user)
@recipients = "#{user.email}"
@from = "my@email"
headers "Reply-to" => "my@email"
@subject = "Your daily message"
@sent_on = Time.now
@body[:user] = user
end
end
daily_email.text.html.erb
- The actual content of the email
<h2>Hi <%=h @user.name %></h2>
You like:
<ul>
<% @user.account.ideas.each do |idea| %>
<li><%=h idea.title %></li>
<% end %>
</ul>
-sincerely, the mailer server
Hope this gives you a good idea of how this should all be configured. Let me know if anything is confusing here. Enjoy!
[Edit]
As noted by Josh Pinter below, you'll probably only want to send the user new ideas (either created in the last 24 hours, or track which ideas the user has seen and preclude those from e-mail). You can do this by a scope in idea.rb
scope :recent, -> { where("created_at < ", 1.day.ago) }
or
# Note, you'll need to create and maintain a `viewed_at` column
scope :unviewed, -> { where(viewed_at: nil) }
Then, for each user, you can check
ideas = user.ideas.recent.any?
and only send the e-mail if there are recent
or unviewed
ideas. After you send the e-mail, you could set viewed at by running ideas.update_all(viewed_at: Time.now)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With