Пример #1
0
 /**
  * {@inheritdoc}
  */
 public function nextMail()
 {
     // Get the current mail spool row and update the internal pointer to the
     // next row.
     $return = each($this->mails);
     // If we're done, return false.
     if (!$return) {
         return FALSE;
     }
     $spool_data = $return['value'];
     // Store this spool row as processed.
     $this->processed[$spool_data->msid] = $spool_data;
     $entity = entity_load($spool_data->entity_type, $spool_data->entity_id);
     if (!$entity) {
         // If the entity load failed, set the processed status done and proceed with
         // the next mail.
         $this->processed[$spool_data->msid]->result = array('status' => SpoolStorageInterface::STATUS_DONE, 'error' => TRUE);
         return $this->nextMail();
     }
     if ($spool_data->data) {
         $subscriber = $spool_data->data;
     } else {
         $subscriber = simplenews_subscriber_load_by_mail($spool_data->mail);
     }
     if (!$subscriber) {
         // If loading the subscriber failed, set the processed status done and
         // proceed with the next mail.
         $this->processed[$spool_data->msid]->result = array('status' => SpoolStorageInterface::STATUS_DONE, 'error' => TRUE);
         return $this->nextMail();
     }
     $mail = new MailEntity($entity, $subscriber, \Drupal::service('simplenews.mail_cache'));
     // Set the langcode langcode.
     $this->processed[$spool_data->msid]->langcode = $mail->getEntity()->language()->getId();
     return $mail;
 }
Пример #2
0
 /**
  * {@inheritdoc}
  */
 public function sendTest(NodeInterface $node, array $test_addresses)
 {
     // Force the current user to anonymous to ensure consistent permissions.
     $this->accountSwitcher->switchTo(new AnonymousUserSession());
     // Send the test newsletter to the test address(es) specified in the node.
     // Build array of test email addresses.
     // Send newsletter to test addresses.
     // Emails are send direct, not using the spool.
     $recipients = array('anonymous' => array(), 'user' => array());
     foreach ($test_addresses as $mail) {
         $mail = trim($mail);
         if (!empty($mail)) {
             $subscriber = simplenews_subscriber_load_by_mail($mail);
             if (!$subscriber) {
                 // @todo: Find a cleaner way to do this.
                 $subscriber = Subscriber::create(array());
                 $subscriber->setUserId(0);
                 $subscriber->setMail($mail);
                 $subscriber->setLangcode(\Drupal::languageManager()->getCurrentLanguage());
             }
             if ($subscriber->getUserId()) {
                 $account = $subscriber->uid->entity;
                 $recipients['user'][] = $account->getUserName() . ' <' . $mail . '>';
             } else {
                 $recipients['anonymous'][] = $mail;
             }
             $mail = new MailEntity($node, $subscriber, \Drupal::service('simplenews.mail_cache'));
             $mail->setKey('test');
             $this->sendMail($mail);
         }
     }
     if (count($recipients['user'])) {
         $recipients_txt = implode(', ', $recipients['user']);
         drupal_set_message(t('Test newsletter sent to user %recipient.', array('%recipient' => $recipients_txt)));
     }
     if (count($recipients['anonymous'])) {
         $recipients_txt = implode(', ', $recipients['anonymous']);
         drupal_set_message(t('Test newsletter sent to anonymous %recipient.', array('%recipient' => $recipients_txt)));
     }
     $this->accountSwitcher->switchBack();
 }