Multipart (MIME) email with PHP: Creating the text and HTML parts simultaneously

4 replies
I'm creating my first PHP script that generates multipart emails, and I'd like some consistent method for creating the text and HTML parts at the same time. There are plenty of pages out there that say, "put the text part here and the HTML part there", but I haven't been able to find anything that gets into the building of those two parts. Each part uses the identical math and calls to the database, so I really rather not have to build (essentially) the same thing twice. The closest "solution" seems to be to build the HTML normally and then strip out all of the tags, but that doesn't allow for much control over the resulting text part. e.g., There, the headings should probably be preceded by a blank line or two, the [br/] tag should force a new line, and maybe some pictures should be replaced with the contents of their alt attribute.

What feels most "natural" to me would be some sort of simple package where I can create the two parts simultaneously, rather than treating the text part as an afterthought.

This certainly feels like one of those things that surely must have already been written. I'd rather not re-invent the wheel, although admittedly I've already started to roll my own solution, with a few functions that use a two-element array with keys "text" and "html".

I'm trying hard to not make this discussion about the merits of PHP's mail() function vs. PEAR's Mail package vs. PHPMailer. I do realize there are multiple ways to send off the mail once it's crafted; my question is about building it in the first place.

Any ideas?
#creating #email #html #mime #multipart #parts #php #simultaneously #text

Trending Topics