/** * 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)) { if (!$isQueueFlush && ($bounceEmail = $this->generateBounceEmail($this->idHash))) { $this->message->setReturnPath($bounceEmail); } elseif (!empty($this->returnPath)) { $this->message->setReturnPath($this->returnPath); } } if (empty($this->errors)) { if (!$isQueueFlush) { // Only add unsubscribe header to one-off sends as tokenized sends are built by the transport $this->addUnsubscribeHeader(); // Search/replace tokens if this is not a queue flush // 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) { if (!in_array($asset->getId(), $this->attachedAssets)) { $this->attachedAssets[] = $asset->getId(); $this->attachFile($asset->getFilePath(), $asset->getOriginalFileName(), $asset->getMime()); } } } // Set custom headers if (!empty($this->headers)) { $headers = $this->message->getHeaders(); foreach ($this->headers as $headerKey => $headerValue) { if ($headers->has($headerKey)) { $header = $headers->get($headerKey); $header->setFieldBodyModel($headerValue); } else { $headers->addTextHeader($headerKey, $headerValue); } } } 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; }