-
-
Notifications
You must be signed in to change notification settings - Fork 154
/
Copy pathcustom_attachment_mask.php
57 lines (44 loc) · 1.5 KB
/
custom_attachment_mask.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php
/*
* File: custom_message_mask.php
* Category: Example
* Author: M.Goldenbaum
* Created: 14.03.19 18:47
* Updated: -
*
* Description:
* -
*/
class CustomAttachmentMask extends \Webklex\PHPIMAP\Support\Masks\AttachmentMask {
/**
* New custom method which can be called through a mask
* @return string
*/
public function token(): string {
return implode('-', [$this->id, $this->getMessage()->getUid(), $this->name]);
}
/**
* Custom attachment saving method
* @return bool
*/
public function custom_save(): bool {
$path = "foo".DIRECTORY_SEPARATOR."bar".DIRECTORY_SEPARATOR;
$filename = $this->token();
return file_put_contents($path.$filename, $this->getContent()) !== false;
}
}
$cm = new \Webklex\PHPIMAP\ClientManager('path/to/config/imap.php');
/** @var \Webklex\PHPIMAP\Client $client */
$client = $cm->account('default');
$client->connect();
$client->setDefaultAttachmentMask(CustomAttachmentMask::class);
/** @var \Webklex\PHPIMAP\Folder $folder */
$folder = $client->getFolder('INBOX');
/** @var \Webklex\PHPIMAP\Message $message */
$message = $folder->query()->limit(1)->get()->first();
/** @var \Webklex\PHPIMAP\Attachment $attachment */
$attachment = $message->getAttachments()->first();
/** @var CustomAttachmentMask $masked_attachment */
$masked_attachment = $attachment->mask();
echo 'Token for uid ['.$masked_attachment->getMessage()->getUid().']: '.$masked_attachment->token();
$masked_attachment->custom_save();