示例#1
0
 /**
  * Send the message
  *
  * @param bool $dispatchSendEvent
  * @param bool $isQueueFlush
  *
  * @return bool
  */
 public function send($dispatchSendEvent = false, $isQueueFlush = false)
 {
     // Set from email
     $from = $this->message->getFrom();
     if (empty($from)) {
         $this->setFrom($this->from);
     }
     // Set system return path if applicable
     $returnPath = $this->message->getReturnPath();
     if (empty($returnPath) && !empty($this->returnPath)) {
         $this->message->setReturnPath($this->returnPath);
     }
     if (empty($this->errors)) {
         // Search/replace tokens if this is not a queue flush
         if (!$isQueueFlush) {
             // Generate tokens from listeners
             if ($dispatchSendEvent) {
                 $this->dispatchSendEvent();
             }
             // Queue an asset stat if applicable
             $this->queueAssetDownloadEntry();
         }
         $this->message->setSubject($this->subject);
         $this->message->setBody($this->body['content'], $this->body['contentType'], $this->body['charset']);
         if (!empty($this->plainText)) {
             $this->message->addPart($this->plainText, 'text/plain');
         }
         if (!$isQueueFlush) {
             // Replace token content
             $tokens = $this->getTokens();
             if (!empty($tokens)) {
                 // Replace tokens
                 $search = array_keys($tokens);
                 $replace = $tokens;
                 self::searchReplaceTokens($search, $replace, $this->message);
             }
         }
         // Attach assets
         if (!empty($this->assets)) {
             /** @var \Mautic\AssetBundle\Entity\Asset $asset */
             foreach ($this->assets as $asset) {
                 $this->attachFile($asset->getFilePath(), $asset->getOriginalFileName(), $asset->getMime());
             }
         }
         try {
             $failures = array();
             $this->mailer->send($this->message, $failures);
             if (!empty($failures)) {
                 $this->errors['failures'] = $failures;
                 $this->logError('Sending failed for one or more recipients');
             }
             // Clear the log so that previous output is not associated with new errors
             $this->logger->clear();
         } catch (\Exception $e) {
             $this->logError($e);
             // Exception encountered when sending so all recipients are considered failures
             $this->errors['failures'] = array_merge(array_keys((array) $this->message->getTo()), array_keys((array) $this->message->getCc()), array_keys((array) $this->message->getBcc()));
         }
     }
     $error = empty($this->errors);
     $this->createAssetDownloadEntries();
     return $error;
 }