使用AWS SDK for PHP发送电子邮件

时间:2017-12-22 07:57:55

标签: amazon-web-services amazon-ses

我正在尝试使用AWS SDK for PHP设置电子邮件,到目前为止还没有成功。我做了以下事情:

  1. 验证了我的域名和收件人电子邮件地址。
  2. 生成我的AWS访问密钥
  3. 3.安装pHP版本7.0.25

    4.安装了适用于PHP版本3的AWS开发工具包。

    1. 创建了一个共享凭据文件。
    2. 文档指示我需要将此文件保存到以下位置:"〜/ .aws / credentials" (我正在使用MAC)

      我的问题是我没有" .aws"文件夹,所以我在我的主目录中创建了一个(隐藏的aws目录),然后保存了我的"凭证"文件在那里(包含访问密钥凭据)。我收到以下错误:

       PHP Fatal error:  Uncaught Aws\Exception\CredentialsException: Error 
       retrieving credentials from the instance profile metadata server. (Client 
       error: `GET http://169.254.169.254/latest/meta-data/iam/security-
       credentials/` resulted in a `404 Not Found` response:
       <?xml version="1.0" encoding="iso-8859-1"?>
       <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
           "htt (truncated...)
       ) in /home/ec2-user/vendor/aws/aws-sdk-
       php/src/Credentials/InstanceProfileProvider.php:79
       Stack trace:
       #0 /home/ec2-user/vendor/guzzlehttp/promises/src/Promise.php(203): 
       Aws\Credentials\InstanceProfileProvider->Aws\Credentials\{closure}
       (Object(GuzzleHttp\Exception\ClientException))
       #1 /home/ec2-user/vendor/guzzlehttp/promises/src/Promise.php(156): 
       GuzzleHttp\Promise\Promise::callHandler(2, Array, Array)
       #2 /home/ec2-user/vendor/guzzlehttp/promises/src/TaskQueue.php(47): 
       GuzzleHttp\Promise\Promise::GuzzleHttp\Promise\{closure}()
       #3 /home/ec2-
       user/vendor/guzzlehttp/guzzle/src/Handler/CurlMultiHandler.php(96): 
       GuzzleHttp\Promise\TaskQueue- in /home/ec2-user/vendor/aws/aws-sdk-
       php/src/Credentials/InstanceProfileProvider.php on line 79
      

      在终端中执行的PHP脚本(如aws:http://docs.aws.amazon.com/ses/latest/DeveloperGuide/send-using-sdk-php.html所述):

       <?php
      
       // Replace path_to_sdk_inclusion with the path to the SDK as described in 
       // http://docs.aws.amazon.com/aws-sdk-php/v3/guide/getting-started/basic-
       usage.html
       define('REQUIRED_FILE','path_to_sdk_inclusion'); 
      
       // Replace sender@example.com with your "From" address. 
       // This address must be verified with Amazon SES.
       define('SENDER', 'sender@example.com');           
      
       // Replace recipient@example.com with a "To" address. If your account 
       // is still in the sandbox, this address must be verified.
       define('RECIPIENT', 'recipient@example.com');    
      
       // Specify a configuration set. If you do not want to use a configuration
       // set, comment the following variable, and the 
       // 'ConfigurationSetName' => CONFIGSET argument below.
       define('CONFIGSET','ConfigSet');
      
       // Replace us-west-2 with the AWS Region you're using for Amazon SES.
       define('REGION','us-west-2'); 
      
       define('SUBJECT','Amazon SES test (AWS SDK for PHP)');
      
       define('HTMLBODY','<h1>AWS Amazon Simple Email Service Test Email</h1>'.
                    '<p>This email was sent with <a 
       href="https://aws.amazon.com/ses/">'.
                    'Amazon SES</a> using the <a href="https://aws.amazon.com/sdk-
       for-php/">'.
                    'AWS SDK for PHP</a>.</p>');
       define('TEXTBODY','This email was send with Amazon SES using the AWS SDK for 
       PHP.');
      
       define('CHARSET','UTF-8');
      
       require REQUIRED_FILE;
      
       use Aws\Ses\SesClient;
       use Aws\Ses\Exception\SesException;
      
       $client = SesClient::factory(array(
           'version'=> 'latest',     
           'region' => REGION
       ));
      
       try {
            $result = $client->sendEmail([
           'Destination' => [
               'ToAddresses' => [
                   RECIPIENT,
              ],
       ],
      'Message' => [
          'Body' => [
              'Html' => [
                  'Charset' => CHARSET,
                  'Data' => HTMLBODY,
              ],
              'Text' => [
                  'Charset' => CHARSET,
                  'Data' => TEXTBODY,
              ],
          ],
          'Subject' => [
              'Charset' => CHARSET,
              'Data' => SUBJECT,
          ],
      ],
      'Source' => SENDER,
      // If you are not using a configuration set, comment or delete the
      // following line
           'ConfigurationSetName' => CONFIGSET,
       ]);
       $messageId = $result->get('MessageId');
       echo("Email sent! Message ID: $messageId"."\n");
      
       } catch (SesException $error) {
       echo("The email was not sent. Error message: ".$error-
       >getAwsErrorMessage()."\n");
       }
      
       ?>
      

1 个答案:

答案 0 :(得分:0)

首先,您不要手动创建〜/ .aws / credentials。让AWS CLI为您完成此任务。

安装CLI:pip install awscli --upgrade

删除您创建的凭据文件。

配置CLI和凭据:aws configure

您的凭据现已正确设置。

您收到的错误消息是由尝试从EC2实例元数据获取凭据的PHP SDK引起的。由于您尚未为此系统配置IAM角色,因此失败。这也意味着凭证未正确存储在您的个人资料中(〜/ .aws / credentials)。

注意:更好的方法是不在EC2实例上存储凭据。而是创建IAM角色并将该角色分配给EC2实例。所有AWS软件开发人员都知道如何从实例元数据中提取凭据。

IAM Roles for Amazon EC2