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
  • Profile picture of the author 2WDHost
    Hi David.

    This could help: https://github.com/soundasleep/html2text
    Signature
    2WDH.com: Professional cPanel Hosting since 2003
    Really helpful support. You're not a number to us! Programming, database and site development help.
    No overselling. SSD. Real 99.99%+ uptime. Free 2-week trial: no obligation/credit card required.
    {{ DiscussionBoard.errors[9198723].message }}
    • Profile picture of the author valvednd
      i have a sql proc for this so i just store a text version of html content right along side it. emails don't convert so nicely because they are often table dependent which creates some odd spacing issues sometimes, but its easy enough to address w/ some replace hackery on the sql side... You just have to be smart about the content, instead of anchored text just put a "click here: http://www.blah.com" so that the text converts well and doesnt end up with a dead link.

      i'd be happy to send you the proc if you pm me.
      {{ DiscussionBoard.errors[9199055].message }}
      • Profile picture of the author David Beroff
        Originally Posted by valvednd View Post

        i have a sql proc for this so i just store a text version of html content right along side it.
        Interesting; you're auto-converting on the database server side. I can see that working for a lot of cases, although what I'm doing is generating the outbound emails on-the-fly, rather than storing the text. Thanks, though!
        Signature
        Put MY voice on YOUR video: AwesomeAmericanAudio.com
        {{ DiscussionBoard.errors[9199060].message }}
    • Profile picture of the author David Beroff
      Originally Posted by 2WDHost View Post

      That's one good answer; thank you. I'm also now reading the corresponding original discussions:

      Converting HTML to plain text in PHP for e-mail - Stack Overflow
      php - HTML to plain text (for email) - Stack Overflow
      Signature
      Put MY voice on YOUR video: AwesomeAmericanAudio.com
      {{ DiscussionBoard.errors[9199056].message }}

Trending Topics