How can I send cyrillic emails with Mandrill and Windows1251 encoding?

958 views Asked by At

I have started to use Mandrill to send all mails from our site but faced a problem with encoding.

Encoding of the site is still Windows1251 / CP-1251 (I really don't have time to change it). I can send emails in English. But when I try to send cyrillic emails (Ukrainian, Russian, etc) it shows me an error "You must specify a key value...".

I need always to encode body of email to UTF-8. In this case Mandrill sends letter well but emails are with broken encoding.

Does somebody know if it possible to send emails with Mandrill using Windows-1251 charset? How can I fix it? Any advice will be useful!

Code is below:

require_once "all-sdk/mandrill/src/Mandrill.php";
$message_txt = utf8_encode($message);

try {
    $mandrill = new Mandrill('my_api_key_here');
    $message = array(
        'html' => $message_txt,
        'text' => 'Example text content',
        'subject' => 'example subject',
        'from_email' => '[email protected]',
        'from_name' => 'Example Name',
        'to' => array(
            array(
                'email' => '[email protected]',
                'name' => 'Recipient Name',
                'type' => 'to'
            )
        ),
        'headers' => array('Reply-To' => '[email protected]'),
        'important' => false,
        'track_opens' => null,
        'track_clicks' => null,
        'auto_text' => null,
        'auto_html' => null,
        'inline_css' => null,
        'url_strip_qs' => null,
        'preserve_recipients' => null,
        'view_content_link' => null,
        'bcc_address' => '[email protected]',
        'tracking_domain' => null,
        'signing_domain' => null,
        'return_path_domain' => null,
        'merge' => true,
        'merge_language' => 'mailchimp',
        'global_merge_vars' => array(
            array(
                'name' => 'merge1',
                'content' => 'merge1 content'
            )
        ),
        'merge_vars' => array(
            array(
                'rcpt' => '[email protected]',
                'vars' => array(
                    array(
                        'name' => 'merge2',
                        'content' => 'merge2 content'
                    )
                )
            )
        )//,
        // 'tags' => array('password-resets'),
        // 'subaccount' => 'customer-123',
        // 'google_analytics_domains' => array('example.com'),
        // 'google_analytics_campaign' => '[email protected]',
        // 'metadata' => array('website' => 'www.example.com'),
        // 'recipient_metadata' => array(
        //     array(
        //         'rcpt' => '[email protected]',
        //         'values' => array('user_id' => 123456)
        //     )
        //),
        // 'attachments' => array(
        //     array(
        //         'type' => 'text/plain',
        //         'name' => 'myfile.txt',
        //         'content' => 'ZXhhbXBsZSBmaWxl'
        //     )
        // ),
        // 'images' => array(
        //     array(
        //         'type' => 'image/png',
        //         'name' => 'IMAGECID',
        //         'content' => 'ZXhhbXBsZSBmaWxl'
        //     )
        // )
    );
    $async = false;
    // $ip_pool = 'Main Pool';
    // $send_at = 'example send_at';
    // $result = $mandrill->messages->send($message, $async, $ip_pool, $send_at);
    $result = $mandrill->messages->send($message, $async, '','');
    print_r($result);
    /*
    Array
    (
        [0] => Array
            (
                [email] => [email protected]
                [status] => sent
                [reject_reason] => hard-bounce
                [_id] => abc123abc123abc123abc123abc123
            )

    )
    */
} catch(Mandrill_Error $e) {
    // Mandrill errors are thrown as exceptions
    echo 'A mandrill error occurred: ' . get_class($e) . ' - ' . $e->getMessage();
    // A mandrill error occurred: Mandrill_Unknown_Subaccount - No subaccount exists with the id 'customer-123'
    throw $e;
}
1

There are 1 answers

1
Kir Mazur On

Found! To send cyrillic (cp1251) emails in Madrill just use iconv-function before any API-call.

$message_txt = iconv('windows-1251','UTF-8',$message);
$subject = iconv('windows-1251','UTF-8',$subject);