How to remove *|MC_PREVIEW_TEXT|* from email title using Mailchimp and or Mandrill

Viewed 28339

I have created an email template which includes merging tags on MailChimp which then gets published to Mandrill.

When my script runs and I receive email, as you can see, |MC_PREVIEW_TEXT| appears in the title.

image

I have searched both Mandrill and MailChimp for this tag but it is not appearing in either template file.

How do I remove this from the email?

10 Answers

We had the same issue, so I wrote simple rake task for our ROR app, which removes |MC_PREVIEW_TEXT| section from Mandrill templates automatically. Here an example of my rake task:

require 'mandrill'
namespace :mandrill do
  desc 'Removes *|MC_PREVIEW_TEXT|* section for all email templates in mandrill app'
  task remove_mc_preview_text: :environment do

    # Templates with handlebars merge language
    templates = [
      'template-example-1',
      'template-example-2',
    ]

    mandrill = Mandrill::API.new 'YOUR_API_KEY'

    templates.each do |name|
      begin
        puts "Processing the template: #{name}"

        # Get the information for an existing template
        result = mandrill.templates.info name

        # Finds the section with MC_PREVIEW_TEXT inside a template and substitutes it to the empty string
        code = result['code'].sub(/\<\!\-\-\*\|IF:MC_PREVIEW_TEXT[[:ascii:]]+END:IF\|\*\-\-\>/m, '')

        # If nil is provided for any fields, the values will remain unchanged.
        from_email = nil
        from_name = nil
        subject = nil
        text = nil
        labels = nil

        # Set to false to update the draft version of the template without publishing
        publish = true

        # Update the code for an existing template
        mandrill.templates.update name, from_email, from_name, subject, code, text, publish, labels

        puts "Successfully deleted *|MC_PREVIEW_TEXT|* section from the template: #{name}"
      rescue Mandrill::Error => e
        # Mandrill errors are thrown as exceptions
        puts "A mandrill error occurred: #{e.class} - #{e.message}"
      end
    end
    puts 'Done!'
  end
end

There are few steps should be done to make it work:

  1. Install the Mandrill API client as a gem: gem install mandrill-api or add to your Gemfile: gem 'mandrill-api'
  2. Create any rake task (for example remove_mc_preview_text.rake) with the code listed above inside the lib/tasks/ folder
  3. Change 'YOUR_API_KEY' to your real Mandrill API Key
  4. Change template names in templates array to your real templates, if for some reason you don't know them, you can find them here https://mandrillapp.com/templates

And finally run this command in the root folder of your rails app:

bundle exec rake mandrill:remove_mc_preview_text

If you use any other programming language you can write similar script using my as an example, see Mandrill API Clients for different programming languages.

Remove following lines from your html code:

 <span class="m_-63420320203924518mcnPreviewText" style="display:none;font-size:0px;line-height:0px;max-height:0px;max-width:0px;opacity:0;overflow:hidden">*|MC_PREVIEW_TEXT|*</span>

After this insert your html code in the body of your mail.
Now your problem should be solved ....

We had the same issue with Mailchimp template editor for sending templates to Mandrill. Also there was an issue with https:// appended to handlebars tag by Mailchimp template editor which forced us to remove https:// from urls in payload. So to fix MC_PREVIEW_TEXTand issue with urls I created a Firefox extension.

https://addons.mozilla.org/en-US/firefox/addon/mandrillchimp/

The only thing you need to do is create "special" Mandrill API Key which will allow extension to get and update template (info and update permissions).

open html file and then ctrl+f , then use |MC_PREVIEW_TEXT| to search and replace your text instead of it

This may help some of you wondering how to do it.

I had the issue when exporting a HTML template from MailChimp into Intercom.

Line numbers may be different, so copy HTML code into notepad/text edit and search |MC_PREVIEW_TEXT| to find it

Video with instructions.

I use Mailchimp templates exported to my Linux and Mac server (Teamcity agents). So I replace patterns with pretty text using sed:

# replace
sed -i 's/*|MC\:SUBJECT|*//' *.html
sed -i 's/*|MC_PREVIEW_TEXT|\*//' *.html

For those who need a mail command at the same place:

# send email
html="/path/to/html"

# linux agent
mutt -e 'set content_type=text/html' -s "Subject #%build.counter%" user@example.com <"$html"

# mac agent
/opt/homebrew/bin/mutt -e 'set content_type=text/html' -s "Subject #%build.counter%" user@example.com <"$html"
Related