/** * Sends a previously prepared email Message to the given recipients. * * Returns true on success and false on error. * * @param Message $message Message to be sent. * @param string|array $to Recipient(s) of the email. * @param array $logData [optional] Any additional log data that should be logged regarding this email. * @return bool */ public function sendMessage(Message $message, $to, $logData = array()) { if (!$this->verifyEmailAddress($to)) { throw new InvalidArgumentException('valid email address', $to, 2); } $logData = array_merge($logData, array('to' => $to, 'subject' => $message->getSubject(), 'transport' => $this->config['transport'], 'message' => array('id' => $message->getId()))); if (!empty($this->config['force_to'])) { $message->setTo($this->config['force_to']); $logData['to_forced'] = $this->config['force_to']; } else { $message->setTo($to); } $message->setFrom($this->config['from']); $logData['from'] = $this->config['from']; $totalRecipients = is_string($to) ? 1 : count($to); if (!empty($this->config['bcc'])) { $message->setBcc($this->config['bcc']); $totalRecipients = $totalRecipients + (is_string($this->config['bcc']) ? 1 : count($this->config['bcc'])); $logData['bcc'] = $this->config['bcc']; } $mailer = $this->provideMailer(); $deliveryCount = $mailer->send($message, $failures); $success = $deliveryCount === $totalRecipients; // log it! $logData['message']['to'] = $message->getTo(); $logData['message']['cc'] = $message->getCc(); $logData['message']['bcc'] = $message->getBcc(); $logMessage = 'Sent email with subject "' . $message->getSubject() . '" to ' . $deliveryCount . ' / ' . $totalRecipients . ' recipients (To: ' . (is_string($to) ? $to : json_encode($to)) . ').'; if ($success) { $this->logger->info($logMessage, $logData); } else { // @codeCoverageIgnoreStart // can't really test failures atm $logData['failures'] = $failures; $this->logger->notice($logMessage, $logData); // @codeCoverageIgnoreEnd } return $success; }