当在我的 Symfony2 应用程序中发送电子邮件时,我正在尝试指定发件人的名称
Administrator <[email protected]>
我已经尝试过
parameters.ini
:
mailer_from = {"[email protected] : Administrator"}
似乎可以这样做,因为在 SimpleMessage.php 中我们确实有:
/**
* Set the sender of this message.
* This does not override the From field, but it has a higher significance.
* @param string $sender
* @param string $name optional
* @return Swift_Mime_SimpleMessage
*/
public function setSender($address, $name = null)
{
if (!is_array($address) && isset($name))
{
$address = array($address => $name);
}
if (!$this->_setHeaderFieldModel('Sender', (array) $address))
{
$this->getHeaders()->addMailboxHeader('Sender', (array) $address);
}
return $this;
}
我在
parameters.ini
中尝试的所有事情都失败了。你知道我做错了什么吗?
// Set a single From: address
$message->setFrom('[email protected]');
// Set a From: address including a name
$message->setFrom(array('[email protected]' => 'Your Name'));
// Set multiple From: addresses if multiple people wrote the email
$message->setFrom(array(
'[email protected]' => 'Sender One',
'[email protected]' => 'Sender Two'
));
似乎可以这样做,因为在 SimpleMessage.php 中我们确实有
嗯,可以设置名称,但是没有任何迹象表明您可以使用配置来设置它。
将返回
SwitfMailerBundle
(php app/console config:dump-reference SwiftmailerBundle
) 的可用配置的快速转储:
Default configuration for "SwiftmailerBundle"
swiftmailer:
transport: smtp
username: ~
password: ~
host: localhost
port: false
encryption: ~
auth_mode: ~
spool:
type: file
path: %kernel.cache_dir%/swiftmailer/spool
sender_address: ~
antiflood:
threshold: 99
sleep: 0
delivery_address: ~
disable_delivery: ~
logging: true
如您所见,有
sender_address
被传递到 ImpersonatePlugin.php
。
我不确定,您可以设置一个名称,但标准方法,原始地,是使用这种形式的字符串:
“管理员”
如果它不起作用,则可能意味着您需要做一些工作,编写一个真正的 EmailManager。
SwiftMailerBundle 实际上只是 SwiftMailer 库的集成,它是一个库,而不是一个“管理器”。
不使用相同版本的 Symfony,但由于这是此问题的最佳结果之一,因此以下是我为
"symfony/mailer": "5.4.*",
所做的操作,以获取显示名称。
$email = (new Email())
->from(new Address('[email protected]', 'Name To Display'))
->to($recipientEmail)
...
$mailer->send($email);