📜 ⬆️ ⬇️

How we did email

Hi, Habr!

For a long time already I was going to write about how we implemented email-mailings in our own hands, but everything didn’t reach. In general, it’s not for me to tell you about the benefits of email marketing, I just want to share the technical details of the implementation of this task on Ruby on Rails.

Task


It is necessary that users periodically receive emails on a specific condition. For example: User registered 5 days ago.

Implementation


Each sent email message must be logged in order to know whether we have already sent such and such a letter to such or such a user. To begin, create a model EmailLog
')
rails g model EmailLog user:belongs_to type:string 

Next, we describe the class of notification that sends messages and logs:

app / services / notifications / base.rb

 module Notifications module Notification class Base attr_reader :user def initialize(user) @user = user end def perform? false end def send! NotificationMailer.send(notification_type, user).deliver create_log! end private def notification_params { user_id: user.id, type: notification_type } end def notification_type self.class.name.demodulize.underscore end def create_log! EmailLog.create!(notification_params) end end end end 

Now we want to send in 5 days after registration a letter with content like: “Hey, you registered, but you never bought anything!”. We need to describe the class of this notification.

app / services / notifications / notification / five_days_after_registration.rb

 module Notifications module Notification class FiveDaysAfterRegistration < Base def perform? user.created_at.to_date == 5.days.ago.to_date && EmailLog.where(log_params).blank? end end end end 

And then the most interesting, all this must be periodically run. We need a class that runs through all types of notifications and sends them depending on their conditions. Create a class Worker

app / services / notifications / worker.rb

 module Notifications class Worker EMAIL_TYPES = %w( five_days_after_registration ) attr_reader :user def initialize(user) @user = user end def go! EMAIL_TYPES.each do |notification_type| notification_class = "notifications/notification/#{notification_type}".classify.constantize notification = notification_class.new(user) if notification.perform? notification.send! puts "Notification «#{notification_type}» was sent to #{user.email}" break end end end end end 

Is done. So, to send notifications to the user, you can simply write in the console:

 Notifications::Worker.new(user).go! 

And all the notifications, the conditions of which will satisfy the current user will go.

But we need to do the newsletter on the entire user base. The rake task , which we will run every day, for example, at 10 am cron, is perfect for this. For this we need gem whenever .

lib / tasks / notifications.rake

 namespace :notifications do desc 'Lets send emails' task send: :environment do User.where(receive_email_notifications: true).each do |user| Notifications::Worker.new(user).go! end end end 


config / schedule.rb

 every :day, at: '10:00am' do rake 'notifications:send', output: 'log/notifications.log' end 


Well, that's all, the newsletter works! If you have questions, write comments, if there are no questions, then write comments too;)

Source: https://habr.com/ru/post/251973/


All Articles