PROJET AUTOBLOG


BohwaZ

Archivé

Site original : BohwaZ

⇐ retour index

Lightweight PHP SMTP library

mercredi 26 septembre 2012 à 08:25

As always, I like to keep things simple and light in my code so one day I was looking for a PHP library able to send mails directly via SMTP. I still find it amazing that PHP doesn't have this feature in its standard package but hey why not. So I looked and I found some libraries from bigger frameworks like PEAR and Zend. They are good, but the PEAR Mail package depends on Net_SMTP, which depends on Socket, which depends on... You get the idea.

I like things that interact directly with standard PHP features, things that don't make a new interface for everything. For example PHP has a fairly good socket API so why bother adding an abstraction layer here?

So in a couple of hours, including some RFC reading (did you know that you can include comments in email addresses, like for example bohwaz@(oh that seems nice ! (but not as nice as emails addresses like "oh, look !"@example.tld))other.example.tld, what a nightmare that would be to parse with a regexp¹), and here it is: just a light and simple PHP SMTP library: lib.smtp.php

It's licensed under the LGPL and it can handle SSL, TLS, and SMTP-AUTH. It only sends text/plain emails in UTF-8 but <troll>who cares about HTML emails anyway?</troll>

¹ Even if I'm really supportive of the fight Stéphane Bortzmeyer is leading against the badly written email validation checks, I have to admit that it's sometimes tricky to do things right. Like here I had to extract multiple addresses from one string, and it's not easy if you don't want to use a very big and slow regexp.