Swift_Transport_AbstractSmtpTransport::send PHP Method

send() public method

Recipient/sender data will be retrieved from the Message API. The return value is the number of recipients who were accepted for delivery.
public send ( Swift_Mime_Message $message, string[] &$failedRecipients = null ) : integer
$message Swift_Mime_Message
$failedRecipients string[] An array of failures by-reference
return integer
    public function send(Swift_Mime_Message $message, &$failedRecipients = null)
    {
        $sent = 0;
        $failedRecipients = (array) $failedRecipients;
        if ($evt = $this->_eventDispatcher->createSendEvent($this, $message)) {
            $this->_eventDispatcher->dispatchEvent($evt, 'beforeSendPerformed');
            if ($evt->bubbleCancelled()) {
                return 0;
            }
        }
        if (!($reversePath = $this->_getReversePath($message))) {
            $this->_throwException(new Swift_TransportException('Cannot send message without a sender address'));
        }
        $to = (array) $message->getTo();
        $cc = (array) $message->getCc();
        $tos = array_merge($to, $cc);
        $bcc = (array) $message->getBcc();
        $message->setBcc(array());
        try {
            $sent += $this->_sendTo($message, $reversePath, $tos, $failedRecipients);
            $sent += $this->_sendBcc($message, $reversePath, $bcc, $failedRecipients);
        } catch (Exception $e) {
            $message->setBcc($bcc);
            throw $e;
        }
        $message->setBcc($bcc);
        if ($evt) {
            if ($sent == count($to) + count($cc) + count($bcc)) {
                $evt->setResult(Swift_Events_SendEvent::RESULT_SUCCESS);
            } elseif ($sent > 0) {
                $evt->setResult(Swift_Events_SendEvent::RESULT_TENTATIVE);
            } else {
                $evt->setResult(Swift_Events_SendEvent::RESULT_FAILED);
            }
            $evt->setFailedRecipients($failedRecipients);
            $this->_eventDispatcher->dispatchEvent($evt, 'sendPerformed');
        }
        $message->generateId();
        //Make sure a new Message ID is used
        return $sent;
    }

Usage Example

 /**
  * Send the given Message.
  *
  * Recipient/sender data will be retrieved from the Message API.
  *
  * The return value is the number of recipients who were accepted for delivery.
  * NOTE: If using 'sendmail -t' you will not be aware of any failures until
  * they bounce (i.e. send() will always return 100% success).
  *
  * @param Swift_Mime_Message $message
  * @param string[]           $failedRecipients An array of failures by-reference
  *
  * @return int
  */
 public function send(Swift_Mime_Message $message, &$failedRecipients = null)
 {
     $failedRecipients = (array) $failedRecipients;
     $command = $this->getCommand();
     $buffer = $this->getBuffer();
     if (false !== strpos($command, ' -t')) {
         if ($evt = $this->_eventDispatcher->createSendEvent($this, $message)) {
             $this->_eventDispatcher->dispatchEvent($evt, 'beforeSendPerformed');
             if ($evt->bubbleCancelled()) {
                 return 0;
             }
         }
         if (false === strpos($command, ' -f')) {
             $command .= ' -f' . escapeshellarg($this->_getReversePath($message));
         }
         $buffer->initialize(array_merge($this->_params, array('command' => $command)));
         if (false === strpos($command, ' -i') && false === strpos($command, ' -oi')) {
             $buffer->setWriteTranslations(array("\r\n" => "\n", "\n." => "\n.."));
         } else {
             $buffer->setWriteTranslations(array("\r\n" => "\n"));
         }
         $count = count((array) $message->getTo()) + count((array) $message->getCc()) + count((array) $message->getBcc());
         $message->toByteStream($buffer);
         $buffer->flushBuffers();
         $buffer->setWriteTranslations(array());
         $buffer->terminate();
         if ($evt) {
             $evt->setResult(Swift_Events_SendEvent::RESULT_SUCCESS);
             $evt->setFailedRecipients($failedRecipients);
             $this->_eventDispatcher->dispatchEvent($evt, 'sendPerformed');
         }
         $message->generateId();
     } elseif (false !== strpos($command, ' -bs')) {
         $count = parent::send($message, $failedRecipients);
     } else {
         $this->_throwException(new Swift_TransportException('Unsupported sendmail command flags [' . $command . ']. ' . 'Must be one of "-bs" or "-t" but can include additional flags.'));
     }
     return $count;
 }