easy-swoole / smtp

10 stars 7 forks source link

Requested action not taken: too many emails per second #21

Closed ljfreelancer88 closed 3 years ago

ljfreelancer88 commented 3 years ago

Hi, is there any way to control in the config that if the first email is still sending, the following emails should be queued because I got the following error below. Or just add coroutine sleep after sendTo() as work around.

PHP Fatal error:  Uncaught EasySwoole\Smtp\Exception\Exception: 550 5.7.0 Requested action not taken: too many emails per second
 in /var/www/html/easyswoole/smtp/vendor/easyswoole/smtp/src/MailerClient.php:142
Stack trace:
#0 /var/www/html/easyswoole/smtp/vendor/easyswoole/smtp/src/MailerClient.php(91): EasySwoole\Smtp\MailerClient->recvCodeCheck()
#1 /var/www/html/easyswoole/smtp/vendor/easyswoole/smtp/src/Mailer.php(36): EasySwoole\Smtp\MailerClient->send()
#2 /var/www/html/easyswoole/smtp/index.php(68): EasySwoole\Smtp\Mailer->sendTo()
#3 {main}
  thrown in /var/www/html/easyswoole/smtp/vendor/easyswoole/smtp/src/MailerClient.php on line 142

This is my sample code.


$http->on('request', function (Request $request, Response $response) {
        print_r($response);

    if ($request->server['request_method'] !== 'POST') {
        return;
    }

    // IF swoole.use_shortname = off THEN use Coroutine::create instead of go()
    Coroutine::create(function() use ($request, $response) {
        $config = new MailerConfig();
        $config->setServer('smtp.mailtrap.io');
        $config->setSsl(false);
        $config->setUsername('7777777');
        $config->setPassword('333333');
        $config->setPort(2525);
        $config->setMailFrom('noreply@domain.com');
        $config->setTimeout(10); // Set connection timeout of the client
        $config->setMaxPackage(1024 * 1024 * 5); // Set the size of the packet to be sent

        $mimeBean = new Html();
        $mimeBean->setSubject('Hello Word!');
        $mimeBean->setBody('<h1>Hello Word</h1>');

        $mailer = new Mailer($config);
        $mailer->sendTo('dev@domain.com', $mimeBean);
    });

    $response->header("Content-Type", "text/plain");
    $response->end("Hello World\n");
});
Player626 commented 3 years ago

This is controlled by the user himself.

Player626 commented 3 years ago

You can try using the redis lock.