Skip to content

Commit 57d2cca

Browse files
committed
[!!!][TASK] Remove MailMessage->send()
Resolves: TYPO3-Documentation/Changelog-To-Doc#1452 Releases: main
1 parent cf47ec1 commit 57d2cca

File tree

3 files changed

+86
-112
lines changed

3 files changed

+86
-112
lines changed

Documentation/ApiOverview/Mail/Index.rst

Lines changed: 19 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -438,82 +438,21 @@ like the following:
438438
.. index:: Mail; MailMessage
439439
.. _mail-mail-message:
440440

441-
Send email with `MailMessage`
442-
-----------------------------
441+
Send email of type `MailMessage` with an injected `MailerInterface`
442+
-------------------------------------------------------------------
443443

444-
:php:`MailMessage` can be used to generate and send an email without using
445-
Fluid:
444+
.. versionchanged:: 14.0
445+
The following methods have been removed:
446446

447-
.. code-block:: php
448-
:caption: EXT:site_package/Classes/Utility/MyMailUtility.php
449-
450-
use Symfony\Component\Mime\Address;
451-
use TYPO3\CMS\Core\Mail\MailMessage;
452-
use TYPO3\CMS\Core\Utility\GeneralUtility;
453-
454-
// Create the message
455-
$mail = GeneralUtility::makeInstance(MailMessage::class);
456-
457-
// Prepare and send the message
458-
$mail
459-
// Defining the "From" email address and name as an object
460-
// (email clients will display the name)
461-
->from(new Address('[email protected]', 'John Doe'))
462-
463-
// Set the "To" addresses
464-
->to(
465-
new Address('[email protected]', 'Max Mustermann'),
466-
new Address('[email protected]')
467-
)
468-
469-
// Give the message a subject
470-
->subject('Your subject')
471-
472-
// Give it the text message
473-
->text('Here is the message itself')
447+
* :php:`TYPO3\CMS\Core\Mail\MailMessage->send()`
448+
* :php:`TYPO3\CMS\Core\Mail\MailMessage->isSent()`
474449

475-
// And optionally an HTML message
476-
->html('<p>Here is the message itself</p>')
477-
478-
// Optionally add any attachments
479-
->attachFromPath('/path/to/my-document.pdf')
480-
481-
// And finally send it
482-
->send()
483-
;
484-
485-
486-
487-
Or, if you prefer, do not concatenate the calls:
488-
489-
.. code-block:: php
490-
:caption: EXT:site_package/Classes/Utility/MyMailUtility.php
491-
492-
use Symfony\Component\Mime\Address;
493-
use TYPO3\CMS\Core\Utility\GeneralUtility;
494-
use TYPO3\CMS\Core\Mail\MailMessage;
495-
496-
$mail = GeneralUtility::makeInstance(MailMessage::class);
497-
$mail->from(new Address('[email protected]', 'John Doe'));
498-
$mail->to(
499-
new Address('[email protected]', 'Max Mustermann'),
500-
new Address('[email protected]')
501-
);
502-
$mail->subject('Your subject');
503-
$mail->text('Here is the message itself');
504-
$mail->html('<p>Here is the message itself</p>');
505-
$mail->attachFromPath('/path/to/my-document.pdf');
506-
$mail->send();
507-
508-
509-
.. note::
510-
Before TYPO3 v10 the :php:`MailMessage` class only had methods like
511-
:php:`->setTo()`, :php:`setFrom()`, :php:`->setSubject()` etc.
512-
Now the class inherits from :php:`\Symfony\Component\Mime\Email` which
513-
provides the methods from the example. To make migration from older TYPO3
514-
versions easier the previous methods still exist. The use of
515-
:php:`MailMessage` in own extensions is recommended.
450+
:php:`\TYPO3\CMS\Core\Mail\MailMessage` can be used to generate an email
451+
without using Fluid, the email can then be send via an injected interface of
452+
:php:`\TYPO3\CMS\Core\Mail\MailerInterface`.
516453

454+
.. literalinclude:: _codesnippets/_MyMailerController.php
455+
:caption: EXT:site_package/Classes/Controller/MyMailerController.php
517456

