Email Digest for Rail Application - ruby-on-rails

I am working on a rails application where people can follow each other and post comments. Currently I use delayed_jobs for people to get notifications when someone follows them or post a comment. My question is how can I collect all the notifications for one user and create an email digest for notifications and send the email at the end of the day. Putting it in another way, can we collate the delayed_jobs?

Delayed_job won't do this for you, but it's straightforward enough to do yourself.
The simplest thing to do would be to write a rake task that you run once a day, and have the task find all the activity for that user that has occurred in the past day, and then send them an email about that activity.

Right now you are sending an email on a set of actions (following event or comment event).
Instead, if your user is digest-enabled, insert a record into a pending-email-digest table, and then daily process the email digests via a cron job.

Related

How to structure send email to all users using Rails

I plan to send my users a reminder email once a day.
I will user Heroku scheduler.
As I understand it:
I create a RAKE task in lib/tasks/scheduler.rake
Then do the config on Heroku to run that
In the RAKE task I need to call something like User.send_reminders, in send_reminders I pass a request to the mailer to send an email to each user.
My question is should send_reminders be in the the users model or the users controller?
Probably very basic but I can't quite work it out.
Thanks in advance.
Always need to write model related actions in the model itself.
Controller should be kept percise for request related decision making.
So keep your send_reminders in model and call it from rake task.

Rails: Sending a reminder email if a condition is true

I am looking to send users of a product an email reminding them to do something if a certain condition is true.
Based on my knowledge so far, the following aspects are going to be involved:
Script: The code that needs to be executed (sending the email), and a condition that checks whether the email should be sent or not.
Scheduling: Execute the script once a day, using the gem whenever for easier creation of cron jobs.
Assuming this is true, I would do this:
Create a script consisting of an email incl. the condition whether the email shall be sent.
Create a cron job using whenever to execute the script.
Would that work? It seems to be that step one does not work, since I doubt you can set up the whole email in the script.
Can someone elaborate on creating a script that sends a message based on a condition?
Bonus question: How can I adapt a cron job to different time zones? Some users are from Europe and some from the States. Ideally, everyone should get the email at the same time in their timezone.
I tried to write this question in an elaborative way so that other beginners on this topic who might run in to the same roadblocks have an easy way to follow.
The most straight forward way I can think of is to have a job run every hour that checks your condition, then sends an email to that user. You can use cron for this, or that gem whenever.
I would make this a rake task in your Rails application. That rake task's psuedo code would look something like this.
def remind_user_to_do_something
users = User.get_users_based_on_condition
// The above could be a scope defined on your User class.
// Basically that scope would check 2 things
// 1. Do I meet the condition you're talking about above.
// 2. Is it time to send an email to them based on their timezone.
for each user in users
MyActionMailerClass.send_email_for_user user
end
end

General Guidance on Rails Mailers

I am making an event registration tool in Rails and I am having trouble working out the mailing section. I am using Mailgun API and I've got a generic "Thank you for Registering" email working when the user signs up as well as a contact form submission that comes to my email. Part of the requirements for the application is the ability to send promotional emails (separate from Thank you for Registering emails). These promotional emails are more like (One week reminder) type emails.
So these emails need to be able to be created by the admin setting up the event as this is a general purpose tool. So to save the emails the admin creates, I have a mailings object. So the relationship is a bit like this:
Event has many mailers, registrations, etc. (and those belong to the event). They are nested resources because they are specific to an event. Now I need to bridge the gap of how to go from the mailers created by the admin to sending them to Mailgun. The problem is we will have to have the ability to add recipients because they may want to send to people besides the registrants for the event. So I need to go from the mailing#show (which shows a preview of the mailing and will need to be able to add/remove recipients), loop through all of the recipients, and send the message that is in the mailing.message field.
I am so close to finishing this tool except for this mailing which I cannot wrap my head around. I see a lot of examples that create a mailer but I am not sure if that would work for me since the message are unique and it needs to get the message and subject from the mailer object. Any advice or guidance? I am really struggling to get this part done.
I assume you have a User model with an email column
I would setup an extra model i.e.
class PromoMail < ActiveRecord::Base
has_many :users # recpients
validates :body, presence: true
end
Then add a controller, where admins can create these and insert the Mail content in the body field and add recipients.
Then create a new method in your existing mailer to send the mail with the yielded body to one user.
Then add a action to the forementioned controller to send the PromoMail by looping over the associated users and call the ne mailer metod with each.
Couple of steps here, without going into much detail.
1) Make a rake task which looks for any emails which needs to be sent out, and sends them out. You might need to expand your schema to record whether a mail (or mailing or whatever) has been sent already. The rake task itself shouldn't have much code, it should just call a class method in eg User or Mailing or something.
You'll need to think about how the system can decide which emails need to get sent out. I find that flowcharts can be helpful here: it's going to involve iterating over all users, or all mailings, or something, and applying various logical tests to them. You may find that your current schema isn't up to the job, in which case expand it.
2) Schedule this rake task to be run at regular intervals, eg once a day or once a week. Various scheduled task runners are available, eg cron, or if you're on Heroku you'll need to use their Scheduler tool, for example.

