~bohwaz/blog/

Avec de vrais morceaux de 2.0 !

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.

Write a comment
(optional)
(optional)
(mandatory)
 _                   _           
| |__  _ __ ___   __| | ___ _ __ 
| '_ \| '__/ _ \ / _` |/ _ \ '__|
| |_) | | | (_) | (_| |  __/ |   
|_.__/|_|  \___/ \__,_|\___|_|   
                                 
(mandatory)

URLs will create links automatically.
Allowed HTML tags: <blockquote> <cite> <pre> <code> <var> <strong> <em> <del> <ins> <kbd> <samp> <abbr>

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.

preg_match_all('/^(\\S.*?):(.*?)\\s*(?=^\\S|\\Z)/sm', $headers, $m);

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.

$key = preg_replace_callback('/^.|(?<=-)./', function ($m) { return ucfirst($m[0]); }, strtolower(trim($key)));

Also note that your CSS is broken, there's a thick vertical gray line across this textarea.

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'] = ''; }

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