Skip to content

Commit 897dbd8

Browse files
committed
Added copy plugin
1 parent 68d9bfa commit 897dbd8

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,22 @@ swiftmailer:
5454
file:
5555
transport: file
5656
```
57+
58+
## Copy plugin
59+
60+
Copy plugin is useful to BCC all outgoing emails to specific address (e.g. if you want to monitor everything what you send out).
61+
62+
**Connecting to Symfony:**
63+
64+
Define CopyPlugin as service in `services.yml`:
65+
66+
```
67+
swiftmailer.mailer.plugin.copy:
68+
class: Geekdevs\SwiftMailer\Plugins\CopyPlugin
69+
arguments:
70+
71+
tags:
72+
- { name: "swiftmailer.primary.plugin" }
73+
```
74+
75+
Note the tag `swiftmailer.primary.plugin` where "primary" should be the name of your mailer.

src/SwiftMailer/Plugin/CopyPlugin.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
namespace Geekdevs\SwiftMailer\Plugin;
3+
4+
/**
5+
* Class CopyPlugin
6+
* @package Geekdevs\SwiftMailer\Transport
7+
*/
8+
class CopyPlugin implements \Swift_Events_SendListener
9+
{
10+
/**
11+
* @var string
12+
*/
13+
protected $recipient;
14+
15+
/**
16+
* CopyPlugin constructor.
17+
* @param string $recipient
18+
*/
19+
public function __construct($recipient)
20+
{
21+
$this->recipient = $recipient;
22+
}
23+
24+
/**
25+
* @param \Swift_Events_SendEvent $evt
26+
*/
27+
public function beforeSendPerformed(\Swift_Events_SendEvent $evt)
28+
{
29+
/**
30+
* @var \Swift_Message $message
31+
*/
32+
$message = $evt->getMessage();
33+
$recipient = $this->recipient;
34+
$headers = $message->getHeaders();
35+
36+
if ($headers->has('to')) {
37+
$headers->addMailboxHeader('X-Swift-To', $message->getTo());
38+
}
39+
40+
if ($headers->has('cc')) {
41+
$headers->addMailboxHeader('X-Swift-Cc', $message->getCc());
42+
}
43+
44+
$message->addBcc($recipient);
45+
}
46+
47+
/**
48+
* @param \Swift_Events_SendEvent $evt
49+
*/
50+
public function sendPerformed(\Swift_Events_SendEvent $evt)
51+
{
52+
}
53+
}

0 commit comments

Comments
 (0)