Skip to content

Commit 709acb0

Browse files
author
bidi
committed
added save copy of sent message to folder
1 parent b000b23 commit 709acb0

5 files changed

Lines changed: 124 additions & 7 deletions

File tree

CHANGELOG.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
## 4.1.0 - 2022-08-11
2+
3+
### Changed
4+
* Nothing
5+
6+
### Added
7+
* Saving outgoing emails into folders
8+
9+
### Deprecated
10+
* Nothing
11+
12+
### Removed
13+
* Nothing
14+
15+
### Fixed
16+
* Nothing
17+
118
## 4.0.0 - 2022-08-10
219

320
### Changed
@@ -16,6 +33,23 @@
1633
### Fixed
1734
* Nothing
1835

36+
## 3.4.0 - 2022-08-10
37+
38+
### Changed
39+
* Nothing
40+
41+
### Added
42+
* Saving outgoing emails into folders
43+
44+
### Deprecated
45+
* Nothing
46+
47+
### Removed
48+
* Nothing
49+
50+
### Fixed
51+
* Nothing
52+
1953
## 3.3.1 - 2022-08-10
2054

2155
### Changed

README.md

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ DotKernel mail component based on [laminas-mail](https://github.com/laminas/lami
44

55

66
![OSS Lifecycle](https://img.shields.io/osslifecycle/dotkernel/dot-mail)
7-
![PHP from Packagist (specify version)](https://img.shields.io/packagist/php-v/dotkernel/dot-mail/3.3.0)
7+
![PHP from Packagist (specify version)](https://img.shields.io/packagist/php-v/dotkernel/dot-mail/4.1.0)
88

99
[![GitHub issues](https://img.shields.io/github/issues/dotkernel/dot-mail)](https://github.com/dotkernel/dot-mail/issues)
1010
[![GitHub forks](https://img.shields.io/github/forks/dotkernel/dot-mail)](https://github.com/dotkernel/dot-mail/network)
1111
[![GitHub stars](https://img.shields.io/github/stars/dotkernel/dot-mail)](https://github.com/dotkernel/dot-mail/stargazers)
12-
[![GitHub license](https://img.shields.io/github/license/dotkernel/dot-mail)](https://github.com/dotkernel/dot-mail/blob/3.0/LICENSE.md)
12+
[![GitHub license](https://img.shields.io/github/license/dotkernel/dot-mail)](https://github.com/dotkernel/dot-mail/blob/4.0/LICENSE.md)
1313

1414

1515

@@ -19,9 +19,8 @@ The `$this->mailService->send()->isValid()` method call will return a boolean va
1919
If the returned result is `true`, the e-mail was valid, otherwise the e-mail was invalid.
2020
In case your e-mail was invalid, you can check for any errors using `$this->mailService->send()->getMessage()`.
2121

22-
Using the below logic will let you determinate if a message was valid or not and log it.
23-
24-
The implementor can write it's own custom error log logic.
22+
Using the below logic will let you determine if a message was valid or not and log it.
23+
You can implement your own custom error logging logic.
2524

2625
````
2726
$result = $this->mailService->send();
@@ -54,3 +53,21 @@ return [
5453
];
5554
```
5655
To disable it again, set the value of `sent` to `null`.
56+
57+
### Saving a copy of an outgoing mail into a folder
58+
First, make sure the `save_sent_message_folder` key is present in config file `mail.local.php` under `dot_mail.default`. Below you can see its placement and default value.
59+
```php
60+
<?php
61+
62+
return [
63+
'dot_mail' => [
64+
'default' => [
65+
...
66+
'save_sent_message_folder' => ['INBOX.Sent']
67+
],
68+
],
69+
];
70+
```
71+
Common folder names are `INBOX`, `INBOX.Archive`, `INBOX.Drafts`, `INBOX.Sent`, `INBOX.Spam`, `INBOX.Trash`. If you have `MailService` available in your class, you can call `$this->mailService->getFolderGlobalNames()` to list the folder global names for the email you are using.
72+
73+
Multiple folders can be added to the `save_sent_message_folder` key to save a copy of the outgoing email in each folder.

src/Factory/MailServiceAbstractFactory.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Dot\Mail\Service\MailServiceInterface;
2020
use Interop\Container\ContainerInterface;
2121
use Laminas\Mail\Message;
22+
use Laminas\Mail\Storage\Imap;
2223
use Laminas\Mail\Transport\File;
2324
use Laminas\Mail\Transport\Smtp;
2425
use Laminas\Mail\Transport\TransportInterface;
@@ -58,8 +59,9 @@ public function __invoke(
5859
$logService = $container->get(LogServiceInterface::class);
5960
$message = $this->createMessage();
6061
$transport = $this->createTransport($container);
62+
$storage = $this->createStorage($container);
6163

62-
$mailService = new MailService($logService, $message, $transport);
64+
$mailService = new MailService($logService, $message, $transport, $storage, $this->mailOptions);
6365

6466
//set subject
6567
$mailService->setSubject($this->mailOptions->getMessageOptions()->getSubject());
@@ -186,6 +188,23 @@ protected function setupTransportConfig(TransportInterface $transport)
186188
return $transport;
187189
}
188190

191+
/**
192+
* @param ContainerInterface $container
193+
* @return Imap
194+
*/
195+
protected function createStorage(ContainerInterface $container)
196+
{
197+
$host = $this->mailOptions->getSmtpOptions()->getHost();
198+
$connectionConfig = $this->mailOptions->getSmtpOptions()->getConnectionConfig();
199+
$storage = new Imap([
200+
'host' => $host,
201+
'user' => $connectionConfig['username'],
202+
'password' => $connectionConfig['password'],
203+
]);
204+
205+
return $storage;
206+
}
207+
189208
/**
190209
* @param MailEventListenerAwareInterface $service
191210
* @param ContainerInterface $container

src/Options/MailOptions.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ class MailOptions extends AbstractOptions
4747
/** @var array */
4848
protected $eventListeners = [];
4949

50+
/** @var array */
51+
protected $saveSentMessageFolder;
52+
5053
/**
5154
* @return array
5255
*/
@@ -164,4 +167,20 @@ public function setEventListeners(array $eventListeners)
164167
{
165168
$this->eventListeners = $eventListeners;
166169
}
170+
171+
/**
172+
* @return array
173+
*/
174+
public function getSaveSentMessageFolder()
175+
{
176+
return $this->saveSentMessageFolder;
177+
}
178+
179+
/**
180+
* @param array $saveSentMessageFolder
181+
*/
182+
public function setSaveSentMessageFolder(array $saveSentMessageFolder): void
183+
{
184+
$this->saveSentMessageFolder = $saveSentMessageFolder;
185+
}
167186
}

src/Service/MailService.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@
1414
use Dot\Mail\Event\MailEventListenerAwareTrait;
1515
use Dot\Mail\Exception\InvalidArgumentException;
1616
use Dot\Mail\Exception\MailException;
17+
use Dot\Mail\Options\MailOptions;
1718
use Dot\Mail\Result\MailResult;
1819
use Dot\Mail\Result\ResultInterface;
1920
use Laminas\Mail\Message;
21+
use Laminas\Mail\Storage\Imap;
2022
use Laminas\Mail\Transport\TransportInterface;
2123
use Laminas\Mime\Message as MimeMessage;
2224
use Laminas\Mime\Mime;
@@ -49,11 +51,15 @@ class MailService implements
4951
public function __construct(
5052
LogServiceInterface $logService,
5153
Message $message,
52-
TransportInterface $transport
54+
TransportInterface $transport,
55+
Imap $storage,
56+
MailOptions $mailOptions
5357
) {
5458
$this->logService = $logService;
5559
$this->message = $message;
5660
$this->transport = $transport;
61+
$this->storage = $storage;
62+
$this->mailOptions = $mailOptions;
5763
}
5864

5965
/**
@@ -87,6 +93,13 @@ public function send(): ResultInterface
8793
$this->logService->sent($this->message);
8894
}
8995

96+
//save copy of sent message to folders
97+
if ($this->mailOptions->getSaveSentMessageFolder()) {
98+
foreach ($this->mailOptions->getSaveSentMessageFolder() as $folder) {
99+
$this->storage->appendMessage($this->message->toString(), $folder);
100+
}
101+
}
102+
90103
return $result;
91104
}
92105

@@ -268,4 +281,19 @@ public function setTransport(TransportInterface $transport)
268281
{
269282
$this->transport = $transport;
270283
}
284+
285+
/**
286+
* @return array
287+
*/
288+
public function getFolderGlobalNames()
289+
{
290+
$folderGlobalNames = [];
291+
foreach ($this->storage->getFolders() as $folder) {
292+
$folderGlobalNames[] = $folder->getGlobalName();
293+
}
294+
foreach ($this->storage->getFolders()->getChildren() as $folder) {
295+
$folderGlobalNames[] = $folder->getGlobalName();
296+
}
297+
return $folderGlobalNames;
298+
}
271299
}

0 commit comments

Comments
 (0)