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.
Related
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
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.
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
I'd like to send a one off email to users and, being new-ish to ActionMailer, not sure how to approach a one-off like this? Rails Runner, a Rake task or maybe even the rails console on the production machine?!?
Figure I'd add an action for it to user_mailer.rb. And create an accompanying view in app/views/user_mailer/. Just not sure how to trigger it. I'd like to grab a few users and send to them in batches. Happy to do this manually...as we stagger sending the messages over the next few days/weeks.
Appreciate any suggestions or advice.
If you want to do this as automatedly as possible, I'd take a look at the whenever gem. It lets you automate rake (or runner) tasks as cron jobs. You can add a script somewhere in lib that uses ActionMailer to generate the emails, and schedule when they're sent with whenever/cron.
As far as sending in batches,
I would do something like this:
Users.find_each(:batch_size => n) do |m|
mail = UserMailer.new(m.email_address, message)
mail.deliver
end
Obviously, that's near-pseudocode, but hopefully it puts you on the right track?
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.