Search code examples
phpamazon-web-servicesamazon-ses

Sending email to Amazon SES in PHP freezes when triggered by request


Unable to send an email on demand using AWS SesClient

I am trying to send an email for a password recovery on demand. That is I receive an http request in my API and I call sendBulkTemplatedEmail() through a function called send_email().

Here is the thing:

  • If I execute the php file containing the previous code from the server command line using php finename.php, the email is sent successfully.
  • However, when the function is triggered by a request, sendBulkTemplatedEmailfunction simply freezes.
    send_email($destination_email, $type){
        /.../   
        try {
            $SesClient->sendBulkTemplatedEmail($data_to_attach);
            $messageId = 1;
            echo("Email sent! Message ID: $messageId"."\n");
            return true;
        } catch (AwsException $e) {
            /..../
        }
    }

Solution

  • I suspect that a different user is being used in each case.

    When you run php from the command-line, it is running as your logged-in user (ec2-user?). However, when it is triggered via a request, it is running as a different user (apache?), and that user does not have credentials in their ~/.aws/credentials file.

    This can be fixed by copying the credentials to the other user's ~/.aws directory.