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

﹥>﹥吖頭↗ 提交于 2019-12-11 19:23:12

问题


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' => 'fromemail@gmail.com',
        'from_name' => 'Example Name',
        'to' => array(
            array(
                'email' => 'myemail@gmail.com',
                'name' => 'Recipient Name',
                'type' => 'to'
            )
        ),
        'headers' => array('Reply-To' => 'message.reply@example.com'),
        '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' => 'message.bcc_address@example.com',
        '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' => 'recipient.email@example.com',
                'vars' => array(
                    array(
                        'name' => 'merge2',
                        'content' => 'merge2 content'
                    )
                )
            )
        )//,
        // 'tags' => array('password-resets'),
        // 'subaccount' => 'customer-123',
        // 'google_analytics_domains' => array('example.com'),
        // 'google_analytics_campaign' => 'message.from_email@example.com',
        // 'metadata' => array('website' => 'www.example.com'),
        // 'recipient_metadata' => array(
        //     array(
        //         'rcpt' => 'recipient.email@example.com',
        //         '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] => recipient.email@example.com
                [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:


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);


来源:https://stackoverflow.com/questions/30752752/how-can-i-send-cyrillic-emails-with-mandrill-and-windows1251-encoding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!