Skip to content

Commit c5c52bb

Browse files
author
Piotr Suszalski
committed
Add attachments support to client
1 parent 8d88ae5 commit c5c52bb

5 files changed

Lines changed: 157 additions & 5 deletions

File tree

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,27 @@ $mail->addRecipientTo('recipient email address');
141141
$response = $mailService->send($mail);
142142
```
143143

144+
## Send email with attachments
145+
146+
You can sent emails with attachments. You can upload up to 10 files. Weight of all attachments in email can't exceed 10Mb.
147+
```php
148+
use \FreshMail\Api\Client\Service\Messaging\Mail;
149+
use \FreshMail\Api\Client\Messaging\Mail\MailBag;
150+
151+
$token = 'MY_APP_TOKEN';
152+
$mailService = new Mail($token);
153+
154+
$mail = new MailBag();
155+
$mail->setFrom('from@address.com', 'Support');
156+
$mail->setSubject('Hello, thats mail with attachments!!');
157+
$mail->setHtml('<html><body><strong>Attachments</strong> in mail</body></html>');
158+
$mail->addRecipientTo('recipient email address');
159+
$mail->addAttachment('path to local file 1');
160+
$mail->addAttachment('path to local file 2');
161+
162+
$response = $mailService->send($mail);
163+
```
164+
144165
# Error handling
145166
API throws exceptions for errors that occurred during requests and errors occurred before sending requests.
146167

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
}
1111
},
1212
"require": {
13-
"php": "^7.0",
13+
"php": "^7.0",
1414
"guzzlehttp/guzzle": "^6.3",
1515
"monolog/monolog": "^1.24",
1616
"psr/log": "^1.1",

src/Messaging/Mail/Attachment.php

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
namespace FreshMail\Api\Client\Messaging\Mail;
4+
5+
use FreshMail\Api\Client\Messaging\Mail\Exception\ExternalFileException;
6+
use FreshMail\Api\Client\Messaging\Mail\Exception\FileDoesNotExistException;
7+
8+
/**
9+
* Class Attachment
10+
* @package FreshMail\Api\Client\Messaging\Mail
11+
*/
12+
class Attachment
13+
{
14+
/**
15+
* @var string
16+
*/
17+
private $name;
18+
19+
/**
20+
* @var string
21+
*/
22+
private $content;
23+
24+
/**
25+
* Attachment constructor.
26+
* @param string $filepath
27+
* @throws ExternalFileException
28+
* @throws FileDoesNotExistException
29+
*/
30+
public function __construct(string $filepath)
31+
{
32+
$this->validate($filepath);
33+
$this->name = basename($filepath);
34+
$this->content = base64_encode(rtrim(file_get_contents($filepath)));
35+
}
36+
37+
/**
38+
* @return array
39+
*/
40+
public function toArray(): array
41+
{
42+
return ['name' => $this->name, 'content' => $this->content];
43+
}
44+
45+
/**
46+
* @return string
47+
*/
48+
public function getName(): string
49+
{
50+
return $this->name;
51+
}
52+
53+
/**
54+
* @return string
55+
*/
56+
public function getContent(): string
57+
{
58+
return $this->content;
59+
}
60+
61+
/**
62+
* @param string $filepath
63+
* @throws FileDoesNotExistException
64+
*/
65+
private function validate(string $filepath)
66+
{
67+
if (!realpath($filepath)) {
68+
throw new FileDoesNotExistException($filepath);
69+
}
70+
}
71+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace FreshMail\Api\Client\Messaging\Mail\Exception;
4+
5+
/**
6+
* Class FileDoesNotExistException
7+
* @package FreshMail\Api\Client\Messaging\Mail\Exception
8+
*/
9+
class FileDoesNotExistException extends \Exception
10+
{
11+
/**
12+
* FileDoesNotExistException constructor.
13+
* @param string $filepath
14+
*/
15+
public function __construct(string $filepath)
16+
{
17+
parent::__construct(sprintf('Invalid path to file or file does not exist, path: %s', $filepath));
18+
}
19+
}

src/Messaging/Mail/MailBag.php

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace FreshMail\Api\Client\Messaging\Mail;
44

55
use FreshMail\Api\Client\Messaging\Mail\Exception\ContentMismatchException;
6+
use FreshMail\Api\Client\Messaging\Mail\Exception\ExternalFileException;
67

78
class MailBag implements \JsonSerializable
89
{
@@ -31,13 +32,22 @@ class MailBag implements \JsonSerializable
3132
*/
3233
private $headers = [];
3334

35+
/**
36+
* @var Attachment[]
37+
*/
38+
private $attachments = [];
39+
3440
/**
3541
* @var string
3642
*/
3743
private $templateHash;
3844

3945
/**
40-
* @param mixed $to
46+
* @param string $email
47+
* @param array $customFields
48+
* @param array $customHeaders
49+
* @throws Exception\InvalidCustomFieldException
50+
* @throws Exception\InvalidHeaderException
4151
*/
4252
public function addRecipientTo(string $email, array $customFields = [], array $customHeaders = []): void
4353
{
@@ -62,7 +72,9 @@ public function setSubject($subject): void
6272
}
6373

6474
/**
65-
* @param mixed $html
75+
* @param string $body
76+
* @throws ContentMismatchException
77+
* @throws Exception\InvalidContentBodyException
6678
*/
6779
public function setHtml(string $body): void
6880
{
@@ -74,7 +86,9 @@ public function setHtml(string $body): void
7486
}
7587

7688
/**
77-
* @param mixed $text
89+
* @param string $body
90+
* @throws ContentMismatchException
91+
* @throws Exception\InvalidContentBodyException
7892
*/
7993
public function setText(string $body): void
8094
{
@@ -87,6 +101,7 @@ public function setText(string $body): void
87101

88102
/**
89103
* @param string $hash
104+
* @throws ContentMismatchException
90105
*/
91106
public function setTemplateHash(string $hash): void
92107
{
@@ -98,7 +113,19 @@ public function setTemplateHash(string $hash): void
98113
}
99114

100115
/**
101-
* @param mixed $headers
116+
* @param string $filepath
117+
* @throws ExternalFileException
118+
* @throws Exception\FileDoesNotExistException
119+
*/
120+
public function addAttachment(string $filepath): void
121+
{
122+
$this->attachments[] = new Attachment($filepath);
123+
}
124+
125+
/**
126+
* @param string $name
127+
* @param string $value
128+
* @throws Exception\InvalidHeaderException
102129
*/
103130
public function addHeader(string $name, string $value): void
104131
{
@@ -107,6 +134,7 @@ public function addHeader(string $name, string $value): void
107134

108135
/**
109136
* @param array $headers
137+
* @throws Exception\InvalidHeaderException
110138
*/
111139
public function addHeaders(array $headers): void
112140
{
@@ -115,6 +143,14 @@ public function addHeaders(array $headers): void
115143
}
116144
}
117145

146+
/**
147+
* @return Attachment[]
148+
*/
149+
public function getAttachments(): array
150+
{
151+
return $this->attachments;
152+
}
153+
118154
/**
119155
* @return string
120156
*/
@@ -192,6 +228,7 @@ private function getTemplateHash(): ?string
192228
/**
193229
* @param ContentType $contentType
194230
* @param string $body
231+
* @throws Exception\InvalidContentBodyException
195232
*/
196233
private function replaceContent(ContentType $contentType, string $body): void
197234
{
@@ -279,6 +316,10 @@ function jsonSerialize(): array
279316
$data['headers'] = $headers;
280317
}
281318

319+
foreach ($this->getAttachments() as $attachment) {
320+
$data['attachments'][] = $attachment->toArray();
321+
}
322+
282323
return $data;
283324
}
284325
}

0 commit comments

Comments
 (0)