New in Symfony 6.1: Draft Emails

Contributed by Kevin Bond in #44311.

In HTML it's possible to create email links that, when clicked, open a new email message with some pre-filled information rather than linking to some other page:

Send email

Send email

However, sometimes you need to create a full email, with lots of contents and even file attachments. You can't do that with the mailto: feature. That's why Symfony 6.1 will add a feature to create draft emails. Draft emails are email messages created as a file with the .eml extension and with the X-Unsent header set. This allows email clients to interpret them as draft emails that you can use to create the email to be sent. First, use the new DraftEmail class to create those emails:

    use Symfony\Component\Mime\DraftEmail;

$message = (new DraftEmail()) // ... ->html($this->renderView('...')) ->attach('...');

Now you can define a controller that downloads this draft email as a .eml file:

    // src/Controller/DownloadEmailController.php

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\ResponseHeaderBag; use Symfony\Component\Mime\DraftEmail; use Symfony\Component\Routing\Annotation\Route;

class BudgetController extends AbstractController {

[Route('/budget/estimate-generator/{clientId}')]

public function estimateGenerator(): Response
{
    // ...

    $message = (new DraftEmail())
        ->subject('Your estimate is now available')
        ->html($this->renderView('...'))
        ->attach('...');

    $response = new Response($message->toString());
    $contentDisposition = $response->headers->makeDisposition(
        ResponseHeaderBag::DISPOSITION_ATTACHMENT,
        'estimate.eml'
    );
    $response->headers->set('Content-Type', 'message/rfc822');
    $response->headers->set('Content-Disposition', $contentDisposition);

    return $response;
}

}

                Sponsor the Symfony project.

https://symfony.com/blog/new-in-symfony-6-1-draft-emails?utm_source=Symfony%20Blog%20Feed&utm_medium=feed

Created 3y | Apr 29, 2022, 8:20:20 AM


Login to add comment

Other posts in this group

Symfony 7.3.0-BETA2 released

Symfony 7.3.0-BETA2 has just been released. This is a pre-release version of Symfony 7.3. If you want to test it in your own applications before its final release, run the following commands:

May 10, 2025, 1:10:15 PM | Symfony
New in Symfony 7.3: Mailer Security Improvements

The Symfony Mailer component provides many security-related features like signing and encrypting email messages. In Symfony 7.3 we're pushing those features even further to give you greater control an

May 9, 2025, 9:20:10 AM | Symfony
SymfonyOnline June 2025: Detect Hidden Defects: Check Your PHP Tests

SymfonyOnline June 2025 is almost here, starting in almost 2 months on:

June 10-11: Workshop days. It is possible to attend 1 two-day training or 2 one-day trainings. June 12-13: Online confe

May 8, 2025, 2:40:19 PM | Symfony
New in Symfony 7.3: JsonPath Component

Contributed by Alexandre Daubois in

May 8, 2025, 10:10:10 AM | Symfony
SymfonyOnline June 2025: Combining Turbo, LiveComponent & Stimulus... the Right Way?

SymfonyOnline June 2025 is almost here, starting in almost 2 months on:

June 10-11: Workshop days. It is possible to attend 1 two-day training or 2 one-day trainings. June 12-13: Online confe

May 7, 2025, 1:10:15 PM | Symfony
New in Symfony 7.3: New and Improved Console Helpers

Symfony 7.3 introduces powerful improvements to the Console component, beyond the much-anticipated invokable commands and input attributes. This version also brings new helpers and features designed t

May 7, 2025, 8:30:04 AM | Symfony
New in Symfony 7.3: Static Error Pages

Contributed by Loick Piera in

May 6, 2025, 9:20:10 AM | Symfony