View source
<?php
namespace Drupal\commerce_email;
use Drupal\commerce\MailHandlerInterface;
use Drupal\commerce_email\Entity\EmailInterface;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Utility\Token;
class EmailSender implements EmailSenderInterface {
protected $mailHandler;
protected $token;
public function __construct(MailHandlerInterface $mail_handler, Token $token) {
$this->mailHandler = $mail_handler;
$this->token = $token;
}
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,
],
];
$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);
}
protected function replaceTokens($value, ContentEntityInterface $entity) {
if (!empty($value)) {
$value = $this->token
->replace($value, [
$entity
->getEntityTypeId() => $entity,
]);
}
return $value;
}
}