Как использовать Amazon SES в классе PHP - PullRequest
0 голосов
/ 28 января 2019

Я установил AWS PHP SDK с помощью composer, но не уверен, как использовать AWS SES в моем существующем классе.

Я использовал пример кода на веб-сайте SES PHP Docs.Мне удалось создать файл test.php, чтобы проверить этот код, и он работает.Однако при попытке использовать SES в моем классе на моей странице появляется ошибка 500.

Я попытался включить файл автозагрузки в файл класса.

<?php

namespace Main\messaging;
use vendor\Aws\Ses\SesClient;
use vendor\Aws\Exception\AwsException;
class Email
{
   public function sendEmail(){
       // Create an SesClient. Change the value of the region parameter if you're
// using an AWS Region other than US West (Oregon). Change the value of the
// profile parameter if you want to use a profile in your credentials file
// other than the default.
       $SesClient = new SesClient([
           'profile' => 'default',
           'version' => '2010-12-01',
           'region'  => 'us-west-2'
       ]);

// Replace sender@example.com with your "From" address.
// This address must be verified with Amazon SES.
       $sender_email = 'welcome@myemail.com';

// Replace these sample addresses with the addresses of your recipients. If
// your account is still in the sandbox, these addresses must be verified.
       $recipient_emails = ['test@email.com'];

// Specify a configuration set. If you do not want to use a configuration
// set, comment the following variable, and the
// 'ConfigurationSetName' => $configuration_set argument below.
//$configuration_set = 'ConfigSet';

       $subject = 'Amazon SES test (AWS SDK for PHP)';
       $plaintext_body = 'This email was sent with Amazon SES using the AWS SDK for PHP.' ;
       $html_body =  '<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>';
       $char_set = 'UTF-8';

       try {
           $result = $SesClient->sendEmail([
               'Destination' => [
                   'ToAddresses' => $recipient_emails,
               ],
               'ReplyToAddresses' => [$sender_email],
               'Source' => $sender_email,
               'Message' => [
                   'Body' => [
                       'Html' => [
                           'Charset' => $char_set,
                           'Data' => $html_body,
                       ],
                       'Text' => [
                           'Charset' => $char_set,
                           'Data' => $plaintext_body,
                       ],
                   ],
                   'Subject' => [
                       'Charset' => $char_set,
                       'Data' => $subject,
                   ],
               ],
               // If you aren't using a configuration set, comment or delete the
               // following line
               //'ConfigurationSetName' => $configuration_set,
           ]);
           $messageId = $result['MessageId'];
           echo("Email sent! Message ID: $messageId"."\n");
       } catch (AwsException $e) {
           // output error message if fails
           echo $e->getMessage();
           echo("The email was not sent. Error message: ".$e->getAwsErrorMessage()."\n");
           echo "\n";
       }
   }
}

Я ожидал увидетьПодтверждение сообщения эхо и получить электронное письмо.Однако это не то, что я получаю.Я не думаю, что правильно включил библиотеку AWS в свой класс.

1 Ответ

0 голосов
/ 25 июня 2019

попробуйте этот код:

<?php
require 'vendor/autoload.php';

use Aws\Ses\SesClient;
use Aws\Exception\AwsException;
...