Akollade / mjml-bundle

✉️ Symfony bundle for MJML
MIT License
107 stars 17 forks source link

Sending images as inline attachments? #74

Open mpdude opened 3 years ago

mpdude commented 3 years ago

When sending emails (after all, that's what MJML is all about) that contain images, it is often helpful or even necessary to attach images to the emails – that way, you can ensure emails are rendered correctly, without blocking access to "external" resources in the mail client.

I understand that this currently out of scope for this bundle, since all it does it to provide a way to apply the MJML -> HTML transformation for Twig templates, and also the existing integration/plugin/support for SwiftMailer has been deprecated.

However, the problem seems to be closely related to MJML processing and the use case should make sense for most of the people using this bundle.

The downside is that supporting it would mean that not only the MJML/HTML would have to be processed, but also some kind of integration with the mailer system used (SwiftMailer, Symfony Mailer, ...?) would be necessary to support attachments to the messages.

What do you think – would you be open for proposals or contributions in this regard, and have you (as the maintainer) faced this problem before?

aprat84 commented 2 weeks ago

AFAIK, mjml respects whats in the src attribute. So, as you said, I think this is out of mjml scope.

If you use the symfony/mailer component, according to the docs it should be easy to do!

If you use the symfony framework:

$email = (new TemplatedEmail())
    // ...
    ->htmlTemplate('emails/signup.html.twig')
<!-- emails/signup.html.twig -->
<img src="{{ email.image('@images/logo.png') }}" alt="Logo">

If you use the component standalone:

$email = (new Email())
$email->addPart((new DataPart(new File('/path/to/images/signature.png'), 'signature', 'image/png'))->asInline());
$email->html($twig->render('path/to/email.html.twig'));
<!-- path/to/email.html.twig -->
<img src="cid:signature" alt="signature">