2013-06-12 86 views
2

我在rails應用程序中使用mailjet gem如何使用mailjet在跨電子郵件中指定模板

在Gemfile中

gem 'mailjet' 

而一些配置:初始化/ mailjet.rb

Mailjet.configure do |config| 
    config.api_key = 'your-api-key' 
    config.secret_key = 'your-secret-key' 
    config.default_from = '[email protected]' 
end 

application.rb中(用於測試或環境/ development.rb)

config.action_mailer.delivery_method = :mailjet 

到目前爲止這麼好。這些電子郵件可以通過mailjet正確發送。但是我希望能夠指定模板。這樣,每個發送的郵件看起來都與通過mailjet界面發送的郵件相同。

在界面中,您可以在「我的模板」中創建新橫幅時選擇一個模板。我想在發送郵件時指定其中的一個

我該怎麼做?

回答

1

Disclamer:我正在和Mailjet的Developer Relations小組合作。

從API調用模板是在未來幾個月應該可以從API訪問的功能。我們目前正在對其進行beta測試!

在此期間 - 我會建議在代碼中使用erb或其他本地模板。

您可以隨時使用(並複製/粘貼)來自我們的模板生成器的HTML代碼到您的rails模板中,並調用它來生成電子郵件的正文。這是一個快速解決方案 - 我們將在未來幾周內改進。

+0

我對這個功能也很感興趣。它現在可用嗎? – htaidirt

1

我已經創建了一個解決方案,包裝當前的廣告系列。它的工作原理是這樣的:

  • 在mailjet
  • 建立廣告活動,該廣告活動創建一個模板
  • 返回活動清單中,檢查開始,得到這個廣告系列的ID。進口
  • 新方案將在app/views/shared/_auto_my_new_email_template.html.erb
  • 保存爲ERB您可以用新的模板發送電子郵件致電:
  • 在mailjet.rake
  • 運行rake mailjet添加510969 => 'my_new_email_template'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。耙

# 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\" <[email protected]>" 
    default :to => "\"Support\" <[email protected]>" 

    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封電子郵件)。我希望能夠檢查一些電子郵件是否至少在一個月前被髮送。