rails 2 actionmailer email address alias

I am working with rails 2 . I wanted to know is there a way in ActionMailer to hide/alias the email address??
Also what should be the best approach for sending bulk emails???
Thanks in advance.
Mailcar
Mailcar 0.1
Mailcar is a dead-simple (ie. not many features) bulk emailer for Ruby on Rails, using ActiveRecord
and ActionMailer.
If you need to send emails to your entire user base and don't want to pay the high prices that most
mass email services charge, then this is the plugin for you!
WARNING!!! Using this without having your SMTP server set up correctly could land your server on
hundreds of blacklists. If that prospects scares you, look at the pay-to-send bulk emailers. It's
expensive, but at least you know you're safe.
WARNING!!! As is, this script is tied tightly to the codebase I wrote it for. It will not work out
of the box. I'm posting it in the hopes that someone else needs something similar and doesn't want to
start from scratch. Please send me patches or pull requests with your upgrades and I'll include them
and add you to the credits.
Installation
To copy the models and create the migration, run:
script/generate mailcar all
Then execute the migration:
rake db:migrate
Example
To send a new bulk email, you must first create the message body file. I use Thunderbird to create an
HTML email, then save it out as HTML.
At that point, you can create the message and send it as follows:
rake mailcar:new_message FROM='me#mydomain.com' SUBJECT='New features on the site' BODY_FILE=/path/to/email/body
rake mailcar:prep_for_send MESSAGE_ID=99
rake mailcar:send MESSAGE_ID=99
If the sending process is ever interrupted, you can resume it with another call to send.
TODO
Add a test suite (wish I knew more about plugin testing...)
Make it easy to give a block (or something) to generate the list of email addresses
Pass emails through ERB to allow for templating / dynamic emails
Add support for multipart emails
Add a cleanup task to remove old messages
Make sending delay configurable

Rails send email on schedule

I have an app that presents a calendar allowing you to create events that display on the calendar. One of the fields you set is the day the event starts. I would like to be able to send email reminders out when the day of the event arrives.
I'm new to rails so I'm not exactly sure what I need or where to start. I read the rails guide for ActionMailer and generated a mailer, I was assuming I would need one of those. I also read the rails guide for observers and thought maybe that might be a route to what I want to accomplish.
Let's say every day, 7 days a week at 9am central, a job (cron or rake task) should run that searches the 'start_at' column of every Event in the database (sqlite locally, Postegre with heroku in production) and if the start_date is equal to 'today?' then send the action mailer template to remind the user.
So what's the best way to (and with what tool) to build a job to examine that database and kick off emails every day? In the future I guess this would grow to reminders ahead of time.
Some pseudo-code:
def send_email
if self.start_at == Date.today #assuming self.start_at.today? works too.
// send email
end
Just not sure how to get that working.
You could have a Controller/Action that does it, then call the url from a cron job on a specified schedule using a command like cURL.

Resources