Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
92.86% covered (success)
92.86%
13 / 14
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
Smtp
92.86% covered (success)
92.86%
13 / 14
66.67% covered (warning)
66.67%
2 / 3
9.03
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 create
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
6
 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 <dev@noladev.com>
8 * @copyright  Copyright (c) 2009-2027 NOLA Interactive, LLC.
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 <dev@noladev.com>
26 * @copyright  Copyright (c) 2009-2027 NOLA Interactive, LLC.
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     * @param string           $host
37     * @param int              $port
38     * @param ?string          $security
39     * @param ?BufferInterface $buffer
40     * @param ?array           $handlers
41     */
42    public function __construct(
43        string $host = 'localhost', int $port = 25, ?string $security = null, ?BufferInterface $buffer = null, ?array $handlers = null
44    )
45    {
46        parent::__construct($buffer, $handlers);
47
48        $this->setHost($host);
49        $this->setPort($port);
50        if ($security !== null) {
51            $this->setEncryption($security);
52        }
53    }
54
55
56    /**
57     * Create the SMTP transport
58     *
59     * @param  array $options
60     * @throws Exception
61     * @return Smtp
62     */
63    public static function create(array $options): Smtp
64    {
65        if (empty($options['host']) || empty($options['port']) || empty($options['username']) || empty($options['password'])) {
66            throw new Exception('Error: The required credentials were not provided.');
67        }
68
69        $smtp = new Smtp($options['host'], $options['port']);
70        $smtp->setUsername($options['username'])
71            ->setPassword($options['password']);
72
73        if (isset($options['encryption'])) {
74            $smtp->setEncryption($options['encryption']);
75        }
76
77        return $smtp;
78    }
79
80    /**
81     * Send the message
82     *
83     * @param  Message $message
84     * @return int
85     */
86    public function send(Message $message): int
87    {
88        return parent::send($message);
89    }
90
91}