Tag Archives: E-Mail

(日本語) さくらレンタルサーバでIMAP接続を切る方法


Sorry, this entry is only available in 日本語.


Rails 4 Send mail when Error occurs


I wrote code to send mail when error occurs in Rails 4.

Environment

  • Ubuntu 15.05 (64bit)
  • Ruby 2.2.2
  • Rails 4.1.8

Old Way

In Rails 3, I could use rescue_in_public method in ApplicationController and send error notification mail.

current_user returns user who logging in. It’s defined in devise gem.

But in rails 4, it doesn’t work.

Solution

I used rescue_from, which also exists in Rails 3.

rescue_from Exception catches all exception occurs in controller, but rescue_from StandardError doesn’t. It is because RuntimeError, for example, is inherits Exception but doesn’t inherit StandardError, which is raised by raise "exception!" simply. And, syntax error like 0 = 1 couldn’t be caught. Error caught by rescue_from Exception is errors occurs in running program. And errors occur before controller procedure can’t be caught.

The point is raise exception in handle_exception method. I don’t want to erase exception by handle_exception, but want only to send notification mail. It is like AOP. In the above, add method into application_controller.rb simply.

current_user in send_error_mail method returns logging in user, which is defined in the gem, devise. self is used for reporting in which controller the error occurs. request is for reporting request url, etc. The code !Rails.env.development? should be replace to config value. I heard that we can use custom configuration in Rails 4.2, so it is the issue after rails upgrading.

Attention

Exceptions before controller procedure should be reported by other way.

I know handling exception outside of controller is a way, but there are variables can be referred in controller and objects useful in controller, I thought, so I wrote code into applicatoin_controller.rb.

Continue reading Rails 4 Send mail when Error occurs

How to send email automatically and periodically


How do you do, when you should send email periodically? With Google Apps Script, we can send email automatically and periodically.

First, wrote the code to send email like below.

You can add CC and BCC on the email.

And now, select “Resources” from menu, and click “Current project’s triggers”, and follow the instructions and create a trigger to execute sending method periodically. You can send email everyday, or on the specific weekdays, or beginning or end of month, etc. Notification email is available, which tells you the information when program fails.

In the above case, I use MailApp for sending email、 You can send email according to you or other’s calendar, using CalendarApp. Detail of MailApp or CalendarApp, Utilities is on the online document published by Google.