14 Apr 2011

Send HTML email with an attachment from PHP

It took me a painfully long time to find a PHP script that could send multi-part email (with plain text / fancy HTML body content) and an attachment that actually worked! But I have adapted this excellent script to suit my needs (excluding output buffering). So reposting it mostly for my own reference... hopefully it can help some other poor sods out too.


$htmlbody = "This is an <b>html</b> message";
$textmessage = "This is a plain text message";

//Set Recipient Address
$to = "recipient@domain.com";

//Set Email Subject
$subject = "HTML email with attachment";

//define the from \ reply to headers
$headers = "From: yourname@domain.com\r\nReply-To: yourname@domain.com";

//create a unique boundary string to delimit different parts of the email (plain text, html, file attachment)
$random_hash = md5(date('r', time()));

//add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";

//read the atachment file contents into a string,
//encode it with MIME base64,
//and split it into smaller chunks for sending
$attachment = chunk_split(base64_encode(file_get_contents('attachment.zip')));

//define the body of the message.
$message = "--PHP-mixed-$random_hash\r\n"
."Content-Type: multipart/alternative; boundary=\"PHP-alt-$random_hash\"\r\n\r\n";
$message .= "--PHP-alt-$random_hash\r\n"
."Content-Type: text/plain; charset=\"iso-8859-1\"\r\n"
."Content-Transfer-Encoding: 7bit\r\n\r\n";

//Insert the plain text message.
$message .= strip_tags($textmessage);
$message .= "\r\n\r\n--PHP-alt-$random_hash\r\n"
."Content-Type: text/html; charset=\"iso-8859-1\r\n"
."Content-Transfer-Encoding: 7bit\r\n\r\n";

//Insert the html message.
$message .= $htmlbody;
$message .="\r\n\r\n--PHP-alt-$random_hash--\r\n\r\n";

//include attachment
$message .= "--PHP-mixed-$random_hash\r\n"
."Content-Type: application/zip; name=\"attachment.zip\"\r\n"
."Content-Transfer-Encoding: base64\r\n"
."Content-Disposition: attachment\r\n\r\n";
$message .= $attachment;
$message .= "/r/n--PHP-mixed-$random_hash--";

//send the email
$mail = mail( $to, $subject , $message, $headers );

if ($mail) {
echo "Mail sent!";
} else "Mail not sent!";


2 Apr 2011

Vodacom goes red, like Vodafone

I've always wondered if Vodacom would ever adopt the Vodafone branding and yesterday it happened! Vodacom has rebranded to align themselves more obviously with Vodafone who now own 65% of this South African mobile services provider. Vodacom has retained their name (Vodafone + Telkom from the old days) but it's been updated to the Vodafone type face and 'quote' logo. The Vodacom blue has also been replaced with racy Vodafone Red.

Hopefully the new look and feel won't require much more than a few cosmetic changes to the Vodacom website on the back of the Vodacom4me migration, however Vodacom is promising that the rebrand will come with much more than some new lipstick and will accompany many welcome improvements including network upgrades and customer service enhancements. I think the new look is awesome! and I am looking forward to all the new changes. @Vodacom is doing a great job on responding to all sorts of customer queries about the new look on Twitter and I caught this 'historic' image of the old Vodacom logo while it was still cached in Vodacom's Twitter feed, capturing a little piece of Vodacom's brand evolution...

DefZA is in the process of updating his free SMS website to make use of vodacom.co.za since vodacom4me.co.za is now officially offline.

Free082SMS.co.za has also been updated to cope with the new website changes. The new Red Vodacom website at www.vodacom.co.za went down (understandably) as I was typing this post, but I was surprised by this very decent Vodacom maintenance page, which is a welcome improvement on any Vodacom error pages I have seen before.

Hopefully this is a sign of things to come, even if it means Vodacom is only going to start dealing with problems in a more elegant manner - A welcome change, even if they will not be retiring Mo the Meercat.