2013-06-21 63 views
1

我無法發送郵件與SWIFT郵件時,收件人名稱包含括號()在symfony中1.4,使用swiftmailer,setTo在名稱括號打破消息發送

示例代碼的問題:

$mail = new Swift_Message(); 
$mail->setTo('[email protected]', 'Recipient (bla bla)'); 
$mail->setFrom('[email protected]', 'Sender'); 
$mail->setSubject('Test email'); 
$mail->setBody('<html><body><h3>Hello</h3></body></html>', 'text/html'); 
sfContext::getInstance()->getMailer()->send($mail); 

如果收件人使用Gmail,他確實收到電子郵件,但當點擊向下箭頭以獲取更多信息時,該部分爲空:

Missing to: part

問題是其他一些電子郵件服務沒有收到該電子郵件。

至於原始郵件內容,這裏顯示的內容太多了,但基本上我的問題是如何在不破壞任何內容的情況下使用圓括號。

回答

0

好像BASE64_ENCODE可以幫助,所以我修改了swiftmailer類SimpleMessage.php,以確保應用此修復程序每次。從這個SimpleMessage中的

修改:

public function setTo($addresses, $name = null) 
{ 
    if (!is_array($addresses) && isset($name)) 
    { 
    $addresses = array($addresses => $name); 
    } 

    if (!$this->_setHeaderFieldModel('To', (array) $addresses)) 
    { 
    $this->getHeaders()->addMailboxHeader('To', (array) $addresses); 
    } 
    return $this; 
} 

要這樣:

public function setTo($addresses, $name = null) 
{ 
    // if $name is set, encode it 
    if(isset($name)) 
    { 
    $name = $this->encodeName($name); 
    } 

    // if $addresses is an non numeric array (email => name), encode each name. 
    if(is_array($addresses) && array_keys($addresses) !== range(0, count($addresses) - 1)) 
    { 
    foreach($addresses as $key => $value) 
    { 
     $addresses[$key] = $this->encodeName($value);; 
    } 
    } 

    if (!is_array($addresses) && isset($name)) 
    { 
    $addresses = array($addresses => $name); 
    } 

    if (!$this->_setHeaderFieldModel('To', (array) $addresses)) 
    { 
    $this->getHeaders()->addMailboxHeader('To', (array) $addresses); 
    } 
    return $this; 
} 

/** 
* Encode the name to avoid issues with some characters 
* 
* @param string $name 
* @return string 
*/ 
private function encodeName($name) 
{ 
    return "=?UTF-8?B?". base64_encode($name) . '?='; 
} 

這個答案讓我:Base64 encode from name for email