public function SwiftMailer::format in Swift Mailer 8.2
Same name and namespace in other branches
- 8 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
File
- src/
Plugin/ Mail/ SwiftMailer.php, line 157
Class
- SwiftMailer
- Provides a 'Swift Mailer' plugin to send emails.
Namespace
Drupal\swiftmailer\Plugin\MailCode
public function format(array $message) {
// Get content type.
$is_html = $this
->getContentType($message) == SWIFTMAILER_FORMAT_HTML;
// Determine if a plain text alternative is required. The message parameter
// takes priority over config. Support the alternate parameter 'convert'
// for back-compatibility.
$generate_plain = $message['params']['generate_plain'] ?? $message['params']['convert'] ?? $this->config['message']['generate_plain'];
if ($generate_plain && empty($message['plain']) && $is_html) {
// Generate plain text alternative. This must be done first with the
// original message body, before overwriting it with the HTML version.
$saved_body = $message['body'];
$this
->massageMessageBody($message, FALSE);
$message['plain'] = $message['body'];
$message['body'] = $saved_body;
}
$this
->massageMessageBody($message, $is_html);
// We replace all 'image:foo' in the body with a unique magic string like
// 'cid:[randomname]' and keep track of this. It will be replaced by the
// final "cid" in ::embed().
$random = new Random();
$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 = $random
->name(8, TRUE);
$message['params']['images'][] = $image;
$message['body'] = preg_replace($image_id, 'cid:' . $image->cid, $message['body']);
$processed_images[$image_id] = 1;
}
return $message;
}