Ever felt like your ProcessWire emails look like they're stuck in 1999? You know the drill - sending emails is super easy with WireMail:
$m = new WireMail();
$m->from('foo@bar.com');
$m->to('xxx@yyy.com');
$m->subject('Hello there!');
$m->bodyHTML('<h1>This is great!</h1><p>I am an ugly mail...</p>');
$m->send();
But let's be honest - they look about as pretty as a website built with Microsoft FrontPage! 😅
🪄 Enter the Mail Pimp Hook!
Drop this magical hook into your /site/ready.php (or even better Site.module.php), and watch your emails transform from ugly ducklings into beautiful swans:
<?php
$wire->addHookBefore('WireMail::send', function(HookEvent $event) {
// double check that we got a wiremail instance
// this also tells the IDE what $mail is (to get IntelliSense)
$mail = $event->object;
if (!$mail instanceof WireMail) return;
// get current mail body
$html = $mail->get('bodyHTML');
if (!$html) return;
// get email layout markup
$layoutFile = wire()->config->paths->templates . 'mails/default.html';
if (!is_file($layoutFile)) return;
// replace ##content## with actual mail content
$html = str_replace(
'##content##',
$html,
wire()->files->render($layoutFile)
);
// write new body to mail
$mail->bodyHTML($html);
});
The HTML
Just create a beautiful MJML template at /site/templates/mails/default.mjml, put ##content## where your email content should go, convert it to HTML and BOOM! 💥 Every email gets automatically wrapped in your gorgeous template.
No more CSS wrestling matches, no more "Why does this look different in Outlook?" headaches. Just pure email beauty, automagically! ✨
Now your clients will think you spent days crafting those emails, when in reality, you're sipping coffee while your hook does all the heavy lifting. Work smarter, not harder! 🚀
#ProcessWire #EmailMagic #NoMoreUglyEmails
PS: This is the MJML template that I used:
<mjml>
<mj-head>
<mj-attributes>
<mj-all font-family="Tahoma" />
<mj-text line-height="140%" />
</mj-attributes>
</mj-head>
<mj-body background-color="#efefef">
<mj-section
background-color="#ffffff"
background-repeat="repeat"
padding-bottom="30px"
padding-top="30px"
text-align="center"
>
<mj-column>
<mj-image
align="center"
padding="25px"
src="xxx"
target="_blank"
width="200px"
alt="Logo"
></mj-image>
<mj-text>##content##</mj-text>
</mj-column>
</mj-section>
<mj-section>
<mj-column>
<mj-text
font-size="10px"
color="#a0a0a0"
align="center"
>
powered by
<a
href="https://www.baumrock.com/"
style="color: #158f66"
>baumrock.com</a
>
</mj-text>
</mj-column>
</mj-section>
</mj-body>
</mjml>
VSCode has an extension to get a live preview and export MJML to HTML:
And here are some other free templates: https://mjml.io/templates
I use https://www.base64-image.de/ to add the logo to my mail template as src="data:image/jpeg;base64,/9j/4QAWRXhpZgAATU0AKgAAAA..." to avoid headaches with image paths, remote assets blocking etc.