public function SwiftMailer::format in Swift Mailer 8
Same name and namespace in other branches
- 8.2 src/Plugin/Mail/SwiftMailer.php \Drupal\swiftmailer\Plugin\Mail\SwiftMailer::format()
Formats a message composed by drupal_mail().
Parameters
array $message: A message array holding all relevant details for the message.
Return value
array The message as it should be sent.
Overrides MailInterface::format
See also
http://api.drupal.org/api/drupal/includes--mail.inc/interface/MailSystem...
File
- src/
Plugin/ Mail/ SwiftMailer.php, line 121
Class
- SwiftMailer
- Provides a 'Swift Mailer' plugin to send emails.
Namespace
Drupal\swiftmailer\Plugin\MailCode
public function format(array $message) {
$message = $this
->massageMessageBody($message);
// Get applicable format.
$applicable_format = $this
->getApplicableFormat($message);
// Theme message if format is set to be HTML.
if ($applicable_format == SWIFTMAILER_FORMAT_HTML) {
$render = [
'#theme' => isset($message['params']['theme']) ? $message['params']['theme'] : 'swiftmailer',
'#message' => $message,
];
$message['body'] = $this->renderer
->renderPlain($render);
if (empty($message['plain']) && $this->config['message']['convert_mode'] || !empty($message['params']['convert'])) {
$converter = new Html2Text($message['body']);
$message['plain'] = $converter
->getText();
}
}
// Process any images specified by 'image:' which are to be added later
// in the process. All we do here is to alter the message so that image
// paths are replaced with cid's. Each image gets added to the array
// which keeps track of which images to embed in the e-mail.
$embeddable_images = [];
$processed_images = [];
preg_match_all('/"image:([^"]+)"/', $message['body'], $embeddable_images);
for ($i = 0; $i < count($embeddable_images[0]); $i++) {
$image_id = $embeddable_images[0][$i];
if (isset($processed_images[$image_id])) {
continue;
}
$image_path = trim($embeddable_images[1][$i]);
$image_name = basename($image_path);
if (mb_substr($image_path, 0, 1) == '/') {
$image_path = mb_substr($image_path, 1);
}
$image = new \stdClass();
$image->uri = $image_path;
$image->filename = $image_name;
$image->filemime = \Drupal::service('file.mime_type.guesser')
->guess($image_path);
$image->cid = rand(0, 9999999999);
$message['params']['images'][] = $image;
$message['body'] = preg_replace($image_id, 'cid:' . $image->cid, $message['body']);
$processed_images[$image_id] = 1;
}
return $message;
}