I had this idea a few months ago, but it's only now that I'm in a position where I'd be interested in implementing it, if possible.
I already know about setting both a HTML and a plan text version of any transactional emails which I sent from Laravel. The problem with this I have to maintain two separate view files, one for the html version, and one for the plain text. I know this might not sound like a big deal, but to my mind, this is duplication, and as such, not very DRY.
This is why I think having the ability to maintain a single markdown document for each transactional email would be a good way to go. For the HTML version of the email, simply parse the Markdown it into HTML, otherwise, leave it as is for the plain text version.
This of course is the great benefit of Markdown - that it's easy to understand what's what even in the unparsed syntax, and why I think it would be the perfect plain text alternative for HTML emails.
So my question is this, how would be go about implementing something like this?
Well, you'd use the API for Mandrill, Mailgun or such. You'd write the email in Markdown and then you parse it to HTML just before sending the API request and use the Markdown version for the plain text.
Here's an example of how we override the Mandrill transport class:
<?php namespace ScubaClick\Email;
use Illuminate\Mail\MailServiceProvider;
use ScubaClick\Email\Transport\MandrillTransport;
class EmailServiceProvider extends MailServiceProvider
{
/**
* {@inheritdoc}
*/
protected function registerMandrillTransport($config)
{
$mandrill = $this->app['config']->get('services.mandrill', []);
$this->app->bindShared('swift.transport', function() use ($mandrill)
{
return new MandrillTransport($mandrill['secret']);
});
}
}
In MandrillTransport we override the send method to inline styles to keep the templates clean, but I don't see a reason why that can't be used to parse the Markdown,
You then also have to add the new email provider to the providers list in app/config/app.php and remove Illuminate\Mail\MailServiceProvider to make it work.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community