Last active
February 22, 2020 11:04
-
-
Save stephenfrank/7053148 to your computer and use it in GitHub Desktop.
Chatting to an SMTP server with ReactPHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| require "vendor/autoload.php"; | |
| use React\Promise\Deferred; | |
| $loop = React\EventLoop\Factory::create(); | |
| $stream = @ stream_socket_client('tcp://gmail-smtp-in.l.google.com:25', $errNo, $errStr, 10); | |
| $connection = new React\Socket\Connection($stream, $loop); | |
| $deferred = new React\Promise\Deferred(); | |
| $connection->on('data', | |
| function ($data) { | |
| echo "<<< $data\n"; | |
| } | |
| ); | |
| $connection->once('data', | |
| function ($data, $conn) use ($deferred) { | |
| $deferred->resolve(array($data, $conn)); | |
| } | |
| ); | |
| $deferred->promise() | |
| ->then(function ($data) use ($connection) { | |
| $deferred = new Deferred(); | |
| $connection->write("HELO flowsa.com\r\n"); | |
| $connection->once('data', | |
| function ($data) use ($deferred) { | |
| $deferred->resolve($data); | |
| } | |
| ); | |
| return $deferred->promise(); | |
| } | |
| ) | |
| ->then(function ($data) use ($connection) { | |
| $deferred = new Deferred(); | |
| $connection->write("MAIL FROM: <stephen@flowsa.com>\r\n"); | |
| $connection->once('data', | |
| function ($data) use ($deferred) { | |
| $deferred->resolve($data); | |
| } | |
| ); | |
| return $deferred->promise(); | |
| } | |
| ) | |
| ->then(function ($data) use ($connection) { | |
| $deferred = new Deferred(); | |
| $connection->write("RCPT TO: <stephenfrankza@gmail.com>\r\n"); | |
| $connection->once('data', | |
| function ($data) use ($deferred) { | |
| $deferred->resolve($data); | |
| } | |
| ); | |
| return $deferred->promise(); | |
| } | |
| )->then(function ($data) use ($connection) { | |
| $deferred = new Deferred(); | |
| $connection->write("RCPT TO: <this-email-does-not-exist-asdf123@gmail.com>\r\n"); | |
| $connection->once('data', | |
| function ($data) use ($deferred) { | |
| $deferred->resolve($data); | |
| } | |
| ); | |
| return $deferred->promise(); | |
| } | |
| ); | |
| $loop->run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This was great, used it to write tests for an SMTP server all inside the same loop.
https://bitbucket.org/david_garcia_garcia/smtpserver/src/a4b5f786363a284908b8c3ab478d17f66820b523/tests/ServerTest.php?at=1.0.x&fileviewer=file-view-default#ServerTest.php-100