Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
64 / 64
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
Ses
100.00% covered (success)
100.00%
64 / 64
100.00% covered (success)
100.00%
2 / 2
20
100.00% covered (success)
100.00%
1 / 1
 createClient
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
4
 send
100.00% covered (success)
100.00%
49 / 49
100.00% covered (success)
100.00%
1 / 1
16
1<?php
2/**
3 * Pop PHP Framework (http://www.popphp.org/)
4 *
5 * @link       https://github.com/popphp/popphp-framework
6 * @author     Nick Sagona, III <dev@nolainteractive.com>
7 * @copyright  Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com)
8 * @license    http://www.popphp.org/license     New BSD License
9 */
10
11/**
12 * @namespace
13 */
14namespace Pop\Mail\Transport;
15
16use Pop\Http\Client;
17use Pop\Mail\Api\AbstractHttp;
18use Pop\Mail\Message;
19use Aws\Ses\SesClient;
20use Pop\Mail\Message\Attachment;
21use Pop\Mail\Message\Html;
22use Pop\Mail\Message\Text;
23
24/**
25 * Mailgun API transport class
26 *
27 * @category   Pop
28 * @package    Pop\Mail
29 * @author     Nick Sagona, III <dev@nolainteractive.com>
30 * @copyright  Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com)
31 * @license    http://www.popphp.org/license     New BSD License
32 * @version    4.0.0
33 */
34class Ses extends AbstractHttp implements TransportInterface
35{
36
37    /**
38     * Create the API client
39     *
40     * @param  array|string $options
41     * @throws Exception|\Pop\Mail\Api\Exception
42     * @return Ses
43     */
44    public function createClient(array|string $options): Ses
45    {
46        if (is_string($options)) {
47            $options = $this->parseOptions($options);
48        }
49
50        $key    = $options['key'] ?? null;
51        $secret = $options['secret'] ?? null;
52
53        if (($key === null) || ($secret === null)) {
54            throw new Exception('Error: The required credentials to create the client object are missing.');
55        }
56
57        $this->client = new SesClient([
58            'credentials' => [
59                'key'     => $key,
60                'secret'  => $secret
61            ],
62            'version' => 'latest',
63            'region'  => $options['region'] ?? 'us-east-1'
64        ]);
65
66        return $this;
67    }
68
69    /**
70     * Send the message
71     *
72     * @param  Message $message
73     * @return mixed
74     */
75    public function send(Message $message): mixed
76    {
77        if ($message->hasAttachments()) {
78            return $this->client->sendRawEmail([
79                'RawMessage' => [
80                    'Data' => $message->render()
81                ]
82            ]);
83        } else {
84            $parts       = $message->getParts();
85            $messageData = [
86                'Destination' => [
87                    'ToAddresses' => []
88                ],
89                'Message' => [
90                    'Subject' => [
91                        'Data' => $message->getSubject()
92                    ],
93                    'Body' => []
94                ],
95                'Source' => array_key_first($message->getFrom())
96            ];
97
98            $toAddresses       = $message->getTo();
99            $ccAddresses       = $message->getCc();
100            $bccAddresses      = $message->getBcc();
101            $replyToAddress    = $message->getReplyTo();
102            $returnPathAddress = $message->getReturnPath();
103
104            if (!empty($toAddresses)) {
105                foreach ($toAddresses as $email => $name) {
106                    $messageData['Destination']['ToAddresses'][] = (!empty($name)) ? $name . ' <' . $email . '>' : $email;
107                }
108            }
109            if (!empty($ccAddresses)) {
110                $messageData['Destination']['CcAddresses'] = [];
111                foreach ($ccAddresses as $email => $name) {
112                    $messageData['Destination']['CcAddresses'][] = (!empty($name)) ? $name . ' <' . $email . '>' : $email;
113                }
114            }
115            if (!empty($bccAddresses)) {
116                $messageData['Destination']['BccAddresses'] = [];
117                foreach ($bccAddresses as $email => $name) {
118                    $messageData['Destination']['BccAddresses'][] = (!empty($name)) ? $name . ' <' . $email . '>' : $email;
119                }
120            }
121            if (!empty($replyToAddress)) {
122                $messageData['ReplyToAddresses'] = [array_key_first($replyToAddress)];
123            }
124            if (!empty($returnPathAddress)) {
125                $messageData['ReturnPath'] = array_key_first($returnPathAddress);
126            }
127
128            foreach ($parts as $part) {
129                if ($part instanceof Text) {
130                    $messageData['Message']['Body']['Text'] = [
131                        'Data' => $part->getBody()
132                    ];
133                } else if ($part instanceof Html) {
134                    $messageData['Message']['Body']['Html'] = [
135                        'Data' => $part->getBody()
136                    ];
137                }
138            }
139
140            return $this->client->sendEmail($messageData);
141        }
142    }
143
144}