Internal Server Error 500 when sending mail to AWS SES with cakephp running on Hiawatha

1.3k views Asked by At

I'm getting "Internal Server Error 500" after the third or sometimes at the first attempt to send a mail using CakePHP 3 through AWS SES account (in production mode) running on Hiawatha server.

Here is my php code:

  public function sendmail()
{
    $email = new Email();
    $email->transport('SES');
    try {
        $res = $email->from(['[email protected]' => 'Name'])
              ->to(['[email protected]' => 'Receiver'])
              ->subject('Test mail')
              ->send('some text');
    } catch (Exception $e) {
        $this->Flash->error('Error. Please, try again.');
        echo 'Exception : ',  $e->getMessage(), "\n";
        return $this->redirect('/');
    }
    $this->Flash->success('Ok. You will receive a confirmation mail');
    return $this->redirect('/');} 

Here is the transport configuration

     'EmailTransport' => [
     'SES' => [
         'host' => 'email-smtp.eu-west-1.amazonaws.com',
         'port' => 25,
         'timeout' => 60,
         'username' => 'ASDFASADQWE',
         'password' => 'FSDFDSFDSFSEREWRWERWER',
         'tls' => true,
         'className' => 'Smtp'
     ],

port 465 and 587 are not working at the first attemp

So, basically I can't identify if the problem came from CakePHP, AWS SES or some configuration on the server.

Thank you for any recommendation.

1

There are 1 answers

0
coder On

At the end I stop to use cakePHP mail and setup PHPMailer, some difficulties to use compose and make it run, however at the end this is the working code that I can send many mails in a row.

   public function sendMailPHPMailer()
    {
      $mail = new \PHPMailer();
      $mail->isSMTP();                                      
      $mail->Host = 'email-smtp.eu-west-1.amazonaws.com';  
      $mail->SMTPAuth = true;                              
      $mail->Username = 'username'; 
      $mail->Password = 'password';
      $mail->SMTPSecure = 'tls';    
      $mail->Port = 587;                               
      $mail->From = '[email protected]';
      $mail->FromName = 'cakePHP PHPMailer';
      $mail->addAddress('[email protected]', 'receiver');
      $mail->isHTML(true);                               
      $mail->Subject = 'Test using PHPMailer & SES';
      $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
      $mail->AltBody = 'This is the body in plain text';

      if(!$mail->send()) {
          $this->Flash->error('error');
          echo 'Exception : ',  $mail->ErrorInfo, "\n";
          return $this->redirect('/');
        }else{
          $this->Flash->success('ok');
          return $this->redirect('/');
        }
    }

And with this code I can send only 3 mails with an interval of 1s then I receive an error 500.

    public function sendmail()
    {
        $email = new Email();
        $email->transport('SES');
        try {
            $res = $email->from(['[email protected]' => 'cakePHP mail'])
                  ->to(['[email protected]' => 'receiver'])
                  ->subject('cakePHP & SES')
                  ->send('message via cakePHP and SES');
        } catch (Exception $e) {
            $this->Flash->error('error');
            echo 'Exception : ',  $e->getMessage(), "\n";
            return $this->redirect('/');
        }
        $this->Flash->success('ok');
        return $this->redirect('/');
}