Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
MailHelper
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
2 / 2
4
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 sendMailToNewUser
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3/**
4 * This class is a helper class for sending mails.
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 2020-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     2020-02-23
16 */
17
18declare(strict_types=1);
19
20namespace phpMyFAQ\Helper;
21
22use phpMyFAQ\Configuration;
23use phpMyFAQ\Core\Exception;
24use phpMyFAQ\Mail;
25use phpMyFAQ\Translation;
26use phpMyFAQ\User;
27use phpMyFAQ\Utils;
28use SensitiveParameter;
29use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
30
31/**
32 * Class MailHelper
33 *
34 * @package phpMyFAQ\Helper
35 */
36readonly class MailHelper
37{
38    private Mail $mail;
39
40    /**
41     * MailHelper constructor.
42     */
43    public function __construct(
44        private Configuration $configuration,
45    ) {
46        $this->mail = new Mail($this->configuration);
47    }
48
49    /**
50     * @throws Exception
51     * @throws TransportExceptionInterface
52     */
53    public function sendMailToNewUser(User $user, #[SensitiveParameter] string $password): bool
54    {
55        $displayName = $user->getUserData('display_name');
56        $displayName = is_string($displayName) ? $displayName : '';
57        $email = $user->getUserData('email');
58        $email = is_string($email) ? $email : '';
59
60        $text = sprintf(
61            '<p>You have been registered as a new user:</p><p>Name: %s<br>Login name: %s<br>Password: %s</p>'
62            . '<p><a href="%s">Check it out here</a></p>',
63            $displayName,
64            $user->getLogin(),
65            $password,
66            $this->configuration->getDefaultUrl(),
67        );
68
69        $this->mail->addTo($email, $displayName);
70        $this->mail->subject = Utils::resolveMarkers(Translation::getString('emailRegSubject'), $this->configuration);
71        $this->mail->message = $text;
72
73        return (bool) $this->mail->send();
74    }
75}