public function send(Notifications $notification) { if ($notification->sent == 'Y') { return; } $post = $notification->post; $user = $notification->user; if ($notification->type != 'P') { $reply = $notification->reply; } else { $reply = true; } $from = $this->config->mail->fromEmail; $url = $this->config->site->url; if ($post && $user && $reply) { if ($user->email && $user->notifications != 'N' && strpos($user->email, '@users.noreply.github.com') === false) { try { $message = new \Swift_Message('[Phalcon Forum] ' . $post->title); $message->setTo(array($user->email => $user->name)); $message->addReplyTo('reply-i' . $post->id . '-' . time() . '@phosphorum.com'); if ($notification->type == 'P') { $originalContent = $post->content; $htmlContent = $this->markdown->render($post->content); $message->setFrom(array($from => $post->user->name)); } else { $reply = $notification->reply; $originalContent = $reply->content; $htmlContent = $this->markdown->render($reply->content); $message->setFrom(array($from => $reply->user->name)); } if (trim($originalContent)) { $textContent = nl2br($originalContent); $htmlContent .= '<p style="font-size:small;-webkit-text-size-adjust:none;color:#717171;">'; if ($notification->type == 'P') { $htmlContent .= '—<br>Reply to this email directly or view the complete thread on ' . PHP_EOL . '<a href="' . $url . '/discussion/' . $post->id . '/' . $post->slug . '">Phosphorum</a>. '; } else { $htmlContent .= '—<br>Reply to this email directly or view the complete thread on ' . PHP_EOL . '<a href="' . $url . '/discussion/' . $post->id . '/' . $post->slug . '#C' . $reply->id . '">Phosphorum</a>. '; } $htmlContent .= PHP_EOL . 'Change your e-mail preferences <a href="' . $url . '/settings">here</a></p>'; $bodyMessage = new \Swift_MimePart($htmlContent, 'text/html'); $bodyMessage->setCharset('UTF-8'); $message->attach($bodyMessage); $bodyMessage = new \Swift_MimePart($textContent, 'text/plain'); $bodyMessage->setCharset('UTF-8'); $message->attach($bodyMessage); if (!$this->transport) { $this->transport = \Swift_SmtpTransport::newInstance($this->config->smtp->host, $this->config->smtp->port, $this->config->smtp->security); $this->transport->setUsername($this->config->smtp->username); $this->transport->setPassword($this->config->smtp->password); } if (!$this->mailer) { $this->mailer = \Swift_Mailer::newInstance($this->transport); } $this->mailer->send($message); } } catch (\Exception $e) { echo $e->getMessage(), PHP_EOL; } } } $notification->sent = 'Y'; if ($notification->save() == false) { foreach ($notification->getMessages() as $message) { echo $message->getMessage(), PHP_EOL; } } }
/** * @param \Swift_Message $instance * @param array $from From addresses. An array of (email-address => name) * @param array $to To addresses. An array of (email-address => name) * @param array $cc Cc addresses. An array of (email-address => name) [optional] * @param array $bcc Bcc addresses. An array of (email-address => name) [optional] * @param array $replyTo Reply to addresses. An array of (email-address => name) [optional] */ protected function setupMessageHeaders($instance, $from, $to, $cc = [], $bcc = [], $replyTo = []) { // Add from addresses foreach ($from as $address => $name) { $instance->addFrom($address, $name); } // Add to addresses foreach ($to as $address => $name) { $instance->addTo($address, $name); } // Add cc addresses foreach ($cc as $address => $name) { $instance->addCc($address, $name); } // Add bcc addresses foreach ($bcc as $address => $name) { $instance->addBcc($address, $name); } // Add reply to addresses foreach ($replyTo as $address => $name) { $instance->addReplyTo($address, $name); } }
/** * {@inheritdoc} * * @return $this|self */ public function addReplyTo($address, $name = null) : self { $this->message->addReplyTo($address, $name); return $this; }
/** * @return \Swift_Message */ public static function getValid() { $message = new \Swift_Message(); $message->setFrom(MailWrapperTestBootstrap::$from); $message->addTo(MailWrapperTestBootstrap::$toAddresses[0]); $message->addTo(MailWrapperTestBootstrap::$toAddresses[1]); $message->addCC(MailWrapperTestBootstrap::$ccAddresses[0]); $message->addCC(MailWrapperTestBootstrap::$ccAddresses[1]); $message->addBCC(MailWrapperTestBootstrap::$bccAddresses[0]); $message->addBCC(MailWrapperTestBootstrap::$bccAddresses[1]); $message->addReplyTo(MailWrapperTestBootstrap::$alternate); $message->setSubject(MailWrapperTestBootstrap::$subject); $message->setBody(MailWrapperTestBootstrap::$contentText); $message->addPart(MailWrapperTestBootstrap::$contentHtml, 'text/html'); return $message; }