如何使用mailjet在事务性电子邮件中指定模板

时间:2013-06-12 12:47:49

标签: ruby-on-rails-3 mailjet

我在我的rails应用程序中使用mailjet gem

在Gemfile中

gem 'mailjet'

和一些配置:initializers / mailjet.rb

Mailjet.configure do |config|
  config.api_key = 'your-api-key'
  config.secret_key = 'your-secret-key'
  config.default_from = 'my_registered_mailjet_email@domain.com'
end

application.rb (或者用于测试的环境/ development.rb)

config.action_mailer.delivery_method = :mailjet

到目前为止一切顺利。使用mailjet正确发送电子邮件。但我希望能够指定模板。这样,每个发送的电子邮件看起来都与通过mailjet接口发送的电子邮件相同。

在界面中,您可以在“我的模板”中创建新的campain时选择模板。我想在发送电子邮件时指定其中一个

我该怎么做?

2 个答案:

答案 0 :(得分:1)

Disclamer:我正在与Mailjet的Developer Relations小组合作。

从API调用模板是一项应该可以在未来几个月内从API访问的功能。我们目前正在测试它!

与此同时 - 我建议在您的代码中使用erb或其他本地模板。

您始终可以使用(并复制/粘贴)我们的Template Builder中的HTML代码到您的rails模板中,并调用它来生成您的电子邮件正文。这是一个快速解决方案 - 我们将在未来几周内改进这一点。

答案 1 :(得分:1)

我创建了一个包含当前广告系列的解决方案。它的工作原理如下:

  • 在mailjet中创建广告系列
  • 为该广告系列创建模板
  • 返回广告系列列表,检查开始日期,并获取此广告系列的ID。
  • 在mailjet.rake
  • 中添加510969 => 'my_new_email_template'
  • 运行rake mailjet:import
  • 新模板将在app/views/shared/_auto_my_new_email_template.html.erb
  • 中保存为erb
  • 您可以致电:CustomMailer.send_mailjet_email('my_new_email_template', emails, subject, {USER_NAME: 'John'})
  • 发送包含新模板的电子邮件

最后一个参数将在电子邮件中替换John中的===USER_NAME===(作为能够在电子邮件中包含变量的一种方式)。在mailjet中你会写这样的东西:

Hello, ===USER_NAME===, 
click here to activate your account: ===ACTIVATION_LINK=== ...

<强> LIB /任务/ 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

应用/邮寄者/ 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封电子邮件)。我希望能够检查是否已经发送了一些电子邮件,比如说,至少一个月前。