Question:
I have to upgrade my PHP Amazon SES API from version v2 to version v3. The same code I had working in v2 does not working in the v3.
Follow the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
//Send the message (which must be base 64 encoded): $ses = new SesClient([ 'credentials' => new Credentials( $this->connection->getUsername(), $this->connection->getPassword() ), 'region' => $this->connection->getServer(), 'version' => '2010-12-01' ]); // the body message generated by PHP Mailer: $message = "Date: Tue, 6 Sep 2016 16:46:35 -0300\n" . "To: some@email.com\n" . "From: my_registered_email@at.amazon.com\n" . "Reply-To: my_registered_email@at.amazon.com\n" . "Subject: Email Subject\n" . "Message-ID: <3a1db7d5ae6b610cab5898f0be4a00a3@machine-id>\n" . "X-Mailer: PHPMailer 5.2.16 (https://github.com/PHPMailer/PHPMailer)\n" . "MIME-Version: 1.0\n" . "Content-Type: multipart/alternative;\n" . " boundary=\"b1_3a1db7d5ae6b610cab5898f0be4a00a3\"\n" . "Content-Transfer-Encoding: 8bit\n" . "\n" . "This is a multi-part message in MIME format.\n" . "\n" . "--b1_3a1db7d5ae6b610cab5898f0be4a00a3\n" . "Content-Type: text/plain; charset=us-ascii\n" . "\n" . "html text bodyOK\n" . "\n" . "\n" . "--b1_3a1db7d5ae6b610cab5898f0be4a00a3\n" . "Content-Type: text/html; charset=us-ascii\n" . "\n" . " html text bodyOK\n" . "\n" . "\n" . "\n" . "--b1_3a1db7d5ae6b610cab5898f0be4a00a3--\n"; $ses->sendRawEmail( [ 'RawMessage' => [ 'Data' => base64_encode($message), ] ] ); |
When I run the code I got the error:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
PHP Fatal error: Uncaught exception 'Aws\Ses\Exception\SesException' with message 'Error executing "SendRawEmail" on "https://email.us-east-1.amazonaws.com"; AWS HTTP error: Client error: `POST https://email.us-east-1.amazonaws.com` resulted in a `400 Bad Request` response: InvalidParameterValue (client): Missing required header 'From'. - InvalidParameterValue ' |
But this code worked with the API v2.
What is wrong here?
Answer:
I digged into the AWS SDK source code and I realize that I do not need to encode the message.
So, I removed the base64_encode function and everything is working now!!
The final code is:
1 2 3 4 5 6 7 8 9 |
(...) $ses->sendRawEmail( [ 'RawMessage' => [ 'Data' => $message, // <-- Removed base64_encode from here ] ] ); |