You are here

public function EmailSender::send in Commerce Email 8

Sends the given email.

Parameters

\Drupal\commerce_email\Entity\EmailInterface $email: The email.

\Drupal\Core\Entity\ContentEntityInterface $entity: The entity.

Return value

bool TRUE if the email was sent successfully, FALSE otherwise.

Throws

\InvalidArgumentException Thrown when given an entity that doesn't match the email event.

Overrides EmailSenderInterface::send

File

src/EmailSender.php, line 42

Class

EmailSender

Namespace

Drupal\commerce_email

Code

public function send(EmailInterface $email, ContentEntityInterface $entity) {
  $entity_type_id = $entity
    ->getEntityTypeId();
  $event_entity_type_id = $email
    ->getEvent()
    ->getEntityTypeId();
  if ($entity_type_id != $event_entity_type_id) {
    throw new \InvalidArgumentException(sprintf('The email requires a "%s" entity, but a "%s" entity was given.', $event_entity_type_id, $entity_type_id));
  }
  $short_entity_type_id = str_replace('commerce_', '', $entity_type_id);
  $to = $this
    ->replaceTokens($email
    ->getTo(), $entity);
  $subject = $this
    ->replaceTokens($email
    ->getSubject(), $entity);
  $body = [
    '#type' => 'inline_template',
    '#template' => $this
      ->replaceTokens($email
      ->getBody(), $entity),
    '#context' => [
      $short_entity_type_id => $entity,
    ],
  ];

  // @todo Figure out how to get the langcode generically.
  $params = [
    'id' => 'commerce_email',
    'from' => $this
      ->replaceTokens($email
      ->getFrom(), $entity),
    'cc' => $this
      ->replaceTokens($email
      ->getCc(), $entity),
    'bcc' => $this
      ->replaceTokens($email
      ->getBcc(), $entity),
  ];
  return $this->mailHandler
    ->sendMail($to, $subject, $body, $params);
}