shuchkin/react-smtp-client

ReactPHP 异步 SMTP 客户端

0.2 2020-02-19 04:16 UTC

This package is auto-updated.

Last update: 2024-09-19 14:52:05 UTC


README

ReactPHP 异步 SMTP 客户端,用于发送邮件,类似于 php mail() 功能。内置简单的 UTF-8 文本/纯文本消息。

基本用法

$loop = \React\EventLoop\Factory::create();

$smtp = new \Shuchkin\ReactSMTP\Client( $loop ); // localhost:25

$smtp->send('info@example.org', 'sergey.shuchkin@gmail.com', 'Test ReactPHP mailer', 'Hello, Sergey!')->then(
    function() {
        echo 'Message sent'.PHP_EOL;
    },
    function ( \Exception $ex ) {
        echo 'SMTP error '.$ex->getCode().' '.$ex->getMessage().PHP_EOL;
    }
);

$loop->run();

Google SMTP 服务器 – 如何免费发送批量邮件

$loop = \React\EventLoop\Factory::create();

$smtp = new \Shuchkin\ReactSMTP\Client( $loop, 'tls://smtp.gmail.com:465', 'username@gmail.com','password' );

$recipients = ['sergey.shuchkin@gmail.com','example@example.com'];

foreach( $recipients as $to ) {

    $smtp->send('username@gmail.com', $to, 'Test ReactPHP mailer', 'Hello, Sergey!')->then(
        function() use ( $to ) {
            echo 'Message to '.$to.' sent via Google SMTP'.PHP_EOL;
        },
        function ( \Exception $ex ) use ( $to ) {
            echo 'Message to '.$to.' not sent: '.$ex->getMessage().PHP_EOL;
        }
    );
}

$loop->run();

Google 个人 SMTP 限制为每 24 小时 99 封邮件。

使用 mime/mail 类发送邮件和附件

查看 https://github.com/shuchkin/simplemail

$ composer require shuchkin/simplemail
$smtp = new \Shuchkin\ReactSMTP\Client( $loop, 'example.com:25', 'username', 'password' );

// setup fabric
$sm = new \Shuchkin\SimpleMail();
$sm->setFrom( 'example@example.com' );
$sm->setTransport( function ( \Shuchkin\SimpleMail $m, $encoded ) use ( $smtp ) {

    $smtp->send( $m->getFromEmail(), $encoded['to'], $encoded['subject'], $encoded['message'], $encoded['headers'] )
        ->then(
            function () {
                echo "\r\nSent mail";
            },
            function ( \Exception $ex ) {
                echo "\r\n" . $ex->getMessage();
            }
        );
});

// send mail
$sm->to( ['sergey.shuchkin@gmail.com', 'reactphp@example.com'] )
    ->setSubject('Async mail with ReactPHP')
    ->setText('Async mail sending perfect! See postcard')
    ->attach('image/postcard.jpg')
    ->send();

安装

推荐通过 Composer 安装此库。 Composer 新手?

这将安装最新的支持版本

$ composer require shuchkin/react-smtp-client

变更日志

0.2 (2020-02-19) - 内置基本的 UTF-8 文本/纯文本消息,composer.json 中的 ReactPHP 实际版本

0.1.1 (2019-03-12) - 初次发布