PHP Amazon SES v3 - 缺少必需项 Header 'From'
PHP Amazon SES v3 - Missing Required Header 'From'
我必须将我的 PHP Amazon SES API 从版本 v2 升级到版本 v3。我在 v2 中使用的相同代码在 v3 中不起作用。
关注代码:
//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" .
"<h1>html text body</h1>OK\n" .
"\n" .
"\n" .
"\n" .
"--b1_3a1db7d5ae6b610cab5898f0be4a00a3--\n";
$ses->sendRawEmail(
[
'RawMessage' => [
'Data' => base64_encode($message),
]
]
);
当我 运行 代码时出现错误:
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:
<ErrorResponse xmlns="http://ses.amazonaws.com/doc/2010-12-01/">
<Error>
<Type>Sender</Type>
<Code>InvalidParameterValue (client): Missing required header 'From'. - <ErrorResponse xmlns="http://ses.amazonaws.com/doc/2010-12-01/">
<Error>
<Type>Sender</Type>
<Code>InvalidParameterValue</Code>
<Message>Missing required header 'From'.</Message>
</Error>
<RequestId>9ea0eaa9-746a-11e6-a6cb-21af912ef7d9</RequestId>
</ErrorResponse>
'
但是此代码适用于 API v2。
这里有什么问题?
我深入研究了 AWS SDK 源代码,发现不需要对消息进行编码。
所以,我删除了 base64_encode 功能,现在一切正常!!
最终代码为:
(...)
$ses->sendRawEmail(
[
'RawMessage' => [
'Data' => $message, // <-- Removed base64_encode from here
]
]
);
我必须将我的 PHP Amazon SES API 从版本 v2 升级到版本 v3。我在 v2 中使用的相同代码在 v3 中不起作用。
关注代码:
//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" .
"<h1>html text body</h1>OK\n" .
"\n" .
"\n" .
"\n" .
"--b1_3a1db7d5ae6b610cab5898f0be4a00a3--\n";
$ses->sendRawEmail(
[
'RawMessage' => [
'Data' => base64_encode($message),
]
]
);
当我 运行 代码时出现错误:
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:
<ErrorResponse xmlns="http://ses.amazonaws.com/doc/2010-12-01/">
<Error>
<Type>Sender</Type>
<Code>InvalidParameterValue (client): Missing required header 'From'. - <ErrorResponse xmlns="http://ses.amazonaws.com/doc/2010-12-01/">
<Error>
<Type>Sender</Type>
<Code>InvalidParameterValue</Code>
<Message>Missing required header 'From'.</Message>
</Error>
<RequestId>9ea0eaa9-746a-11e6-a6cb-21af912ef7d9</RequestId>
</ErrorResponse>
'
但是此代码适用于 API v2。
这里有什么问题?
我深入研究了 AWS SDK 源代码,发现不需要对消息进行编码。
所以,我删除了 base64_encode 功能,现在一切正常!!
最终代码为:
(...)
$ses->sendRawEmail(
[
'RawMessage' => [
'Data' => $message, // <-- Removed base64_encode from here
]
]
);