|
class EmailCampaign < ApplicationRecord |
|
include HtmlToPlainText |
|
|
|
has_many :email_transactions |
|
|
|
def send_to_all(test = false, test_quantity = 0) |
|
|
|
# BEGIN TEST |
|
if test |
|
# Replicate the same transaction for the same subscription. |
|
# Since it's a test they'll all go to a test email address |
|
test_subs = [] |
|
subscription = EmailSubscription.first |
|
test_quantity.times do |n| |
|
test_subs.push(subscription) |
|
end |
|
|
|
send_to_subs(test_subs, true, test) |
|
|
|
return |
|
end |
|
# END TEST |
|
|
|
EmailSubscription.where("level >= ?", self.level).find_in_batches(batch_size: 100) do |subs| |
|
send_to_subs(subs, true, test) |
|
end |
|
end |
|
|
|
def send_to_subs(subs, mass_mail = false, test = false) |
|
subs.each do |subscription| |
|
transaction = create_transaction_for_subscription(subscription) |
|
begin |
|
SesMailerJob.perform_later(transaction.id, mass_mail, test) |
|
rescue => e |
|
puts "Error caught in queing SesMailer #{e}" |
|
end |
|
end |
|
end |
|
|
|
def create_transaction_for_subscription(subscription) |
|
transaction = EmailTransaction.new |
|
transaction.email_campaign = self |
|
transaction.email_subscription = subscription |
|
transaction.email = subscription.user.email |
|
transaction.save |
|
transaction |
|
end |
|
|
|
def send_to_user(user) |
|
if !user.email_subscription |
|
user.email_subscription = EmailSubscription.create_for_user(user) |
|
user.save |
|
else |
|
return if user.email_subscription.is_campaign_muted(self) |
|
return if self.level > user.email_subscription.level |
|
end |
|
|
|
send_to_subs([user.email_subscription]) |
|
end |
|
|
|
def send_to_email(email) |
|
user = User.find_or_create_by(email: email) |
|
send_to_user(user) |
|
end |
|
|
|
TEMPLATE_ROOT = "app/views/email_campaigns" |
|
|
|
def get_binding(transaction) |
|
binding |
|
end |
|
|
|
TOKEN = '@TOKEN' |
|
ERB_TAG = /<%.+?%>/ |
|
|
|
def precompile |
|
layout_path = "#{TEMPLATE_ROOT}/layout.html.erb" |
|
layout_raw = File.open(layout_path).read |
|
|
|
template_path = "#{TEMPLATE_ROOT}/#{self.template}" |
|
template_html = File.open(template_path).read |
|
|
|
result = layout_raw.gsub("<%= yield %>", template_html) |
|
|
|
# premailer doesnt play well with ERB, so we remove them temporarily |
|
matches = result.scan(ERB_TAG) |
|
tokenized_text = result.dup |
|
matches.each do |match| |
|
tokenized_text.sub!(match, TOKEN) |
|
end |
|
|
|
# Ensure assets are precompiled before accessing public/assets |
|
# rails assets:precompile |
|
|
|
premailer = Premailer.new( |
|
tokenized_text, |
|
:warn_level => Premailer::Warnings::SAFE, |
|
:with_html_string => true, |
|
:css => [ |
|
"public/assets/mailers/style.css", |
|
] |
|
) |
|
premailer.to_plain_text |
|
premailed_tokenized_text = premailer.to_inline_css |
|
premailed_text = premailed_tokenized_text.dup |
|
|
|
matches.each do |match| |
|
premailed_text.sub!(TOKEN, match) |
|
end |
|
|
|
require 'fileutils' |
|
FileUtils.mkdir_p "#{TEMPLATE_ROOT}/generated" |
|
|
|
path = "#{TEMPLATE_ROOT}/generated/#{self.template}" |
|
File.open(path, "w+") do |f| |
|
f.write(premailed_text) |
|
end |
|
end |
|
|
|
def get_html_and_plain(transaction) |
|
template_path = "#{TEMPLATE_ROOT}/generated/#{self.template}" |
|
template_html = File.open(template_path).read |
|
|
|
rendered_template_html = ERB.new(template_html).result(binding) |
|
|
|
# remove all text in between <style></style> tag |
|
cleaned = rendered_template_html.dup.gsub /<style.+style>/m, '' |
|
plain = convert_to_text(cleaned) |
|
|
|
rich = rendered_template_html |
|
|
|
return rich, plain |
|
end |
|
|
|
def self.create_campaign(name, subject, level) |
|
campaign = EmailCampaign.new |
|
campaign.subject = subject |
|
template_name = "#{DateTime.now.strftime('%Y%m%d%H%M%S')}_#{name}.html.erb" |
|
campaign.template = template_name |
|
campaign.level = level |
|
|
|
path = "#{TEMPLATE_ROOT}/#{template_name}" |
|
File.open(path, "w+") do |f| |
|
f.write("") |
|
end |
|
|
|
campaign.save |
|
end |
|
|
|
def self.compile_all |
|
self.all.each { |c| c.precompile } |
|
end |
|
|
|
end |