View source
<?php
namespace Drupal\courier\Entity;
use Drupal\courier\ChannelBase;
use Drupal\courier\EmailInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\courier\Exception\ChannelFailure;
class Email extends ChannelBase implements EmailInterface {
public function getEmailAddress() {
return $this
->get('mail')->value;
}
public function setEmailAddress($mail) {
$this
->set('mail', [
'value' => $mail,
]);
return $this;
}
public function getRecipientName() {
return $this
->get('name')->value;
}
public function setRecipientName($name) {
$this
->set('name', [
'value' => $name,
]);
return $this;
}
public function getSubject() {
return $this
->get('subject')->value;
}
public function setSubject($subject) {
$this
->set('subject', [
'value' => $subject,
]);
return $this;
}
public function getBody() {
return $this
->get('body')->value;
}
public function setBody($body) {
$this
->get('body')->value = $body;
return $this;
}
public function getBodyFormat() {
return $this
->get('body')->format;
}
public function setBodyFormat($format) {
$this
->get('body')->format = $format;
return $this;
}
public function applyTokens() {
$tokens = $this
->getTokenValues();
$options = $this
->getTokenOptions();
$this
->setSubject(\Drupal::token()
->replace($this
->getSubject(), $tokens, $options));
$this
->setBody(\Drupal::token()
->replace($this
->getBody(), $tokens, $options));
return $this;
}
public function isEmpty() {
return empty($this
->getBody()) || empty($this
->getSubject());
}
public static function sendMessages(array $messages, array $options = []) {
foreach ($messages as $message) {
if (!($email = $message
->getEmailAddress())) {
throw new ChannelFailure('Missing email address for email.');
}
$name = $message
->getRecipientName();
$email_to = !empty($name) ? "{$name} <{$email}>" : $email;
$params = [
'context' => [
'subject' => $message
->getSubject(),
'message' => $message
->getBody(),
],
];
$mailman = \Drupal::service('plugin.manager.mail');
$mailman
->mail('system', 'courier_email', $email_to, $message
->language()
->getId(), $params, array_key_exists('reply_to', $options) ? $options['reply_to'] : NULL);
}
}
public function sendMessage(array $options = []) {
$this
->sendMessages([
$this,
], $options);
}
public function importTemplate($content) {
$this
->setSubject($content['subject']);
$this
->setBody($content['body']);
}
public function exportTemplate() {
return [
'subject' => $this
->getSubject(),
'body' => $this
->getBody(),
];
}
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields['id'] = BaseFieldDefinition::create('integer')
->setLabel(t('Email ID'))
->setDescription(t('The email ID.'))
->setReadOnly(TRUE)
->setSetting('unsigned', TRUE);
$fields['mail'] = BaseFieldDefinition::create('email')
->setLabel(t('Email'))
->setDescription(t('The email address to send this mail.'))
->setDefaultValue('')
->setDisplayOptions('form', [
'type' => 'hidden',
]);
$fields['name'] = BaseFieldDefinition::create('string')
->setLabel(t('Recipient name'))
->setDescription(t('Nickname for the recipient.'));
$fields['subject'] = BaseFieldDefinition::create('string')
->setLabel(t('Subject'))
->setDescription(t('The email subject.'))
->setDefaultValue('')
->setDisplayOptions('form', [
'type' => 'text_textfield',
'weight' => 0,
]);
$fields['body'] = BaseFieldDefinition::create('text_long')
->setLabel(t('Body'))
->setDescription(t('The main content of the email.'))
->setDefaultValue('')
->setDisplayOptions('form', [
'type' => 'text_textarea',
'weight' => 50,
]);
$fields['langcode'] = BaseFieldDefinition::create('language')
->setLabel(t('Language code'))
->setDescription(t('The email language code.'));
return $fields;
}
}