So I have a PHP script which takes in piped emails, appends a footer to them and passes them on.
But if someone sends an email which is already in HTML format it just inserts the entire html email into the body of a new html document.
I need a script which will (if the email is already in HTML) take off the html, head and body tags leaving the original email.
I.e. if someone sent an email
<html><body>This is my awesome input email which is <strong>already</strong> in HTML</body></html>
It is parsed by my script to become
<html><body><html><body>This is my awesome input email which is <strong>already</strong> in HTML</body></html></body></html>
How can I get it to take off the HTML structure if it exists?
I don’t think it’s possible to detect if the
htmlelement is present when working with DOMDocument and HTML becauseloadHTML()will add its ownhtmlelement if it is not present.The code below will just always return the serialised HTML of the
bodyelement.CodePad.
Alternatively, you could treat the HTML as XML and then detect it, but without a
documentElementyou may have problems. I solved that by adding a dummydocumentElement, though it’s a bit clunky (I’d probably stick to the above code myself).CodePad.