MailAddress.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 <brion@pobox.com>
  22. * @author <mail@tgries.de>
  23. * @author Tim Starling
  24. * @author Luke Welling lwelling@wikimedia.org
  25. */
  26. /**
  27. * Stores a single person's name and email address.
  28. * These are passed in via the constructor, and will be returned in SMTP
  29. * header format when requested.
  30. */
  31. class MailAddress {
  32. /**
  33. * @var string
  34. */
  35. public $name;
  36. /**
  37. * @var string
  38. */
  39. public $realName;
  40. /**
  41. * @var string
  42. */
  43. public $address;
  44. /**
  45. * @param string $address String with an email address, or a User object
  46. * @param string $name Human-readable name if a string address is given
  47. * @param string $realName Human-readable real name if a string address is given
  48. */
  49. function __construct( $address, $name = null, $realName = null ) {
  50. if ( is_object( $address ) && $address instanceof User ) {
  51. // Old calling format, now deprecated
  52. wfDeprecated( __METHOD__ . ' with a User object', '1.24' );
  53. $this->address = $address->getEmail();
  54. $this->name = $address->getName();
  55. $this->realName = $address->getRealName();
  56. } else {
  57. $this->address = strval( $address );
  58. $this->name = strval( $name );
  59. $this->realName = strval( $realName );
  60. }
  61. }
  62. /**
  63. * Create a new MailAddress object for the given user
  64. *
  65. * @since 1.24
  66. * @param User $user
  67. * @return MailAddress
  68. */
  69. public static function newFromUser( User $user ) {
  70. return new MailAddress( $user->getEmail(), $user->getName(), $user->getRealName() );
  71. }
  72. /**
  73. * Return formatted and quoted address to insert into SMTP headers
  74. * @return string
  75. */
  76. function toString() {
  77. # PHP's mail() implementation under Windows is somewhat shite, and
  78. # can't handle "Joe Bloggs <joe@bloggs.com>" format email addresses,
  79. # so don't bother generating them
  80. if ( $this->address ) {
  81. if ( $this->name != '' && !wfIsWindows() ) {
  82. global $wgEnotifUseRealName;
  83. $name = ( $wgEnotifUseRealName && $this->realName !== '' ) ? $this->realName : $this->name;
  84. $quoted = UserMailer::quotedPrintable( $name );
  85. // Must only be quoted if string does not use =? encoding (T191931)
  86. if ( $quoted === $name ) {
  87. $quoted = '"' . addslashes( $quoted ) . '"';
  88. }
  89. return "$quoted <{$this->address}>";
  90. } else {
  91. return $this->address;
  92. }
  93. } else {
  94. return "";
  95. }
  96. }
  97. function __toString() {
  98. return $this->toString();
  99. }
  100. }