Getting started: Sending an email in Rails with delayed job send email - ruby-on-rails

I am new to delayed job. I am trying to configure delayed job to send an email. In my model I have...
def send_reminder_emails
NsoMailer.send_reminder_emails(self)
end
I have the appropriate send_reminder_emails action in app/mailers/nso_mailer.rb, and a test email in registrations/reminder_email.html.erb.
I have followed directions in https://github.com/collectiveidea/delayed_job to install the delayed job and daemons gems. Now my question is how to configure the job? In this case I think the job should be Delayed::Job.enqueue Registrations.send_email_reminders??? Documentation seems a little lacking on the github wiki. I know there is the script/delayed_job file. Should I be modifying this file?
If anyone can suggest a getting started page or blog that would be nice. I'm running this on my own server (no heroku), also we have our own SMTP server (so no sendgrid or 3rd party mail services). Mail is already configured on the server and I am successfully sending emails. My goal with delayed_job is to send some reminder emails once per day.
Any help or nudge in the right direction greatly appreciated.

If you want to call the method on your model asynchronously then you can do the following:
model_instance.delay.send_reminder_email
If you want to call a method on the mailer directly then you'll do something like (I'm assuming that the method name defined in nso_mailer.rb is reminder_emails):
NsoMailer.delay.reminder_emails(model_instance)
In my projects, I usually have either a service object take care of all the notifications or create a concern which handles the sending of emails.
A good resource to look at is Ryan Bates's Railcast titled 'Service Objects' or read DHH's blog post on using concerns Put chubby models on a diet with concerns
As for your setup questions, if you are deploying via capistrano then you can call bundle exec bin/delayed_job restart after the deployment is successful to restart the DJ daemon.

Related

External web service for sending background emails in rails

I've used this instructions and sent "Welcome" mail to my signed up user. But this makes the user wait for 5-8 seconds because the server is trying to complete this mail thing.
I don't want the user to wait until the mail is sent but immediately see the "Mail has sent" message. So this brings me background jobs in Rails.
There are many options like delayed_job, Resque etc for background jobs in Rails. But to use these kind of solutions, as I understand:
1- Create a background job
2- Run this job
Let's say I used one of the background job solutions, so then I need something else to run also this job, like cron job...
I think just for sending sign-up and password reminder emails, another easier solution should be possible. I mean like another external service that 1- I'll create a template for each kind of mail I'll send, 2- I'll pass some arguments like receiver_email, template_id, receiver_username, password_link etc... With that way, I won't need any background job, and the user will not wait.
I came across some other gem called "sucher_punch" but as I understand from the people's messages and posted problems, with using heroku, this gem can fail for some reasons of heroku dynos and the mail may not be sent, and you don't know it.
Anyway, what is the general way that rails developers handle this email issue? Maybe I can also use sendgrid like the way I explained above, can I ?
Sending emails in the background is such a common use case, Rails 4.2 introduced a #deliver_later method in ActionMailer to provide seamless ActiveJob integration.
You don't need to set up a cron job to check if there are any jobs in the background queue. Sidekiq, Resque or DelayedJob will take care of that for you.
It seems Sendgrid does allow creating templates and sending variable content to fill them up, but that feature doesn't undermine the benefits of making that call asynchronously. In fact, deferring it to the background also has the added benefit of not disrupting user experience if the external resource(Sendgrid) is unavailable.
You should try installing one of the background processing solutions you mentioned(I recommend sidekiq) and take advantage of the ActionMailer + ActiveJob integration.

Sending SMS from rails app

