-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8ac8036
commit f26a981
Showing
2 changed files
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,17 +29,39 @@ class Mailer extends CApplicationComponent | |
*/ | ||
public $dsn; | ||
|
||
/** | ||
* @var array<string, string|string[]> the default headers to be applied for each sending email. | ||
* For example: | ||
* | ||
* ``` | ||
* [ | ||
* 'From' => 'My App<[email protected]>', | ||
* 'Bcc' => '[email protected]', | ||
* 'X-Custom-Header' => 'foobar', | ||
* ] | ||
* ``` | ||
*/ | ||
public $defaultHeaders = []; | ||
|
||
/** | ||
* @var \Symfony\Component\Mailer\Mailer Swift mailer instance. | ||
*/ | ||
private $_symfonyMailer; | ||
|
||
/** | ||
* @var \Symfony\Component\Mailer\Transport\TransportInterface|\Closure|string|null transport instance or its class name or factory PHP callback. | ||
*/ | ||
private $_transport; | ||
|
||
public function send(RawMessage $message, ?Envelope $envelope = null): void | ||
{ | ||
foreach ($this->defaultHeaders as $name => $value) { | ||
if (in_array(strtolower($name), ['from', 'to', 'cc', 'bcc', 'reply-to'])) { | ||
$value = (array) $value; | ||
} | ||
$message->getHeaders()->addHeader($name, $value); | ||
} | ||
|
||
$this->getSymfonyMailer()->send($message, $envelope); | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
use Symfony\Component\Mailer\Mailer as SymfonyMailer; | ||
use Symfony\Component\Mailer\Transport\Smtp\SmtpTransport; | ||
use Symfony\Component\Mailer\Transport\NullTransport; | ||
use Symfony\Component\Mime\Email; | ||
use Yii; | ||
use yii1tech\mailer\Mailer; | ||
use yii1tech\mailer\transport\ArrayTransport; | ||
|
@@ -73,4 +74,32 @@ public function testSetupSymfonyMailer(): void | |
$mailer->setSymfonyMailer($symfonyMailer); | ||
$this->assertSame($symfonyMailer, $mailer->getSymfonyMailer()); | ||
} | ||
|
||
public function testDefaultHeaders(): void | ||
{ | ||
$transport = new ArrayTransport(); | ||
|
||
/** @var Mailer $mailer */ | ||
$mailer = Yii::createComponent([ | ||
'class' => Mailer::class, | ||
'transport' => $transport, | ||
'defaultHeaders' => [ | ||
'From' => 'My App<[email protected]>', | ||
'Bcc' => '[email protected]', | ||
], | ||
]); | ||
|
||
$email = new Email(); | ||
$email->addTo('[email protected]'); | ||
$email->subject('Test subject'); | ||
$email->text('Test body'); | ||
|
||
$mailer->send($email); | ||
|
||
$sentMessage = $transport->getLastSentMessage(); | ||
|
||
$this->assertSame('[email protected]', $sentMessage->getFrom()[0]->getAddress()); | ||
$this->assertSame('My App', $sentMessage->getFrom()[0]->getName()); | ||
$this->assertSame('[email protected]', $sentMessage->getBcc()[0]->getAddress()); | ||
} | ||
} |