覆盖 Yii2 Swiftmailer 收件人
Override Yii2 Swiftmailer Recipient
我需要在整个 Yii2 应用程序中为 Swiftmailer 的 send()
函数的每个实例覆盖收件人电子邮件。这是为了负载测试。
有没有简单的方法来做到这一点?或者至少是一种无需编辑 Swiftmailer 的供应商文件即可完成此操作的方法?
如果这只是为了测试,为什么不设置 useFileTransport
这样电子邮件将保存在您选择的文件夹中而不是被发送。为此,请像这样配置它:
'components' => [
// ...
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'useFileTransport' => true,
],
],
这会将所有电子邮件保存在 @runtime/mail
文件夹中,如果您想要不同的一组:
'mailer' => [
// ...
'fileTransportPath' => '@runtime/mail', // path or alias here
],
如果您仍想发送电子邮件并覆盖收件人,您可以扩展 yii\swiftmailer\Mailer
class.
class MyMailer extends \yii\swiftmailer\Mailer
{
public $testmode = false;
public $testemail = 'test@test.com';
public function beforeSend($message)
{
if (parent::beforeSend($message)) {
if ($this->testmode) {
$message->setTo($this->testemail);
}
return true;
}
return false;
}
}
配置它:
'components' => [
// ...
'mailer' => [
'class' => 'namespace\of\your\class\MyMailer',
// the rest is the same like in your normal config
],
],
您可以像一直使用 mailer
组件一样使用它。何时切换到测试模式修改配置:
'mailer' => [
'class' => 'namespace\of\your\class\MyMailer',
'testmode' => true,
'testemail' => 'test222@test.com', // optional if you want to send all to address different than default test@test.com
// the rest is the same like in your normal config
],
有了这个,每封电子邮件都将被您的收件人地址覆盖。
我需要在整个 Yii2 应用程序中为 Swiftmailer 的 send()
函数的每个实例覆盖收件人电子邮件。这是为了负载测试。
有没有简单的方法来做到这一点?或者至少是一种无需编辑 Swiftmailer 的供应商文件即可完成此操作的方法?
如果这只是为了测试,为什么不设置 useFileTransport
这样电子邮件将保存在您选择的文件夹中而不是被发送。为此,请像这样配置它:
'components' => [
// ...
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'useFileTransport' => true,
],
],
这会将所有电子邮件保存在 @runtime/mail
文件夹中,如果您想要不同的一组:
'mailer' => [
// ...
'fileTransportPath' => '@runtime/mail', // path or alias here
],
如果您仍想发送电子邮件并覆盖收件人,您可以扩展 yii\swiftmailer\Mailer
class.
class MyMailer extends \yii\swiftmailer\Mailer
{
public $testmode = false;
public $testemail = 'test@test.com';
public function beforeSend($message)
{
if (parent::beforeSend($message)) {
if ($this->testmode) {
$message->setTo($this->testemail);
}
return true;
}
return false;
}
}
配置它:
'components' => [
// ...
'mailer' => [
'class' => 'namespace\of\your\class\MyMailer',
// the rest is the same like in your normal config
],
],
您可以像一直使用 mailer
组件一样使用它。何时切换到测试模式修改配置:
'mailer' => [
'class' => 'namespace\of\your\class\MyMailer',
'testmode' => true,
'testemail' => 'test222@test.com', // optional if you want to send all to address different than default test@test.com
// the rest is the same like in your normal config
],
有了这个,每封电子邮件都将被您的收件人地址覆盖。