I am building a medication reminder system using Ruby on Rails to be deployed on heroku.
Using this system a doctor will enter a patient's medication details including medication name, dose as well as timing details and the app will then notify the patient via sms when its time to take his/her medicine.
I have developed the application but I am stuck on the sms part since that involves running a process over and over again until the medication's period has elapsed.
I want to be able to run a script from a rails app that will repeatedly query the database and when it is time to send a sms it will dispatch it to the patient. This cannot function in the normal request/response web cycle.
I explored rubygems that allow developers to create background jobs such as rufus scheduler and resque but I can't seem to figure out how to go about doing this.
Please help I am open to all types of suggestions. I am using Twilio for sending sms
I don't know about resque/rufus, but I know that sidekiq has the ability to queue jobs, but have them delayed till a certain time.
https://github.com/mperham/sidekiq/wiki/Delayed-Extensions#advanced-options
You'd need to look into how exact the delay is (ie. what sidekiq's polling frequency is) depending on your needs, but I would suspect this would work well.
It would require another dynamo or whatever heroku calls it these days.
I use delayed_job using the run_at param to send scheduled SMS via Twilio from Heroku.
I have a send_message method on my message model that does the actual send with the Twilio API. I chose to create a custom job with Delayed Job so when I schedule the message:
Delayed::Job.enqueue my_custom_job, :run_at => TIME_I_WANT_TO_SEND
Using a the heroku worker to run the background task of sending has been very reliable.
Since you're deploying to Heroku, reading their documentation about scheduling jobs is a must for you:
https://devcenter.heroku.com/articles/scheduler

Kinda-mass emailing from Rails, but with own mail server

I've read most of the other answers on this topic, but a lot of them related to either third-party services like MailChimp (which I'm not necessarily opposed to) or how not to upset the host's email server.
I believe this case is unique so that it'll contribute...
I have my own DigitalOcean droplet running a rails app. I need to send out 100-1000 emails every so often, each with a unique message (a link I'm using for tracking clicks originating from the email).
I'm also operating my own iRedMail server.
Can someone recommend how to best-handle this task? I was going to simply cycle through the list of emails and use the template.html.erb to drop in my link, but what types of problems might I run into?
Thank you!
You should decouple your Rails App from the mail sending so that you don't have to wait in your view for the mails to be sent (assuming that you click on something that triggers the start of your mail sending). Use something like delayed_job or another queueing mechanism that Rails offers and only queue up the sending job of the e-mails. Then when the queue comes to execute the particular job you can customize the message with an HTML part and a text part or whatever else you need and pass them on individually to your MTA.

How to receive email via IMAP protocol in Rails 3

What is the best way to receive email via IMAP protocol in Rails 3?
I am not sure about best, but a simple way is to use Net::IMAP. You can write a rake task and periodically poll the mailbox for emails by using a scheduler like cron.
So your setup would look like this:
A library file which servers as a wrapper for IMAP client and other related operations, like processing the emails.
The rake task which when called downloads the emails and processes them using the wrapper mentioned above.
The scheduler (Cron or any other of your choice. I personally always prefer cron) which periodically calls this rake task.
As per ActionMailer's documentation the solution is to have the email forwarded to your rails application and implement UserMailer.receive(STDIN.read) to process the email.
The documentation (Action Mailer Basics) doesn't sound very convincing about this approach. It's not telling where the call should be implemented but I guess that would be at the MTA level.

Processing incoming emails on Heroku

For my side project kwiqi, I use ActionMailer's 'receive' method to process incoming email messages for tracking my expenses. Heroku doesn't have a local mail server running that same code will not work. One solution I've thought of is to periodically hit a controller action that will pull messages from Gmail. Are there other solutions that are reasonable? Is anyone processing incoming emails in Heroku?
You can use sendgrid addon, and their parse api (http://wiki.sendgrid.com/doku.php?id=parse_api). I've written a short tutorial on how to do so here: http://nanceskitchen.com/2010/02/21/accept-incoming-emails-into-a-heroku-app-using-sendgrid/
I know that this is a little late but for anyone else that might find this useful in future we created the http:///CloudMailin.com addon for Heroku that should help you to receive email on Heroku Rails apps really easily.
Heroku support running workers using DelayedJob. Workers are resourced just like Dynos (you pay by the hour) and for this you get a dedicated resource to process your emails.
In the past I have used Cron calling a controller in my app. It's pretty effective.
If the hourly limitation is an issue, you can call your app from another location ... I have a cheap Dreamhost account for some of my non-priority sites that I have used as Cron systems.
There are also a number of ping and uptime services that you can use for this purpose as well ... simply pass these services your email controller.
A real limitation of Heroku currently is that the most rapid frequency they support for cron jobs is hourly.
I'd recommend using Gmail and using delayed job as an alternative to cron to set a more reasonable frequency. There is a good tutorial on setting this up at WiseJive

Resources