Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
Smtp
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
2 / 2
11
100.00% covered (success)
100.00%
1 / 1
 setAuthConfig
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 send
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
1 / 1
9
1<?php
2
3/**
4 * MUA (Mail User Agent) implementation using the Symfony Mailer class.
5 *
6 * This Source Code Form is subject to the terms of the Mozilla Public License,
7 * v. 2.0. If a copy of the MPL was not distributed with this file, You can
8 * obtain one at https://mozilla.org/MPL/2.0/.
9 *
10 * @package   phpMyFAQ
11 * @author    Thorsten Rinne <thorsten@phpmyfaq.de>
12 * @copyright 2022-2026 phpMyFAQ Team
13 * @license   https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
14 * @link      https://www.phpmyfaq.de
15 * @since     2022-01-23
16 */
17
18declare(strict_types=1);
19
20namespace phpMyFAQ\Mail;
21
22use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
23use Symfony\Component\Mailer\Mailer;
24use Symfony\Component\Mailer\MailerInterface;
25use Symfony\Component\Mailer\Transport;
26use Symfony\Component\Mime\Email;
27
28/**
29 * Class SMTP
30 */
31class Smtp implements MailUserAgentInterface
32{
33    private string $user;
34
35    private MailerInterface $mailer;
36
37    public function setAuthConfig(
38        string $server,
39        string $user = '',
40        #[\SensitiveParameter]
41        string $password = '',
42        int $port = 25,
43        bool $disableTlsPeerVerification = false,
44    ): void {
45        $dsn = sprintf('smtp://%s:%s@%s:%d', $this->user = $user, urlencode($password), $server, $port);
46
47        if ($disableTlsPeerVerification) {
48            $dsn .= '?verify_peer=0';
49        }
50
51        $this->mailer = new Mailer(Transport::fromDsn($dsn));
52    }
53
54    /**
55     * @param array<string, int|string|null> $headers
56     * @throws TransportExceptionInterface
57     */
58    public function send(string $recipients, array $headers, string $body): int
59    {
60        $sender = '';
61        if (
62            'WIN' !== strtoupper(string: substr(string: PHP_OS, offset: 0, length: 3))
63            && !ini_get(option: 'safe_mode')
64            && array_key_exists('Return-Path', $headers)
65        ) {
66            $sender = str_replace(search: ['<', '>'], replace: '', subject: (string) $headers['Return-Path']);
67            unset($headers['Return-Path']);
68        }
69
70        $email = new Email()
71            ->from($sender === '' ? $this->user : $sender)
72            ->to($recipients)
73            ->subject((string) ($headers['Subject'] ?? ''))
74            ->text($body)
75            ->html($body);
76
77        if (array_key_exists('CC', $headers) || array_key_exists('Cc', $headers)) {
78            $email->cc((string) ($headers['CC'] ?? $headers['Cc'] ?? ''));
79        }
80
81        if (array_key_exists('Bcc', $headers)) {
82            $email->bcc((string) $headers['Bcc']);
83        }
84
85        if (array_key_exists('Reply-To', $headers)) {
86            $email->replyTo((string) $headers['Reply-To']);
87        }
88
89        $this->mailer->send($email);
90
91        return 1;
92    }
93}