/** * Constructor * * @param MailMessage|array|Traversable $mail * @param Transport\TransportInterface $transport Optional * @throws Exception\InvalidArgumentException */ public function __construct($mail, Transport\TransportInterface $transport = null) { if ($mail instanceof Traversable) { $mail = iterator_to_array($mail); } if (is_array($mail)) { parent::__construct($mail); if (isset($mail['subject_prepend_text'])) { $this->setSubjectPrependText($mail['subject_prepend_text']); } $transport = isset($mail['transport']) ? $mail['transport'] : null; $mail = isset($mail['mail']) ? $mail['mail'] : null; if (is_array($mail)) { $mail = MailMessageFactory::getInstance($mail); } } // Ensure we have a valid mail message if (!$mail instanceof MailMessage) { throw new Exception\InvalidArgumentException(sprintf('Mail parameter of type %s is invalid; must be of type Zend\\Mail\\Message', is_object($mail) ? get_class($mail) : gettype($mail))); } $this->mail = $mail; // Ensure we have a valid mail transport if (null === $transport) { $transport = new Transport\Sendmail(); } if (!$transport instanceof Transport\TransportInterface) { throw new Exception\InvalidArgumentException(sprintf('Transport parameter of type %s is invalid; must be of type Zend\\Mail\\Transport\\TransportInterface', is_object($transport) ? get_class($transport) : gettype($transport))); } $this->setTransport($transport); if ($this->formatter === null) { $this->formatter = new SimpleFormatter(); } }
/** * Return a new message object. * * Sets default options if not already set. * * @param array|Traversable $options * @return Message */ public function createMessage($options = []) { if ($options instanceof Traversable) { $options = ArrayUtils::iteratorToArray($options); } $options = array_merge($this->defaultOptions, $options); return MessageFactory::getInstance($options); }