Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
33 / 33
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
Mailgun
100.00% covered (success)
100.00%
33 / 33
100.00% covered (success)
100.00%
2 / 2
13
100.00% covered (success)
100.00%
1 / 1
 createClient
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
4
 send
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
1 / 1
9
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;
19
20/**
21 * Mailgun API transport class
22 *
23 * @category   Pop
24 * @package    Pop\Mail
25 * @author     Nick Sagona, III <dev@nolainteractive.com>
26 * @copyright  Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com)
27 * @license    http://www.popphp.org/license     New BSD License
28 * @version    4.0.0
29 */
30class Mailgun extends AbstractHttp implements TransportInterface
31{
32
33    /**
34     * Create the API client
35     *
36     * @param  array|string $options
37     * @throws Exception|Client\Exception|\Pop\Mail\Api\Exception
38     * @return Mailgun
39     */
40    public function createClient(array|string $options): Mailgun
41    {
42        if (is_string($options)) {
43            $options = $this->parseOptions($options);
44        }
45
46        if (!isset($options['api_url']) || !isset($options['api_key'])) {
47            throw new Exception('Error: The required client options were not provided.');
48        }
49
50        $request = new Client\Request($options['api_url'], 'POST');
51        $request->addHeader('Authorization', 'Basic ' . base64_encode('api:' . $options['api_key']));
52        $this->client = new Client($request, new Client\Handler\Curl(), ['force_custom_method' => true]);
53
54        return $this;
55    }
56
57    /**
58     * Send the message
59     *
60     * @param  Message $message
61     * @return mixed
62     */
63    public function send(Message $message): mixed
64    {
65        $fields         = [];
66        $headers        = $message->getHeaders();
67        $parts          = $message->getParts();
68        $primaryHeaders = ['Subject', 'To', 'From', 'CC', 'BCC'];
69
70        foreach ($headers as $header => $value) {
71            if (!in_array($header, $primaryHeaders)) {
72                $header = 'h:' . $header;
73            } else {
74                $header = strtolower($header);
75            }
76            $fields[$header] = $value;
77        }
78
79        $i = 0;
80
81        foreach ($parts as $part) {
82            if ($part instanceof Message\Text) {
83                $fields['text'] = $part->getBody();
84            } else if ($part instanceof Message\Html) {
85                $fields['html'] = $part->getBody();
86            } else if ($part instanceof Message\Attachment) {
87                $contentType = $part->getContentType();
88                if (str_contains($contentType, ';')) {
89                    $contentType = trim(substr($contentType, 0, strpos($contentType, ';')));
90                }
91                $fields['attachment[' . $i . ']'] = curl_file_create($part->getFilename(), $contentType, $part->getBasename());
92                $i++;
93            }
94        }
95
96        $this->client->setData($fields);
97
98        if ($i > 0) {
99            $this->client->getRequest()->addHeader('Content-Type', 'multipart/form-data');
100        }
101
102        return $this->client->send();
103    }
104
105}