【问题标题】:How to specify a template in a transational email with mailjet如何使用 mailjet 在交易电子邮件中指定模板
【发布时间】:2013-06-12 12:47:49
【问题描述】:

我在我的 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 界面发送的电子邮件相同。

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

我该怎么做?

【问题讨论】:

    标签: ruby-on-rails-3 mailjet


    【解决方案1】:

    声明:我正在与 Mailjet 的开发者关系小组合作。

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

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

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

    【讨论】:

    • 我也对这个功能感兴趣。现在可以使用了吗?
    【解决方案2】:

    我创建了一个围绕当前广告系列系统的解决方案。它的工作原理是这样的:

    • 在 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'})

    最后一个参数将在电子邮件中将===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
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-13
      • 2021-05-31
      • 2013-01-14
      • 2014-03-29
      • 1970-01-01
      • 1970-01-01
      • 2015-06-19
      • 2014-10-11
      相关资源
      最近更新 更多