New in Symfony 7.2: Mime Improvements
The Mime component provides tools to create and manipulate MIME messages.
In Symfony 7.2, we introduced new features to improve it.
Custom MIME Encoders
The MIME standard was created to extend the original format of email messages to
support text in character sets other than ASCII, as well as attachments of any type.
In Symfony’s Mime component, this is possible in part thanks to the encoders used
by TextPart elements. Previously, the encoders were a hardcoded list (quoted-printable,
base64, 8bit).
In Symfony 7.2, TextPart items allow you to define custom encoders for your specific
needs. For example, if your app uses SOAP Attachments, you’ll need to create
a custom encoder like this:
use SymfonyComponentMimeEncoderContentEncoderInterface;
class MyEncoder implements ContentEncoderInterface
{
public function encodeByteStream($stream, int $maxLineLength = 0): iterable
{
// …
}
public function encodeString(string $string, ?string $charset = ‚utf-8‘, int $firstLineOffset = 0, int $maxLineLength = 0): string
{
// …
}
public function getName(): string
{
return ‚my_encoder‘;
}
}
Now, you can use this encoder in your TextPart items when creating the MIME message:
// …
TextPart::addEncoder(‚my_encoder‘, new MyEncoder());
$content = new TextPart(‚…‘, ‚utf-8‘, ‚plain‘, ‚my_encoder‘);
Unicode Email Addresses Support
Traditionally, email addresses only allowed ASCII characters in both the local
part and the domain part. This was inconvenient for a large part of the world’s
population, so RFC 6531 introduced an extension to the SMTP protocol to support
internationalized email addresses.
In Symfony 7.2, we’re adding support for Unicode characters in both the local and
domain parts of email addresses:
use SymfonyComponentMimeEmail;
$email = (new Email())
->from(‚jânë.dœ@ëxãmplę.com‘)
// …
You don’t need to change anything in your application to use this feature.
If the remote SMTP server doesn’t support Unicode email addresses, you’ll
see an InvalidArgumentException with an error message explaining the issue.
Symfony Blog