vendor/swiftmailer/swiftmailer/lib/classes/Swift/Mime/Headers/MailboxHeader.php line 74

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of SwiftMailer.
  4.  * (c) 2004-2009 Chris Corbyn
  5.  *
  6.  * For the full copyright and license information, please view the LICENSE
  7.  * file that was distributed with this source code.
  8.  */
  9. use Egulias\EmailValidator\EmailValidator;
  10. use Egulias\EmailValidator\Validation\RFCValidation;
  11. /**
  12.  * A Mailbox Address MIME Header for something like From or Sender.
  13.  *
  14.  * @author Chris Corbyn
  15.  */
  16. class Swift_Mime_Headers_MailboxHeader extends Swift_Mime_Headers_AbstractHeader
  17. {
  18.     /**
  19.      * The mailboxes used in this Header.
  20.      *
  21.      * @var string[]
  22.      */
  23.     private $mailboxes = [];
  24.     /**
  25.      * The strict EmailValidator.
  26.      *
  27.      * @var EmailValidator
  28.      */
  29.     private $emailValidator;
  30.     private $addressEncoder;
  31.     /**
  32.      * Creates a new MailboxHeader with $name.
  33.      *
  34.      * @param string $name of Header
  35.      */
  36.     public function __construct($nameSwift_Mime_HeaderEncoder $encoderEmailValidator $emailValidatorSwift_AddressEncoder $addressEncoder null)
  37.     {
  38.         $this->setFieldName($name);
  39.         $this->setEncoder($encoder);
  40.         $this->emailValidator $emailValidator;
  41.         $this->addressEncoder $addressEncoder ?? new Swift_AddressEncoder_IdnAddressEncoder();
  42.     }
  43.     /**
  44.      * Get the type of Header that this instance represents.
  45.      *
  46.      * @see TYPE_TEXT, TYPE_PARAMETERIZED, TYPE_MAILBOX
  47.      * @see TYPE_DATE, TYPE_ID, TYPE_PATH
  48.      *
  49.      * @return int
  50.      */
  51.     public function getFieldType()
  52.     {
  53.         return self::TYPE_MAILBOX;
  54.     }
  55.     /**
  56.      * Set the model for the field body.
  57.      *
  58.      * This method takes a string, or an array of addresses.
  59.      *
  60.      * @param mixed $model
  61.      *
  62.      * @throws Swift_RfcComplianceException
  63.      */
  64.     public function setFieldBodyModel($model)
  65.     {
  66.         $this->setNameAddresses($model);
  67.     }
  68.     /**
  69.      * Get the model for the field body.
  70.      *
  71.      * This method returns an associative array like {@link getNameAddresses()}
  72.      *
  73.      * @throws Swift_RfcComplianceException
  74.      *
  75.      * @return array
  76.      */
  77.     public function getFieldBodyModel()
  78.     {
  79.         return $this->getNameAddresses();
  80.     }
  81.     /**
  82.      * Set a list of mailboxes to be shown in this Header.
  83.      *
  84.      * The mailboxes can be a simple array of addresses, or an array of
  85.      * key=>value pairs where (email => personalName).
  86.      * Example:
  87.      * <code>
  88.      * <?php
  89.      * //Sets two mailboxes in the Header, one with a personal name
  90.      * $header->setNameAddresses(array(
  91.      *  'chris@swiftmailer.org' => 'Chris Corbyn',
  92.      *  'mark@swiftmailer.org' //No associated personal name
  93.      *  ));
  94.      * ?>
  95.      * </code>
  96.      *
  97.      * @see __construct()
  98.      * @see setAddresses()
  99.      * @see setValue()
  100.      *
  101.      * @param string|string[] $mailboxes
  102.      *
  103.      * @throws Swift_RfcComplianceException
  104.      */
  105.     public function setNameAddresses($mailboxes)
  106.     {
  107.         $this->mailboxes $this->normalizeMailboxes((array) $mailboxes);
  108.         $this->setCachedValue(null); //Clear any cached value
  109.     }
  110.     /**
  111.      * Get the full mailbox list of this Header as an array of valid RFC 2822 strings.
  112.      *
  113.      * Example:
  114.      * <code>
  115.      * <?php
  116.      * $header = new Swift_Mime_Headers_MailboxHeader('From',
  117.      *  array('chris@swiftmailer.org' => 'Chris Corbyn',
  118.      *  'mark@swiftmailer.org' => 'Mark Corbyn')
  119.      *  );
  120.      * print_r($header->getNameAddressStrings());
  121.      * // array (
  122.      * // 0 => Chris Corbyn <chris@swiftmailer.org>,
  123.      * // 1 => Mark Corbyn <mark@swiftmailer.org>
  124.      * // )
  125.      * ?>
  126.      * </code>
  127.      *
  128.      * @see getNameAddresses()
  129.      * @see toString()
  130.      *
  131.      * @throws Swift_RfcComplianceException
  132.      *
  133.      * @return string[]
  134.      */
  135.     public function getNameAddressStrings()
  136.     {
  137.         return $this->createNameAddressStrings($this->getNameAddresses());
  138.     }
  139.     /**
  140.      * Get all mailboxes in this Header as key=>value pairs.
  141.      *
  142.      * The key is the address and the value is the name (or null if none set).
  143.      * Example:
  144.      * <code>
  145.      * <?php
  146.      * $header = new Swift_Mime_Headers_MailboxHeader('From',
  147.      *  array('chris@swiftmailer.org' => 'Chris Corbyn',
  148.      *  'mark@swiftmailer.org' => 'Mark Corbyn')
  149.      *  );
  150.      * print_r($header->getNameAddresses());
  151.      * // array (
  152.      * // chris@swiftmailer.org => Chris Corbyn,
  153.      * // mark@swiftmailer.org => Mark Corbyn
  154.      * // )
  155.      * ?>
  156.      * </code>
  157.      *
  158.      * @see getAddresses()
  159.      * @see getNameAddressStrings()
  160.      *
  161.      * @return string[]
  162.      */
  163.     public function getNameAddresses()
  164.     {
  165.         return $this->mailboxes;
  166.     }
  167.     /**
  168.      * Makes this Header represent a list of plain email addresses with no names.
  169.      *
  170.      * Example:
  171.      * <code>
  172.      * <?php
  173.      * //Sets three email addresses as the Header data
  174.      * $header->setAddresses(
  175.      *  array('one@domain.tld', 'two@domain.tld', 'three@domain.tld')
  176.      *  );
  177.      * ?>
  178.      * </code>
  179.      *
  180.      * @see setNameAddresses()
  181.      * @see setValue()
  182.      *
  183.      * @param string[] $addresses
  184.      *
  185.      * @throws Swift_RfcComplianceException
  186.      */
  187.     public function setAddresses($addresses)
  188.     {
  189.         $this->setNameAddresses(array_values((array) $addresses));
  190.     }
  191.     /**
  192.      * Get all email addresses in this Header.
  193.      *
  194.      * @see getNameAddresses()
  195.      *
  196.      * @return string[]
  197.      */
  198.     public function getAddresses()
  199.     {
  200.         return array_keys($this->mailboxes);
  201.     }
  202.     /**
  203.      * Remove one or more addresses from this Header.
  204.      *
  205.      * @param string|string[] $addresses
  206.      */
  207.     public function removeAddresses($addresses)
  208.     {
  209.         $this->setCachedValue(null);
  210.         foreach ((array) $addresses as $address) {
  211.             unset($this->mailboxes[$address]);
  212.         }
  213.     }
  214.     /**
  215.      * Get the string value of the body in this Header.
  216.      *
  217.      * This is not necessarily RFC 2822 compliant since folding white space will
  218.      * not be added at this stage (see {@link toString()} for that).
  219.      *
  220.      * @see toString()
  221.      *
  222.      * @throws Swift_RfcComplianceException
  223.      *
  224.      * @return string
  225.      */
  226.     public function getFieldBody()
  227.     {
  228.         // Compute the string value of the header only if needed
  229.         if (null === $this->getCachedValue()) {
  230.             $this->setCachedValue($this->createMailboxListString($this->mailboxes));
  231.         }
  232.         return $this->getCachedValue();
  233.     }
  234.     /**
  235.      * Normalizes a user-input list of mailboxes into consistent key=>value pairs.
  236.      *
  237.      * @param string[] $mailboxes
  238.      *
  239.      * @return string[]
  240.      */
  241.     protected function normalizeMailboxes(array $mailboxes)
  242.     {
  243.         $actualMailboxes = [];
  244.         foreach ($mailboxes as $key => $value) {
  245.             if (is_string($key)) {
  246.                 //key is email addr
  247.                 $address $key;
  248.                 $name $value;
  249.             } else {
  250.                 $address $value;
  251.                 $name null;
  252.             }
  253.             $this->assertValidAddress($address);
  254.             $actualMailboxes[$address] = $name;
  255.         }
  256.         return $actualMailboxes;
  257.     }
  258.     /**
  259.      * Produces a compliant, formatted display-name based on the string given.
  260.      *
  261.      * @param string $displayName as displayed
  262.      * @param bool   $shorten     the first line to make remove for header name
  263.      *
  264.      * @return string
  265.      */
  266.     protected function createDisplayNameString($displayName$shorten false)
  267.     {
  268.         return $this->createPhrase($this$displayName$this->getCharset(), $this->getEncoder(), $shorten);
  269.     }
  270.     /**
  271.      * Creates a string form of all the mailboxes in the passed array.
  272.      *
  273.      * @param string[] $mailboxes
  274.      *
  275.      * @throws Swift_RfcComplianceException
  276.      *
  277.      * @return string
  278.      */
  279.     protected function createMailboxListString(array $mailboxes)
  280.     {
  281.         return implode(', '$this->createNameAddressStrings($mailboxes));
  282.     }
  283.     /**
  284.      * Redefine the encoding requirements for mailboxes.
  285.      *
  286.      * All "specials" must be encoded as the full header value will not be quoted
  287.      *
  288.      * @see RFC 2822 3.2.1
  289.      *
  290.      * @param string $token
  291.      *
  292.      * @return bool
  293.      */
  294.     protected function tokenNeedsEncoding($token)
  295.     {
  296.         return preg_match('/[()<>\[\]:;@\,."]/'$token) || parent::tokenNeedsEncoding($token);
  297.     }
  298.     /**
  299.      * Return an array of strings conforming the the name-addr spec of RFC 2822.
  300.      *
  301.      * @param string[] $mailboxes
  302.      *
  303.      * @return string[]
  304.      */
  305.     private function createNameAddressStrings(array $mailboxes)
  306.     {
  307.         $strings = [];
  308.         foreach ($mailboxes as $email => $name) {
  309.             $mailboxStr $this->addressEncoder->encodeString($email);
  310.             if (null !== $name) {
  311.                 $nameStr $this->createDisplayNameString($name, empty($strings));
  312.                 $mailboxStr $nameStr.' <'.$mailboxStr.'>';
  313.             }
  314.             $strings[] = $mailboxStr;
  315.         }
  316.         return $strings;
  317.     }
  318.     /**
  319.      * Throws an Exception if the address passed does not comply with RFC 2822.
  320.      *
  321.      * @param string $address
  322.      *
  323.      * @throws Swift_RfcComplianceException If invalid.
  324.      */
  325.     private function assertValidAddress($address)
  326.     {
  327.         if (!$this->emailValidator->isValid($address, new RFCValidation())) {
  328.             throw new Swift_RfcComplianceException(
  329.                 'Address in mailbox given ['.$address.'] does not comply with RFC 2822, 3.6.2.'
  330.             );
  331.         }
  332.     }
  333. }