Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Mime] add DraftEmail #44311

Merged
merged 0 commits into from Dec 22, 2021
Merged

[Mime] add DraftEmail #44311

merged 0 commits into from Dec 22, 2021

Conversation

@kbond
Copy link
Member

@kbond kbond commented Nov 27, 2021

Q A
Branch? 6.1
Bug fix? no
New feature? yes
Deprecations? no
Tickets n/a
License MIT
Doc PR todo

I've had the need to create "draft" emails pre-filled with content/attachments. User's can then download these as a .eml, open with their email client, manipulate, then send. Thought I'd share my solution to see if this is something that would be acceptable in core.

Only needed a few minor adjustments to Email that I wrapped up into an DraftEmail object:

  1. Add X-Unsent: 1 header (this marks the email as "draft" for clients that support this)
  2. Allow no From/To (user can add with their client)
  3. Remove Message-ID/Date headers (these will be added by the client)

Usage:

$content = (new DraftEmail()
    ->html($twig->render(...))
    ->attach(...)
    ->toString()
;

$response = new Response($message->toString());
$contentDisposition = $response->headers->makeDisposition(
    ResponseHeaderBag::DISPOSITION_ATTACHMENT,
    'download.eml'
);
$response->headers->set('Content-Type', 'message/rfc822');
$response->headers->set('Content-Disposition', $contentDisposition);
src/Symfony/Component/Mime/Email.php Outdated Show resolved Hide resolved
@kbond kbond force-pushed the unsent-email branch 2 times, most recently from dfff60d to 9da47c6 Nov 27, 2021
@kbond kbond changed the title [Mime] add UnsentEmail [Mime] add DraftEmail Nov 27, 2021
@kbond
Copy link
Member Author

@kbond kbond commented Nov 27, 2021

Some details on X-Unsent header client support:

  • Virtually all versions of Outlook (desktop clients at least) support
  • Appears some versions of Apple Mail support with the X-Uniform-Type-Identifier: com.apple.mail-draft header (maybe worth adding). For versions that don't, it looks like there is an option to switch viewing a .eml to draft
  • Looks like Thunderbird 91+ supports natively and there is an addon for previous versions

@kbond kbond removed this from the 6.0 milestone Nov 27, 2021
@kbond kbond added this to the 6.1 milestone Nov 27, 2021
@fabpot
Copy link
Member

@fabpot fabpot commented Dec 20, 2021

@kbond Can you rebase on 6.1? Is it ready?

@stof
Copy link
Member

@stof stof commented Dec 20, 2021

the point brought by @kbond in #44311 (comment) is not resolved yet: how should the mailer component behave if being asked to send a DraftEmail object ?

@kbond kbond changed the base branch from 6.0 to 6.1 Dec 20, 2021
@kbond
Copy link
Member Author

@kbond kbond commented Dec 20, 2021

Rebased. Two remaining things:

@kbond
Copy link
Member Author

@kbond kbond commented Dec 20, 2021

To @GromNaN's original question. My personal use-case for setting X-Unsent in an email is removed once DraftEmail exists. Should I remove the check for this header in Email and Mailer? Then in Mailer, we'd throw an exception if $message instanceof DraftEmail.

Otherwise having code in the parent class for the behavior of a child class sounds a bad design.

I agree completely if we were calling a method on the child from the parent. This is a "generic" header that can be added to any Message. (I don't feel strongly one way or the other)

@GromNaN
Copy link
Member

@GromNaN GromNaN commented Dec 20, 2021

X-Unsent is actually recognized by Thunderbird and Outlook. Therefore that's legit to support it in the base Email class.

src/Symfony/Component/Mime/DraftEmail.php Outdated Show resolved Hide resolved
src/Symfony/Component/Mime/DraftEmail.php Outdated Show resolved Hide resolved
src/Symfony/Component/Mime/DraftEmail.php Outdated Show resolved Hide resolved
src/Symfony/Component/Mailer/Mailer.php Outdated Show resolved Hide resolved
@kbond kbond force-pushed the unsent-email branch 2 times, most recently from 7fbb065 to ad833aa Dec 22, 2021
@kbond
Copy link
Member Author

@kbond kbond commented Dec 22, 2021

ad833aa refactors this to have ensureValidity() throw the exception. I agree that having the exception thrown here makes the most sense.

fabpot
fabpot approved these changes Dec 22, 2021
@fabpot
Copy link
Member

@fabpot fabpot commented Dec 22, 2021

Thank you @kbond.

@fabpot fabpot closed this Dec 22, 2021
@fabpot fabpot merged commit 0d6e859 into symfony:6.1 Dec 22, 2021
@kbond kbond deleted the unsent-email branch Dec 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

6 participants