2015-06-10 58 views
0

我已經開始使用Mandrill從我們的網站發送所有郵件,但遇到編碼問題。如何使用Mandrill和Windows1251編碼發送西里爾郵件?

該網站的編碼仍然是Windows1251/CP-1251(我真的沒有時間去改變它)。我可以用英文發送電子郵件。但是當我嘗試發送西里爾郵件(烏克蘭語,俄語等)時,它向我顯示了一個錯誤「您必須指定一個關鍵值...」。

我總是需要將電子郵件正文編碼爲UTF-8。在這種情況下,Mandrill可以很好地發送信件,但電子郵件的編碼是破損的。

有人知道是否可以使用Windows-1251字符集發送帶有Mandrill的電子郵件?我該如何解決它?任何建議都會有用!

代碼如下:

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; 
} 
+0

我們可以看到你的代碼調用Mandrill,編輯到問題中嗎? – halfer

+0

@halfer代碼已添加! –

+1

在發送到Mandrill之前,嘗試將消息從其當前字符集轉換爲UTF-8。看看['iconv'](https://php.net/manual/en/function.iconv.php)。 – halfer

回答

1

找到!要在Madrill中發送西里爾文(cp1251)電子郵件,只需在任何API調用之前使用iconv-function。

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

是的,這正是我所建議的':-)'。 – halfer

+0

@halfer是的,非常感謝!我給你一點意見! :) –

相關問題