3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
19 * @author Brooke Vibber
20 * @author <mail@tgries.de>
21 * @author Tim Starling
22 * @author Luke Welling lwelling@wikimedia.org
25 use MediaWiki\Mail\UserEmailContact
;
28 * Represent and format a single name and email address pair for SMTP.
30 * Used by Emailer, e.g. via EmailUser or EmailNotification.
36 class MailAddress
implements Stringable
{
39 public string $realName;
40 public string $address;
45 * @param string $address String with an email address
46 * @param string|null $name Human-readable name if a string address is given
47 * @param string|null $realName Human-readable real name if a string address is given
49 public function __construct( $address, $name = null, $realName = null ) {
50 $this->address
= strval( $address );
51 $this->name
= strval( $name );
52 $this->realName
= strval( $realName );
57 * @param UserEmailContact $user
60 public static function newFromUser( UserEmailContact
$user ) {
61 return new MailAddress( $user->getEmail(), $user->getUser()->getName(), $user->getRealName() );
69 public function equals( self
$other ): bool {
70 return $this->address
=== $other->address
&&
71 $this->name
=== $other->name
&&
72 $this->realName
=== $other->realName
;
76 * Format and quote address for insertion in SMTP headers
81 public function toString() {
82 if ( !$this->address
) {
86 # PHP's mail() implementation under Windows is somewhat shite, and
87 # can't handle "Joe Bloggs <joe@bloggs.com>" format email addresses,
88 # so don't bother generating them
89 if ( $this->name
=== '' ||
wfIsWindows() ) {
90 return $this->address
;
93 global $wgEnotifUseRealName;
94 $name = ( $wgEnotifUseRealName && $this->realName
!== '' ) ?
$this->realName
: $this->name
;
95 $quoted = UserMailer
::quotedPrintable( $name );
96 // Must only be quoted if string does not use =? encoding (T191931)
97 if ( $quoted === $name ) {
98 $quoted = '"' . addslashes( $quoted ) . '"';
101 return "$quoted <{$this->address}>";
104 public function __toString() {
105 return $this->toString();