Lightweight PHP SMTP library
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.
LK
I spotted several bugs and TODOs in your lib.smtp.php.
One is the handling of Cc and Bcc. You don't merge arrays with array_merge, instead, you only take the last array.
Another is that extractEmailAddresses echoes unrecognized addresses instead of skipping them or throwing exceptions.
Using a default server and port localhost:25 would be nice.
You could also easily add support for parsing headers from a string, so your send function would be almost compatible with the built-in mail function.
Also, if you didn't overwrite content-type and such, the user could send manually built multipart messages (HTML and attachments), just as with the built-in mail function.
Normalizing header names would also be good, so that you'd recognize content-type and Content-Type as the same header and overwrite neither.
Also note that your CSS is broken, there's a thick vertical gray line across this textarea.
BohwaZ
Thank you for your help! I applied all of your useful suggestions to the SMTP class which is now a part of the KD2 micro-framework, your patch is available here: https://fossil.kd2.org/kd2fw/fdiff?v1=ab4815bcc9f8dbf5&v2=820842c7382cd90e
As for the CSS I don't see anything broken, what is your web browser?
Cheers.
Kuma
There is a bug when subject is empty, it show "=?UTF-8?B??=" in my email client (Kaiten Mail, Android App). And here my code to fix it:
if (!empty($subject)) { $headers['Subject'] = '=?UTF-8?B?'.base64_encode($subject).'?='; } else { $headers['Subject'] = ''; }
BohwaZ
Fixed: https://fossil.kd2.org/kd2fw/info/0b16c07245
Thanks!
Anonymous
Seems good, where is the docs though?
BohwaZ
Just read the code ;)
MaxMendez
At first thanks for this class.
For someone who wants to use it fast only need to do this:
require_once('llib.smtp.php');
$smtp = new SMTP($server', $port, $username, $password, $secure [None=0, TSL=1, SSL=2]);
$smtp->send( $to, $subject, $message, $array_of_headers );
That's it