Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
MailAddress
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
5 / 5
13
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 newFromUser
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 equals
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
3
 toString
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
7
 __toString
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * Classes used to send e-mails
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @author Brooke Vibber
22 * @author <mail@tgries.de>
23 * @author Tim Starling
24 * @author Luke Welling lwelling@wikimedia.org
25 */
26
27use MediaWiki\Mail\UserEmailContact;
28
29/**
30 * Stores a single person's name and email address.
31 * These are passed in via the constructor, and will be returned in SMTP
32 * header format when requested.
33 *
34 * @newable
35 */
36class MailAddress {
37
38    public string $name;
39
40    public string $realName;
41
42    public string $address;
43
44    /**
45     * @stable to call
46     *
47     * @param string $address String with an email address
48     * @param string|null $name Human-readable name if a string address is given
49     * @param string|null $realName Human-readable real name if a string address is given
50     */
51    public function __construct( $address, $name = null, $realName = null ) {
52        $this->address = strval( $address );
53        $this->name = strval( $name );
54        $this->realName = strval( $realName );
55    }
56
57    /**
58     * Create a new MailAddress object for the given user
59     *
60     * @param UserEmailContact $user
61     * @return MailAddress
62     * @since 1.24
63     */
64    public static function newFromUser( UserEmailContact $user ) {
65        return new MailAddress( $user->getEmail(), $user->getUser()->getName(), $user->getRealName() );
66    }
67
68    /**
69     * @param self $other
70     * @return bool
71     * @since 1.40
72     */
73    public function equals( self $other ): bool {
74        return $this->address === $other->address &&
75            $this->name === $other->name &&
76            $this->realName === $other->realName;
77    }
78
79    /**
80     * Return formatted and quoted address to insert into SMTP headers
81     * @return string
82     */
83    public function toString() {
84        if ( !$this->address ) {
85            return '';
86        }
87
88        # PHP's mail() implementation under Windows is somewhat shite, and
89        # can't handle "Joe Bloggs <joe@bloggs.com>" format email addresses,
90        # so don't bother generating them
91        if ( $this->name === '' || wfIsWindows() ) {
92            return $this->address;
93        }
94
95        global $wgEnotifUseRealName;
96        $name = ( $wgEnotifUseRealName && $this->realName !== '' ) ? $this->realName : $this->name;
97        $quoted = UserMailer::quotedPrintable( $name );
98        // Must only be quoted if string does not use =? encoding (T191931)
99        if ( $quoted === $name ) {
100            $quoted = '"' . addslashes( $quoted ) . '"';
101        }
102
103        return "$quoted <{$this->address}>";
104    }
105
106    public function __toString() {
107        return $this->toString();
108    }
109}