/** * Send an email to lead(s) * * @param $email * @param $users * @param mixed $lead * @param array $tokens * @param array $assetAttachments * @param bool $saveStat * * @return mixed * @throws \Doctrine\ORM\ORMException */ public function sendEmailToUser($email, $users, $lead = null, $tokens = array(), $assetAttachments = array(), $saveStat = true) { if (!($emailId = $email->getId())) { return false; } if (!is_array($users)) { $user = array('id' => $users); $users = array($user); } //get email settings $emailSettings = $this->getEmailSettings($email, false); //noone to send to so bail if (empty($users)) { return false; } $mailer = $this->factory->getMailer(); $mailer->setLead($lead, true); $mailer->setTokens($tokens); $mailer->setEmail($email, false, $emailSettings[$emailId]['slots'], $assetAttachments, !$saveStat); $mailer->useMailerTokenization(); foreach ($users as $user) { $idHash = uniqid(); $mailer->setIdHash($idHash); if (!is_array($user)) { $id = $user; $user = array('id' => $id); } else { $id = $user['id']; } if (!isset($user['email'])) { /** @var \Mautic\UserBundle\Model\UserModel $model */ $userModel = $this->factory->getModel('user'); $userEntity = $userModel->getEntity($id); $user['email'] = $userEntity->getEmail(); $user['firstname'] = $userEntity->getFirstName(); $user['lastname'] = $userEntity->getLastName(); } $mailer->setTo($user['email'], $user['firstname'] . ' ' . $user['lastname']); $mailer->queue(true); if ($saveStat) { //create a stat $stat = new Stat(); $stat->setDateSent(new \DateTime()); $stat->setEmailAddress($user['email']); $stat->setTrackingHash($idHash); if (!empty($source)) { $stat->setSource($source[0]); $stat->setSourceId($source[1]); } $stat->setCopy($mailer->getBody()); $stat->setTokens($mailer->getTokens()); $saveEntities[] = $stat; } } //flush the message $mailer->flushQueue(); if (isset($saveEntities)) { $this->getStatRepository()->saveEntities($saveEntities); } //save some memory unset($mailer); }
/** * Create an email stat * * @param bool|true $persist * @param string|null $emailAddress * @param null $listId * * @return Stat|void * @throws \Doctrine\ORM\ORMException */ public function createEmailStat($persist = true, $emailAddress = null, $listId = null) { static $copies = array(); //create a stat $stat = new Stat(); $stat->setDateSent(new \DateTime()); $stat->setEmail($this->email); // Note if a lead if (null !== $this->lead) { $stat->setLead($this->factory->getEntityManager()->getReference('MauticLeadBundle:Lead', $this->lead['id'])); $emailAddress = $this->lead['email']; } // Find email if applicable if (null === $emailAddress) { // Use the last address set $emailAddresses = $this->message->getTo(); if (count($emailAddresses)) { end($emailAddresses); $emailAddress = key($emailAddresses); } } $stat->setEmailAddress($emailAddress); // Note if sent from a lead list if (null !== $listId) { $stat->setList($this->factory->getEntityManager()->getReference('MauticLeadBundle:LeadList', $listId)); } $stat->setTrackingHash($this->idHash); if (!empty($this->source)) { $stat->setSource($this->source[0]); $stat->setSourceId($this->source[1]); } $stat->setTokens($this->getTokens()); /** @var \Mautic\EmailBundle\Model\EmailModel $emailModel */ $emailModel = $this->factory->getModel('email'); // Save a copy of the email - use email ID if available simply to prevent from having to rehash over and over $id = null !== $this->email ? $this->email->getId() : md5($this->subject . $this->body['content']); if (!isset($copies[$id])) { $hash = strlen($id) !== 32 ? md5($this->subject . $this->body['content']) : $id; $copy = $emailModel->getCopyRepository()->findByHash($hash); if (null === $copy) { // Create a copy entry $copy = new Copy(); $copy->setId($hash)->setBody($this->body['content'])->setSubject($this->subject)->setDateCreated(new \DateTime())->setEmail($this->email); $emailModel->getCopyRepository()->saveEntity($copy); } $copies[$id] = $copy; } $stat->setStoredCopy($copies[$id]); if ($persist) { $emailModel->getStatRepository()->saveEntity($stat); } return $stat; }
/** * Create an email stat */ public function createLeadEmailStat() { if (!$this->lead) { return; } //create a stat $stat = new Stat(); $stat->setDateSent(new \DateTime()); $stat->setEmail($this->email); $stat->setLead($this->factory->getEntityManager()->getReference('MauticLeadBundle:Lead', $this->lead['id'])); $stat->setEmailAddress($this->lead['email']); $stat->setTrackingHash($this->idHash); if (!empty($this->source)) { $stat->setSource($this->source[0]); $stat->setSourceId($this->source[1]); } $stat->setCopy($this->getBody()); $stat->setTokens($this->getTokens()); /** @var \Mautic\EmailBundle\Model\EmailModel $emailModel */ $emailModel = $this->factory->getModel('email'); $emailModel->getStatRepository()->saveEntity($stat); }
/** * Test that processMailerCallback handles an array of hashIds correctly. */ public function testProcessMailerCallbackWithHashIds() { // Setup dependencies $ipLookupHelper = $this->getMockBuilder(IpLookupHelper::class)->disableOriginalConstructor()->getMock(); $themeHelper = $this->getMockBuilder(ThemeHelper::class)->disableOriginalConstructor()->getMock(); $mailboxHelper = $this->getMockBuilder(Mailbox::class)->disableOriginalConstructor()->getMock(); $mailHelper = $this->getMockBuilder(MailHelper::class)->disableOriginalConstructor()->getMock(); $leadModel = $this->getMockBuilder(LeadModel::class)->disableOriginalConstructor()->getMock(); $trackableModel = $this->getMockBuilder(TrackableModel::class)->disableOriginalConstructor()->getMock(); $userModel = $this->getMockBuilder(UserModel::class)->disableOriginalConstructor()->getMock(); $coreParametersHelper = $this->getMockBuilder(CoreParametersHelper::class)->disableOriginalConstructor()->getMock(); // Setup the translator $translator = $this->getMockBuilder(Translator::class)->disableOriginalConstructor()->getMock(); $translator->expects($this->any())->method('has')->will($this->returnValue(false)); // Setup the StatRepository $statRepository = $this->getMockBuilder(StatRepository::class)->disableOriginalConstructor()->getMock(); $statRepository->expects($this->once())->method('getTableAlias')->will($this->returnValue('s')); $leadEntity = $this->getMockBuilder(Lead::class)->disableOriginalConstructor()->getMock(); $leadEntity->expects($this->any())->method('getId')->will($this->returnValue(1)); $emailEntity = $this->getMockBuilder(Email::class)->disableOriginalConstructor()->getMock(); $emailEntity->expects($this->any())->method('getId')->will($this->returnValue(1)); $statEntity = new Stat(); $statEntity->setTrackingHash('xyz123'); $statEntity->setLead($leadEntity); $statEntity->setEmail($emailEntity); $statRepository->expects($this->any())->method('getEntities')->will($this->returnValue([$statEntity])); // Setup the EntityManager $entityManager = $this->getMockBuilder(EntityManager::class)->disableOriginalConstructor()->getMock(); $entityManager->expects($this->any())->method('getRepository')->will($this->returnValueMap([['MauticEmailBundle:Stat', $statRepository]])); $entityManager->expects($this->any())->method('getReference')->will($this->returnValue($leadEntity)); $messageModel = $this->getMockBuilder(MessageQueueModel::class)->disableOriginalConstructor()->getMock(); $companyModel = $this->getMockBuilder(CompanyModel::class)->disableOriginalConstructor()->getMock(); $companyRepository = $this->getMockBuilder(CompanyRepository::class)->disableOriginalConstructor()->getMock(); $companyRepository->method('getCompaniesForContacts')->will($this->returnValue([])); $companyModel->method('getRepository')->willReturn($companyRepository); $emailModel = new \Mautic\EmailBundle\Model\EmailModel($ipLookupHelper, $themeHelper, $mailboxHelper, $mailHelper, $leadModel, $companyModel, $trackableModel, $userModel, $coreParametersHelper, $messageModel); $emailModel->setTranslator($translator); $emailModel->setEntityManager($entityManager); $response = [2 => ['hashIds' => ['xyz123' => 'some reason', '123xyz' => 'some reason']]]; $dnc = $emailModel->processMailerCallback($response); $this->assertCount(1, $dnc); }
/** * {@inheritDoc} */ public function setTrackingHash($trackingHash) { $this->__initializer__ && $this->__initializer__->__invoke($this, 'setTrackingHash', array($trackingHash)); return parent::setTrackingHash($trackingHash); }