我创建了一个围绕当前活动系统的解决方案。它是这样工作的:
- 在 mailjet 中创建活动
- 为该广告系列创建模板
- 返回活动列表,检查开始,并获取此活动的 id。
- 添加
510969 => 'my_new_email_template'
mailjet.rake
- 运行 rake mailjet:import
- 新模板将保存为 erb in
app/views/shared/_auto_my_new_email_template.html.erb
- 您可以通过调用以下方式发送包含新模板的电子邮件:
CustomMailer.send_mailjet_email('my_new_email_template', emails, subject, {USER_NAME: 'John'})
最后一个参数将替换===USER_NAME===
为电子邮件中的 John(作为能够在电子邮件中包含变量的一种方式)。在 mailjet 你会写这样的东西:
Hello, ===USER_NAME===,
click here to activate your account: ===ACTIVATION_LINK=== ...
lib/tasks/mailjet.rake:
# WARNING: you need to add gem 'mailjet' in Gemfile for development group to use this task
# if mailjet is included in development, this breaks mails_viewer
# (see http://stackoverflow.com/questions/17083110/the-gem-mailjet-breaks-mails-viewer)
namespace :mailjet do
desc "Importe les templates d'emails de mailjet dans l'appli. (inscription, mot de passe perdu etc)"
task :import => :environment do
templates = {
510969 => 'reset_password_instructions',
510681 => 'contact_request'
# more templates here
}
templates.each do |id, path|
fullpath = "app/views/shared/_auto_#{path}.html.erb"
out_file = File.new(fullpath, 'w')
out_file.puts(Mailjet::Campaign.find(id).html)
out_file.close
puts "Importing email ##{id} to #{fullpath}\n"
end
end
end
应用程序/mailers/custom_mailer.rb:
class CustomMailer < ActionMailer::Base
default :from => "\"Support\" <contact@yourdomain.com>"
default :to => "\"Support\" <contact@yourdomain.com>"
def send_email(emails, content, subject, reply_to = nil)
@emails = emails.split(',')
@content = content
@emails.each do |m|
#logger.info "==========> sending email to #{m}"
mail(to: m, subject: subject, reply_to: reply_to) do |format|
format.html { content }
#format.text { content }
end.deliver
end
end
def send_mailjet_email(template, emails, subject, params = {}, reply_to = nil)
template = "app/views/shared/_auto_#{template}.html.erb"
content = File.read(template) # can't render twice with rails
params.each do |key, value|
key = "===#{key.upcase}==="
content.gsub!(key, value)
end
content.gsub!('Voir la version en ligne', '')
content.gsub!(t('mailjet_unsubscribe'), '')
content.gsub!(/Voir[^\w]+la[^\w]+version[^\w]+en[^\w]+ligne/, '')
content.gsub!('https://fr.mailjet.com/campaigns/template/', '') # sometimes mailjet appends this at the begining of links
content = content.html_safe
CustomMailer.send_email(emails, content, subject, reply_to)
end
end
有了这个,我可以告诉我的经理按照他的意愿去编辑电子邮件。我告诉他,他可以使用===USER_NAME===
或其他变量。当他完成后,我只是rake mailjet:import
, 并将所有模板检索为 erb。这已经投入生产几个月了,它非常有用。
最后一点:@madflo,如果您可以让我们在 mailjet 界面中查看所有已发送的电子邮件,那就太好了。(现在,您可以看到最近发送的大约 50 封电子邮件)。我希望能够检查至少一个月前是否已经发送了一些电子邮件。