Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
94.74% covered (success)
94.74%
18 / 19
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
Smtp
94.74% covered (success)
94.74%
18 / 19
66.67% covered (warning)
66.67%
2 / 3
11.02
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
5
 create
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
5
 send
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2declare(strict_types=1);
3/**
4 * Pop PHP Framework (https://www.popphp.org/)
5 *
6 * @link       https://github.com/popphp/popphp-framework
7 * @author     Nick Sagona, III <nick@popphp.org>
8 * @copyright  Copyright (c) 2009-2026 Nick Sagona, III
9 * @license    https://www.popphp.org/license     New BSD License
10 */
11
12/**
13 * @namespace
14 */
15namespace Pop\Mail\Transport;
16
17use Pop\Mail\Message;
18use Pop\Mail\Transport\Smtp\Stream\BufferInterface;
19
20/**
21 * SMTP transport class
22 *
23 * @category   Pop
24 * @package    Pop\Mail
25 * @author     Nick Sagona, III <nick@popphp.org>
26 * @copyright  Copyright (c) 2009-2026 Nick Sagona, III
27 * @license    https://www.popphp.org/license     New BSD License
28 * @version    5.0.0
29 */
30class Smtp extends Smtp\EsmtpTransport implements TransportInterface
31{
32
33    /**
34     * Create a new SMTP transport
35     *
36     * Accepts either positional arguments or an options array as the first argument,
37     * keyed by 'host', 'port', 'username', 'password' and 'encryption'.
38     *
39     * @param array|string     $host
40     * @param int              $port
41     * @param ?string          $security
42     * @param ?BufferInterface $buffer
43     * @param ?array           $handlers
44     */
45    public function __construct(
46        array|string $host = 'localhost', int $port = 25, ?string $security = null, ?BufferInterface $buffer = null,
47        ?array $handlers = null
48    )
49    {
50        $options = [];
51
52        // Accept an options array as the first argument, matching how the API-driven
53        // transports (Mailgun, Sendgrid, Ses) are constructed and how the README
54        // documents the SMTP transport
55        if (is_array($host)) {
56            $options  = $host;
57            $host     = $options['host'] ?? 'localhost';
58            $port     = (int)($options['port'] ?? $port);
59            $security = $options['encryption'] ?? $security;
60        }
61
62        parent::__construct($buffer, $handlers);
63
64        $this->setHost($host);
65        $this->setPort($port);
66        if ($security !== null) {
67            $this->setEncryption($security);
68        }
69        if (!empty($options['username'])) {
70            $this->setUsername($options['username']);
71        }
72        if (!empty($options['password'])) {
73            $this->setPassword($options['password']);
74        }
75    }
76
77
78    /**
79     * Create the SMTP transport
80     *
81     * @param  array $options
82     * @throws Exception
83     * @return Smtp
84     */
85    public static function create(array $options): Smtp
86    {
87        if (empty($options['host']) || empty($options['port']) || empty($options['username']) || empty($options['password'])) {
88            throw new Exception('Error: The required credentials were not provided.');
89        }
90
91        return new Smtp($options);
92    }
93
94    /**
95     * Send the message
96     *
97     * @param  Message $message
98     * @return int
99     */
100    public function send(Message $message): int
101    {
102        return parent::send($message);
103    }
104
105}