518457
.. index:: Mail; Attachments
519458
.. _mail-attachments:
@@ -527,14 +466,13 @@ Attach files that exist in your file system:
527466
:caption: EXT:site_package/Classes/Utility/MyMailUtility.php
528467
529468
// Attach file to message
530-
$mail->attachFromPath('/path/to/documents/privacy.pdf');
469+
$email->attachFromPath('/path/to/documents/privacy.pdf');
531470
532471
// Optionally you can tell email clients to display a custom name for the file
533-
$mail->attachFromPath('/path/to/documents/privacy.pdf', 'Privacy Policy');
472+
$email->attachFromPath('/path/to/documents/privacy.pdf', 'Privacy Policy');
534473
535474
// Alternatively attach contents from a stream
536-
$mail->attach(fopen('/path/to/documents/contract.doc', 'r'));
537-
475+
$email->attach(fopen('/path/to/documents/contract.doc', 'r'));
538476
539477
540478
.. index:: Mail; Inline media
@@ -549,13 +487,13 @@ Add some inline media like images in an email:
549487
:caption: EXT:site_package/Classes/Utility/MyMailUtility.php
550488
551489
// Get the image contents from a PHP resource
552-
$mail->embed(fopen('/path/to/images/logo.png', 'r'), 'logo');
490+
$email->embed(fopen('/path/to/images/logo.png', 'r'), 'logo');
553491
554492
// Get the image contents from an existing file
555-
$mail->embedFromPath('/path/to/images/signature.png', 'footer-signature');
493+
$email->embedFromPath('/path/to/images/signature.png', 'footer-signature');
556494
557495
// reference images using the syntax 'cid:' + "image embed name"
558-
$mail->html('<img src="cid:logo"> ... <img src="cid:footer-signature"> ...');
496+
$email->html('<img src="cid:logo"> ... <img src="cid:footer-signature"> ...');
559497
560498
561499
.. index::
@@ -577,39 +515,8 @@ It is possible to define a default email sender ("From:") in
577515
578516
This is how you can use these defaults:
579517

580-
.. code-block:: php
581-
:caption: EXT:site_package/Classes/Utility/MyMailUtility.php
582-
583-
use TYPO3\CMS\Core\Mail\MailMessage;
584-
use TYPO3\CMS\Core\Utility\GeneralUtility;
585-
use TYPO3\CMS\Core\Utility\MailUtility;
586-
587-
$from = MailUtility::getSystemFrom();
588-
$email = new MailMessage();
589-
590-
// As getSystemFrom() returns an array we need to use the setFrom method
591-
$email->setFrom($from);
592-
// ...
593-
$email->send();
594-
595-
In case of the problem "Mails are not sent" in your extension, try to set a
596-
``ReturnPath:``. Start as before but add:
597-
598-
.. code-block:: php
599-
:caption: EXT:site_package/Classes/Utility/MyMailUtility.php
600-
601-
use TYPO3\CMS\Core\Utility\MailUtility;
602-
603-
// You will get a valid email address from 'defaultMailFromAddress' or if
604-
// not set from PHP settings or from system.
605-
// If result is not a valid email address, the final result will be
606-
607-
$returnPath = MailUtility::getSystemFromAddress();
608-
if ($returnPath != "[email protected]") {
609-
$mail->setReturnPath($returnPath);
610-
}
611-
$mail->send();
612-
518+
.. literalinclude:: _codesnippets/_MyMailerControllerDefault.php
519+
:caption: EXT:site_package/Classes/Controller/MyMailerController.php
613520

614521
.. index::
615522
Mail; Custom mailer
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
use Symfony\Component\Mime\Address;
4+
use TYPO3\CMS\Core\Mail\MailerInterface;
5+
use TYPO3\CMS\Core\Mail\MailMessage;
6+
7+
final readonly class MyMailerController
8+
{
9+
public function __construct(
10+
private MailerInterface $mailer,
11+
) {}
12+
13+
/**
14+
* @throws \Symfony\Component\Mailer\Exception\TransportExceptionInterface
15+
*/
16+
public function sendMail()
17+
{
18+
$email = new MailMessage();
19+
// Prepare and send the message
20+
$email
21+
// Defining the "From" email address and name as an object
22+
// (email clients will display the name)
23+
->from(new Address('[email protected]', 'John Doe'))
24+
25+
// Set the "To" addresses
26+
->to(
27+
new Address('[email protected]', 'Max Mustermann'),
28+
new Address('[email protected]'),
29+
)
30+
31+
// Give the message a subject
32+
->subject('Your subject')
33+
34+
// Give it the text message
35+
->text('Here is the message itself')
36+
37+
// And optionally an HTML message
38+
->html('<p>Here is the message itself</p>')
39+
40+
// Optionally add any attachments
41+
->attachFromPath('/path/to/my-document.pdf');
42+
// And finally send it
43+
$this->mailer->send($email);
44+
}
45+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
use TYPO3\CMS\Core\Mail\MailerInterface;
4+
use TYPO3\CMS\Core\Mail\MailMessage;
5+
use TYPO3\CMS\Core\Utility\MailUtility;
6+
7+
final readonly class MyMailerController
8+
{
9+
public function __construct(
10+
private MailerInterface $mailer,
11+
) {}
12+
13+
/**
14+
* @throws \Symfony\Component\Mailer\Exception\TransportExceptionInterface
15+
*/
16+
public function sendMail(MailMessage $email)
17+
{
18+
// As getSystemFrom() returns an array we need to use the setFrom method
19+
$email->setFrom(MailUtility::getSystemFrom());
20+
$this->mailer->send($email);
21+
}
22+
}

0 commit comments

Comments
 (0)