Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to cancel email in delivering_email hook?

I am implementing a email message queue. I use interceptor.

class MyInterceptor
  def self.delivering_email(mail)
    Email.queue(mail)
  end
end

ActionMailer::Base.register_interceptor(MyInterceptor)

But this code sends email in a normal way. How do I stop the email from being sent? I will manually send emails from the queue.

Thanks.

Sam

like image 816
Sam Kong Avatar asked Oct 19 '11 16:10

Sam Kong


People also ask

How do I send an email to ROR?

Go to the config folder of your emails project and open environment. rb file and add the following line at the bottom of this file. It tells ActionMailer that you want to use the SMTP server. You can also set it to be :sendmail if you are using a Unix-based operating system such as Mac OS X or Linux.

What is mail interceptor?

Interceptor allows making any modifications to mail objects: change the subject, add cc and bcc , even modify from . It will apply these changes for the mail delivery life cycle of every email sent before it will be actually handed off to the delivery agents.

What is action mailer?

Action Mailer allows you to send emails using mailer classes and views. Mailers work very similarly to controllers.


1 Answers

Set Mail::Message#perform_deliveries to false:

class NeverDeliverInterceptor
  def self.delivering_email(message)
    message.perform_deliveries = false
  end
end

ActionMailer::Base.register_interceptor(NeverDeliverInterceptor)

See API doc for source & other usage.

like image 145
Eliot Sykes Avatar answered Oct 05 '22 23:10

Eliot Sykes