PHPMailerMailer::send()   A
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 13
nc 8
nop 1
dl 0
loc 24
rs 9.8333
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
/**
4
 * Copyright (c) Phauthentic (https://github.com/Phauthentic)
5
 *
6
 * Licensed under The MIT License
7
 * For full copyright and license information, please see the LICENSE.txt
8
 * Redistributions of files must retain the above copyright notice.
9
 *
10
 * @copyright     Copyright (c) Phauthentic (https://github.com/Phauthentic)
11
 * @link          https://github.com/Phauthentic
12
 * @license       https://opensource.org/licenses/mit-license.php MIT License
13
 */
14
namespace Phauthentic\Email\Mailer;
15
16
use Phauthentic\Email\EmailInterface;
17
use PHPMailer\PHPMailer\PHPMailer;
18
19
/**
20
 * PHP Mailer
21
 */
22
class PHPMailerMailer implements MailerInterface
23
{
24
    /**
25
     * @var \PHPMailer\PHPMailer\PHPMailer
26
     */
27
    protected $mailer;
28
29
    /**
30
     * Constructor
31
     *
32
     * @param \PHPMailer $mailer PHPMailer instance
33
     */
34
    public function __construct(PHPMailer $mailer)
35
    {
36
        $this->mailer = $mailer;
37
    }
38
39
    /**
40
     * @inheritDoc
41
     */
42
    public function send(EmailInterface $email): bool
43
    {
44
        $mailer = clone $this->mailer;
45
46
        $sender = $email->getSender();
47
        $mailer->setFrom($sender->getEmail(), $sender->getName());
48
49
        foreach ($email->getReceivers() as $receiver) {
50
            $mailer->addAddress($receiver->getEmail(), $receiver->getName());
51
        }
52
53
        foreach ($email->getBcc() as $receiver) {
54
            $mailer->addCC($receiver->getEmail(), $receiver->getName());
55
        }
56
57
        foreach ($email->getCc() as $receiver) {
58
            $mailer->addBCC($receiver->getEmail(), $receiver->getName());
59
        }
60
61
        $mailer->Subject = $email->getSubject();
62
        $mailer->Body = $email->getHtmlContent();
63
        $mailer->AltBody = $email->getTextContent();
64
65
        return $mailer->send();
66
    }
67
}
68