You are here

public function AnonymousPublishingClService::sendVerificationMail in Anonymous Publishing 8

Send verification email.

Parameters

object $entity: The node object.

string $akey: Activation key.

File

modules/anonymous_publishing_cl/src/Services/AnonymousPublishingClService.php, line 115

Class

AnonymousPublishingClService
Helper methods for Anonymous Publishing CL.

Namespace

Drupal\anonymous_publishing_cl\Services

Code

public function sendVerificationMail(EntityInterface $entity, $akey) {
  $settings = $this->configFactory
    ->get('anonymous_publishing_cl.settings');
  $emailSettings = $this->configFactory
    ->get('anonymous_publishing_cl.mail');
  $options = $settings
    ->get('general_options');

  // Get the entity title and link.
  if ($entity
    ->getEntityTypeId() == 'node') {
    $title = $entity
      ->getTitle();
    $vfurl = Url::fromRoute('anonymous_publishing_cl.verify', array(), array(
      'query' => array(
        'akey' => $akey,
      ),
      'absolute' => TRUE,
    ));
    $modp = !$options['sactivate'];
  }
  else {
    if ($entity
      ->getEntityTypeId() == 'comment') {
      $title = $entity
        ->getSubject();
      if (empty($title)) {
        $title = '';
      }
      $vfurl = Url::fromRoute('anonymous_publishing_cl.verify', array(), array(
        'query' => array(
          'akey' => $akey,
        ),
        'absolute' => TRUE,
      ));
      $modp = !$options['sactivate'];
    }
  }

  // Build emails remplacement tokens values.
  $autodelhours = $settings
    ->get('autodelhours');
  $variables = array(
    '@action' => $modp ? t('verify') : t('activate'),
    '@autodelhours' => $settings
      ->get('autodelhours'),
    '@email' => $entity->anonymous_publishing_email,
    '@site' => $this->configFactory
      ->get('system.site')
      ->get('name'),
    '@title' => Html::escape($title),
    '@verification_uri' => $vfurl
      ->toString(),
  );

  // Build verification mail.
  $to = $entity->anonymous_publishing_email;
  $subject = $modp ? $this
    ->t($emailSettings
    ->get('email_subject_verify'), $variables) : $this
    ->t($emailSettings
    ->get('email_subject_active'), $variables);
  $b1 = $this
    ->t($emailSettings
    ->get('email_introduction'), $variables);
  $b2 = $autodelhours >= 0 ? $this
    ->t($emailSettings
    ->get('email_activate'), $variables) : '';
  $b3 = $modp ? $this
    ->t($emailSettings
    ->get('email_verify'), $variables) : '';
  $body = array(
    $b1,
    $b2,
    $b3,
  );
  $params = array(
    'subject' => $subject,
    'body' => $body,
  );

  // Send verification mail
  $result = $this->mailManager
    ->mail('anonymous_publishing_cl', 'verify', $to, \Drupal::currentUser()
    ->getPreferredLangcode(), $params);
  if ($result['result'] == TRUE) {
    \Drupal::messenger()
      ->addMessage(t('A link and further instructions have been sent to your email address.'));
    $this->logger
      ->notice('Verification mail sent to @to', array(
      '@to' => $to,
    ));
  }
  else {
    $this->logger
      ->error('Error mailing activation/verification link.');
    \Drupal::messenger()
      ->addError(t('Unable to send mail. Please contact the site admin.'));
  }

  // Send admin notification mail.
  if ($options['modmail']) {
    $subject = $this
      ->t($emailSettings
      ->get('email_admin_subject'), $variables);
    $body = array(
      $this
        ->t($emailSettings
        ->get('email_admin_body'), $variables),
    );
    $to = $settings
      ->get('notification_email_destination');
    $params = array(
      'subject' => $subject,
      'body' => $body,
    );
    $result = $this->mailManager
      ->mail('anonymous_publishing_cl', 'verify', $to, \Drupal::currentUser()
      ->getPreferredLangcode(), $params);
    if ($result['result'] == FALSE) {
      $this->logger
        ->error('Error notifying admin.');
    }
  }
}