Newscoop\Services\EmailService::send PHP Method

send() public method

Send email
public send ( string $placeholder, string $message, string $to, string | null $from = null, string | null $attachmentDir = null ) : void
$placeholder string
$message string
$to string
$from string | null
$attachmentDir string | null
return void
    public function send($placeholder, $message, $to, $from = null, $attachmentDir = null)
    {
        $publicationService = $this->container->get('newscoop_newscoop.publication_service');
        $mailer = $this->container->get('mailer');
        if (empty($from)) {
            $from = 'no-reply@' . $publicationService->getPublicationAlias()->getName();
        }
        try {
            $messageToSend = \Swift_Message::newInstance();
            if (is_array($to)) {
                if (array_key_exists('moderator', $to)) {
                    $messageToSend->addBcc($to['moderator']);
                    unset($to['moderator']);
                }
            }
            $messageToSend->setSubject($placeholder)->setFrom($from)->setTo($to)->setBody($message, 'text/html');
            if ($attachmentDir) {
                $messageToSend->attach(\Swift_Attachment::fromPath($attachmentDir));
            }
            $mailer->send($messageToSend);
        } catch (\Exception $exception) {
            throw $exception;
        }
    }

Usage Example

 /**
  * Sends email notifications
  *
  * @param string                                $code             Email message type
  * @param Newscoop\Entity\User                  $user             User object
  * @param PaywallBundle\Entity\UserSubscription $userSubscription User's subscription
  */
 public function sendNotification($code, array $recipients = array(), array $data = array())
 {
     if ($this->systemPreferences->PaywallEmailNotifyEnabled != '1') {
         return;
     }
     $now = new \DateTime('now');
     $smarty = $this->templatesService->getSmarty();
     $smarty->assign('user', new \MetaUser($data['user']));
     $smarty->assign('userSubscription', new MetaSubscription($data['subscription']));
     try {
         $message = $this->loadProperMessageTemplateBy($code);
     } catch (\Exception $e) {
         throw new \Exception($e->getMessage());
     }
     $recipients = !empty($recipients) ? $recipients : $this->systemPreferences->PaywallMembershipNotifyEmail;
     $this->emailService->send($this->placeholdersService->get('subject'), $message, $recipients, $this->systemPreferences->PaywallMembershipNotifyFromEmail);
 }