Wrong encoding when creating campaign through API

725 views
Skip to first unread message

Anton

unread,
Aug 19, 2009, 8:58:23 AM8/19/09
to MailChimp API Discuss, tho...@headnet.dk
Hi there,

We've sent out a few newsletters through the API. They contain
Scandinavian chars and looks fine in lots of email clients except
Outlook 2000, Outlook 2002 and Outlook 2003. Here, the non-ascii chars
are rendered strange.

When we edit a newsletter through the MailChimp web interface,
everything is fine. And when we submit a campaign through the API and
"touches" the content afterwards in the web interface, everything is
fine. But API without TTW edit afterwards results in wrong encoded
newsletter.

Looking at the email sources the pattern is this. The "bad" emails has
this:

--_----------=_MCPart_861551414
Content-Type: text/html;
charset="us-ascii"
Content-Transfer-Encoding: quoted-printable

<html>


And the "good" emails has this:

Content-Type: text/html; charset="utf-8"
Content-Transfer-Encoding: quoted-printable

<html>


It looks like a bug to me. We are using the API v. 1.1 and are sending
the HTML email content UTF-8 encoded.

Is there a workaround or a fix for this?

I can provide more details if needed.


/Anton

jesse

unread,
Aug 19, 2009, 10:54:32 AM8/19/09
to MailChimp API Discuss
Every time we've run across someone having this problem it has turned
out that they were somehow not actually submitting UTF-8 encoded data.
I'll test a few things again today and see if I can replicate any
problem. If you can send code that consistently reproduces this, that
would be very helpful in tracking down any issue that may exist.


jesse

DavidM

unread,
Aug 19, 2009, 6:39:03 PM8/19/09
to MailChimp API Discuss
I had a similar problem with UTF-8 stuff refusing to show in Apple
Mail and Outlook 2007. Wrote this to get around it:

function entityiseHTML($html) {
// This makes sure all our UTF-8 stuff is entity-ised, as we have
random mail clients dying (I'm looking at you, Outlook 2007 and Apple
Mail) when encountering straight-up UTF.
$html = htmlentities($html, ENT_NOQUOTES, 'utf-8');
$trans = get_html_translation_table();
$newtrans = array();
foreach ($trans as $key => $val) $newtrans[$val] = $key;
return strtr($html, $newtrans);
}

Not sure how it'll work for other languages; this was mainly so
apostrophes would stop turning into accented a's.

Anton

unread,
Aug 20, 2009, 5:43:12 AM8/20/09
to MailChimp API Discuss
Hi Jesse,

On Aug 19, 4:54 pm, jesse <je...@mailchimp.com> wrote:
> Every time we've run across someone having this problem it has turned
> out that they were somehow not actually submitting UTF-8 encoded data.
> I'll test a few things again today and see if I can replicate any
> problem. If you can send code that consistently reproduces this, that
> would be very helpful in tracking down any issue that may exist.

I think I nailed the encoding issue.

It turns out we were leaving out the plain text content. Mailchimp
still puts in a
plain text section to the mail body and gives it ascii encoding. Looks
like thats
what confuses older Outlooks.

When we are adding non-ascii chars to the plain text, the plain text
section gets
utf-8 encoded as well and Outlook is happy.

See example code below.

/Anton


# -*- coding: utf-8 -*-

import urllib
import urllib2

# we need to demonstrate the MailChimp encoding issue
apikey = 'secret'
body = '<p>Testing non-ascii HTML æ, ø, å</p>'
body_text = 'Testing non-ascii plain text æ, ø, å'

params = {'options[list_id]':'823d4d8737',
'options[subject]':'My test newsletter - broken',
'options[from_name]':'Anton',
'options[from_email]':'an...@dummyhost.com',
'content[html]': body,
'apikey':apikey,
'output':'json',
'type':'regular',
}

# lets create a broken campaign
enc_params = urllib.urlencode(params,)
response = urllib2.urlopen("http://api.mailchimp.com/1.1/?
method=campaignCreate", enc_params)
cid = response.read()
response.close()

# add a body with utf-8 chars to force Mailchimp to add right
encoding
params['content[text]'] = body_text
params['options[subject]'] = 'My test newsletter - working'
enc_params = urllib.urlencode(params,)
response = urllib2.urlopen("http://api.mailchimp.com/1.1/?
method=campaignCreate", enc_params)
cid = response.read()
response.close()

Anton

unread,
Aug 20, 2009, 5:45:29 AM8/20/09
to MailChimp API Discuss
Hi David,

Thanks for you suggestion.

It looks like the MailChimp web interface is using a similar strategy
in turning non-ascii chars into HTML entities.

/Anton
Reply all
Reply to author
Forward
0 new messages