似乎 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