comparison core/lib/Drupal/Core/Mail/MailManager.php @ 17:129ea1e6d783

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:21:36 +0000
parents 7a779792577d
children
comparison
equal deleted inserted replaced
16:c2387f117808 17:129ea1e6d783
1 <?php 1 <?php
2 2
3 namespace Drupal\Core\Mail; 3 namespace Drupal\Core\Mail;
4 4
5 use Drupal\Component\Render\MarkupInterface;
5 use Drupal\Component\Render\PlainTextOutput; 6 use Drupal\Component\Render\PlainTextOutput;
6 use Drupal\Component\Utility\Unicode; 7 use Drupal\Component\Utility\Html;
8 use Drupal\Component\Utility\Mail as MailHelper;
7 use Drupal\Core\Logger\LoggerChannelFactoryInterface; 9 use Drupal\Core\Logger\LoggerChannelFactoryInterface;
10 use Drupal\Core\Messenger\MessengerTrait;
8 use Drupal\Core\Plugin\DefaultPluginManager; 11 use Drupal\Core\Plugin\DefaultPluginManager;
9 use Drupal\Core\Cache\CacheBackendInterface; 12 use Drupal\Core\Cache\CacheBackendInterface;
10 use Drupal\Core\Extension\ModuleHandlerInterface; 13 use Drupal\Core\Extension\ModuleHandlerInterface;
11 use Drupal\Core\Config\ConfigFactoryInterface; 14 use Drupal\Core\Config\ConfigFactoryInterface;
15 use Drupal\Core\Render\Markup;
12 use Drupal\Core\Render\RenderContext; 16 use Drupal\Core\Render\RenderContext;
13 use Drupal\Core\Render\RendererInterface; 17 use Drupal\Core\Render\RendererInterface;
14 use Drupal\Core\StringTranslation\StringTranslationTrait; 18 use Drupal\Core\StringTranslation\StringTranslationTrait;
15 use Drupal\Core\StringTranslation\TranslationInterface; 19 use Drupal\Core\StringTranslation\TranslationInterface;
16 20
21 * @see \Drupal\Core\Mail\MailInterface 25 * @see \Drupal\Core\Mail\MailInterface
22 * @see plugin_api 26 * @see plugin_api
23 */ 27 */
24 class MailManager extends DefaultPluginManager implements MailManagerInterface { 28 class MailManager extends DefaultPluginManager implements MailManagerInterface {
25 29
30 use MessengerTrait;
26 use StringTranslationTrait; 31 use StringTranslationTrait;
27 32
28 /** 33 /**
29 * The config factory. 34 * The config factory.
30 * 35 *
247 ]; 252 ];
248 // To prevent email from looking like spam, the addresses in the Sender and 253 // To prevent email from looking like spam, the addresses in the Sender and
249 // Return-Path headers should have a domain authorized to use the 254 // Return-Path headers should have a domain authorized to use the
250 // originating SMTP server. 255 // originating SMTP server.
251 $headers['Sender'] = $headers['Return-Path'] = $site_mail; 256 $headers['Sender'] = $headers['Return-Path'] = $site_mail;
252 // Headers are usually encoded in the mail plugin that implements 257 // Make sure the site-name is a RFC-2822 compliant 'display-name'.
253 // \Drupal\Core\Mail\MailInterface::mail(), for example, 258 $headers['From'] = MailHelper::formatDisplayName($site_config->get('name')) . ' <' . $site_mail . '>';
254 // \Drupal\Core\Mail\Plugin\Mail\PhpMail::mail(). The site name must be
255 // encoded here to prevent mail plugins from encoding the email address,
256 // which would break the header.
257 $headers['From'] = Unicode::mimeHeaderEncode($site_config->get('name'), TRUE) . ' <' . $site_mail . '>';
258 if ($reply) { 259 if ($reply) {
259 $headers['Reply-to'] = $reply; 260 $headers['Reply-to'] = $reply;
260 } 261 }
261 $message['headers'] = $headers; 262 $message['headers'] = $headers;
262 263
272 // email. 273 // email.
273 $this->moduleHandler->alter('mail', $message); 274 $this->moduleHandler->alter('mail', $message);
274 275
275 // Retrieve the responsible implementation for this message. 276 // Retrieve the responsible implementation for this message.
276 $system = $this->getInstance(['module' => $module, 'key' => $key]); 277 $system = $this->getInstance(['module' => $module, 'key' => $key]);
278
279 // Attempt to convert relative URLs to absolute.
280 foreach ($message['body'] as &$body_part) {
281 if ($body_part instanceof MarkupInterface) {
282 $body_part = Markup::create(Html::transformRootRelativeUrlsToAbsolute((string) $body_part, \Drupal::request()->getSchemeAndHttpHost()));
283 }
284 }
277 285
278 // Format the message body. 286 // Format the message body.
279 $message = $system->format($message); 287 $message = $system->format($message);
280 288
281 // Optionally send email. 289 // Optionally send email.
301 ->error('Error sending email (from %from to %to with reply-to %reply).', [ 309 ->error('Error sending email (from %from to %to with reply-to %reply).', [
302 '%from' => $message['from'], 310 '%from' => $message['from'],
303 '%to' => $message['to'], 311 '%to' => $message['to'],
304 '%reply' => $message['reply-to'] ? $message['reply-to'] : $this->t('not set'), 312 '%reply' => $message['reply-to'] ? $message['reply-to'] : $this->t('not set'),
305 ]); 313 ]);
306 drupal_set_message($this->t('Unable to send email. Contact the site administrator if the problem persists.'), 'error'); 314 $this->messenger()->addError($this->t('Unable to send email. Contact the site administrator if the problem persists.'));
307 } 315 }
308 } 316 }
309 } 317 }
310 318
311 return $message; 319 return $message;