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

Contributed by
Kai Dederichs
in
#54975

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

Contributed by
Arnt Gulbrandsen
in
#58361

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.

Sponsor the Symfony project.

Symfony Blog

Read More

Latest News

PHP-Releases

PHP 8.4.1 released!

PHP 8.1.31 released!

PHP 8.3.14 released!

PHP 8.2.26 released!

Generated by Feedzy