RFC Proposal: Pluggable Mail Transports

php.internals

Dolf Schimmel

10 years ago
Howdy, I'm planning to create an RFC on this shortly, but would like to gauge the initial response first. Currently whenever an email is sent through the mail() function it is sent by an invocation of a sendmail-compatible executable. However, there are scenario's in which an alternative transport than through a sendmail executable may be desired. For example when PHP has been locked down, preventing it from starting any executables. It would be nice if it would be possible to allow such an alternative e.g. through a PHP Extension. Of course there are many frameworks out there that setup an smtp connection themselves, but in a shared hosting environment it's unfeasible to tell users they can't use the mail() function. This approach is akin to how sessions have a storage handler. PHP by default provides the 'file' and 'user' handlers. But the memcached extension provides a storage mechanism of its own which is then registered through php_session_register_module(). This would only implement an extra hook to allow for overriding the default sendmail mail transport (introducing a new ini setting) without bringing extra functionality to the php user land. Also, this hook would allow to implement other feature requests, like #29629. [1] The patch I'm proposing can be found here: https://github.com/php/php-src/pull/1778/files Cheers, Dolf 1. https://bugs.php.net/bug.php?id=29629

Keisial

10 years ago
I don't think more than a direct SMTP transport will be needed (LMTP perhaps?), but it seems a good idea that #29629 can finally be fixed.

Johannes Schlueter

10 years ago
On Sun, 2016-02-28 at 17:55 +0000, Dolf Schimmel wrote:
> Howdy, > > I'm planning to create an RFC on this shortly, but would like to gauge > the initial response first. > > Currently whenever an email is sent through the mail() function it is > sent by an invocation of a sendmail-compatible executable. However, > there are scenario's in which an alternative transport than through a > sendmail executable may be desired. For example when PHP has been > locked down, preventing it from starting any executables. It would be > nice if it would be possible to allow such an alternative e.g. through > a PHP Extension. Of course there are many frameworks out there that > setup an smtp connection themselves, but in a shared hosting > environment it's unfeasible to tell users they can't use the mail() > function.
For one: Why can't they use the mail function? A hoster can easily provide a sendmail-compatible program doing whatever is needed in the environment. The purpose of the function is to get rid of mail reliably and quickly in order to have it queued somewhere. PHP shouldn't be the MTA. That's a tough task (i.e. what to do when the SMTP server is down or slow? - PHP shouldn't have to wait) So I'd like to learn where mail() can't be used (aside from a dev desktop where this might need "too much" system configuration) And as a side note: If you really have a special case where providing a sendmail tool is no option you could easily create an extension replacing mail() with a custom function (mbstring functions, runkit and other extensions show how this can be done)
> The patch I'm proposing can be found here: > https://github.com/php/php-src/pull/1778/files
I haven't fully reviewed this. But you're not handing extension unloading. In some SAPIs we have dl() available and serve multiple requests. Thus in one request a mail module could be loaded by dl() on shutdown the extension will be unloaded, but the pointer will still exist in the handler table. johannes