Skip to content

Commit c4d4c87

Browse files
authored
Merge pull request #1 from FreshMail/feature-APPDEV-5359
APPDEV-5359 extended client to upload base64 attachment contents
2 parents c5c52bb + 6afb645 commit c4d4c87

5 files changed

Lines changed: 100 additions & 13 deletions

File tree

README.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,18 +146,33 @@ $response = $mailService->send($mail);
146146
You can sent emails with attachments. You can upload up to 10 files. Weight of all attachments in email can't exceed 10Mb.
147147
```php
148148
use \FreshMail\Api\Client\Service\Messaging\Mail;
149+
use \FreshMail\Api\Client\Messaging\Mail\Base64Attachment;
150+
use \FreshMail\Api\Client\Messaging\Mail\LocalFileAttachment;
149151
use \FreshMail\Api\Client\Messaging\Mail\MailBag;
150152

151153
$token = 'MY_APP_TOKEN';
152154
$mailService = new Mail($token);
153155

156+
//attachment from hard drive
157+
$localFileAttachment = new LocalFileAttachment(
158+
'/my/local/path/file.extension',
159+
'optional file name'
160+
);
161+
162+
//attachment from base64
163+
$base64Attachment = new Base64Attachment(
164+
'example.txt',
165+
base64_encode('example content')
166+
);
167+
154168
$mail = new MailBag();
155169
$mail->setFrom('from@address.com', 'Support');
156170
$mail->setSubject('Hello, thats mail with attachments!!');
157171
$mail->setHtml('<html><body><strong>Attachments</strong> in mail</body></html>');
158172
$mail->addRecipientTo('recipient email address');
159-
$mail->addAttachment('path to local file 1');
160-
$mail->addAttachment('path to local file 2');
173+
174+
$mail->addAttachment($localFileAttachment);
175+
$mail->addAttachment($base64Attachment);
161176

162177
$response = $mailService->send($mail);
163178
```
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace FreshMail\Api\Client\Messaging\Mail;
4+
5+
interface AttachmentInterface
6+
{
7+
public function getName(): string;
8+
9+
public function getContent(): string;
10+
11+
public function toArray(): array;
12+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace FreshMail\Api\Client\Messaging\Mail;
4+
5+
use FreshMail\Api\Client\Messaging\Mail\Exception\InvalidContentBodyException;
6+
7+
/**
8+
* Class Base64Attachment
9+
* @package FreshMail\Api\Client\Messaging\Mail
10+
*/
11+
class Base64Attachment implements AttachmentInterface
12+
{
13+
/**
14+
* @var string
15+
*/
16+
private $name;
17+
18+
/**
19+
* @var string
20+
*/
21+
private $content;
22+
23+
/**
24+
* Attachment constructor.
25+
* @param string $name
26+
* @param string $content
27+
* @throws InvalidContentBodyException
28+
*/
29+
public function __construct(string $name, string $content)
30+
{
31+
if (base64_encode(base64_decode($content, true)) !== $content) {
32+
throw new InvalidContentBodyException(sprintf('Attachment\'s body with name: %s is invalid base64 string', $name));
33+
}
34+
35+
$this->name = $name;
36+
$this->content = $content;
37+
}
38+
39+
/**
40+
* @return array
41+
*/
42+
public function toArray(): array
43+
{
44+
return ['name' => $this->name, 'content' => $this->content];
45+
}
46+
47+
/**
48+
* @return string
49+
*/
50+
public function getName(): string
51+
{
52+
return $this->name;
53+
}
54+
55+
/**
56+
* @return string
57+
*/
58+
public function getContent(): string
59+
{
60+
return $this->content;
61+
}
62+
}

src/Messaging/Mail/Attachment.php renamed to src/Messaging/Mail/LocalFileAttachment.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* Class Attachment
1010
* @package FreshMail\Api\Client\Messaging\Mail
1111
*/
12-
class Attachment
12+
class LocalFileAttachment implements AttachmentInterface
1313
{
1414
/**
1515
* @var string
@@ -24,13 +24,13 @@ class Attachment
2424
/**
2525
* Attachment constructor.
2626
* @param string $filepath
27-
* @throws ExternalFileException
27+
* @param string|null $name
2828
* @throws FileDoesNotExistException
2929
*/
30-
public function __construct(string $filepath)
30+
public function __construct(string $filepath, string $name = null)
3131
{
3232
$this->validate($filepath);
33-
$this->name = basename($filepath);
33+
$this->name = $name ?? basename($filepath);
3434
$this->content = base64_encode(rtrim(file_get_contents($filepath)));
3535
}
3636

src/Messaging/Mail/MailBag.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class MailBag implements \JsonSerializable
3333
private $headers = [];
3434

3535
/**
36-
* @var Attachment[]
36+
* @var AttachmentInterface[]
3737
*/
3838
private $attachments = [];
3939

@@ -113,13 +113,11 @@ public function setTemplateHash(string $hash): void
113113
}
114114

115115
/**
116-
* @param string $filepath
117-
* @throws ExternalFileException
118-
* @throws Exception\FileDoesNotExistException
116+
* @param AttachmentInterface $attachment
119117
*/
120-
public function addAttachment(string $filepath): void
118+
public function addAttachment(AttachmentInterface $attachment): void
121119
{
122-
$this->attachments[] = new Attachment($filepath);
120+
$this->attachments[] = $attachment;
123121
}
124122

125123
/**
@@ -144,7 +142,7 @@ public function addHeaders(array $headers): void
144142
}
145143

146144
/**
147-
* @return Attachment[]
145+
* @return AttachmentInterface[]
148146
*/
149147
public function getAttachments(): array
150148
{

0 commit comments

Comments
 (0)