Monday 26 October 2009

Mail2Blogger formatting

One of the really neat features in Blogger is the ability to create a blog entry simply by sending an email - then subject line becomes the title, the main body of the email becomes the content.


But looking at the output, I found some annoying formatting issues. Somewhere in the process, line-return characters were being added - resulting in a much narrower column for emailed entries than for normally-created entries.

And whilst the first section of content was delivered as a <div>, subsequent paragraphs were delivered as <p> paragraphs. The solution is to replace the opening <p> tag with two line-returns, and to ignore the closing </p> tag.

If you are using the output in an external page, and have PHP on your server, then for modest overhead, you can do some simple replacement to tidy up the output. Luckily, the line-returns added (at least in my case, from Outlook 2000) are plain <br> tags, whereas deliberately-added line-returns in Blogger are <br /> - so the text can be tidied up in a single pass like this:
$findArray=Array("<br>","<p>","</p>");
$replaceArray=Array(" ","<br /><br />","");
$blogHTML=file_get_contents(filePath("/Members/blog/blog.htm"));
$blogHTML=str_replace($findArray,$replaceArray,$blogHTML);
echo $blogHTML;

An extension of this could take care of fixed-format "signature" text added at the end of your emails (such as "sent from my iPhone") - but it might take a bit more programming to take care of any variable text (such as ads) that may be added by some email services.

1 comment:

Tuff Risk said...

Can you elaborate on where that PHP code would be placed in the page?