0

I installed the AWS PHP SDK using composer but am unsure how to use AWS SES within my existing class.

I used the sample code in the SES PHP Docs website. I was able to create a test.php file to test out that code and it works. However when trying to use SES within my class I get error 500 within my page.

Ive tried including the autoload file in the Class file.

<?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";
       }
   }
}

What I expected was to see the the confirmation message echo out and receive the email. However this is not what I get. I dont think I am properly including the AWS library into my class.

Sebastian Flores
  • 101
  • 2
  • 12
  • 1
    What *do* you get? – ceejayoz Jan 28 '19 at 20:43
  • I get nothing when using it within my class. When I call the function I get an error 500 in browser. However if I dont use it within a class and just call the php file through the terminal I get a successful email send. – Sebastian Flores Jan 28 '19 at 20:45
  • 1
    If "nothing" means a blank page, you should check the error logs or adjust your error reporting settings. 500 + a blank page means you've got a useful error message sitting around somewhere. – ceejayoz Jan 28 '19 at 20:46
  • (If you're using Composer, I'd bet that error is that you're using `use vendor\...`. The `vendor` part should usually be left off. – ceejayoz Jan 28 '19 at 20:47
  • I am using composer. Ill give that a try – Sebastian Flores Jan 28 '19 at 20:51
  • Checked logs and found this message PHP Fatal error: Uncaught Error: Class 'vendor\\Aws\\Ses\\SesClient' not found – Sebastian Flores Jan 28 '19 at 20:54
  • Yup, so that's what I thought. Take off the `vendor` bits in your `use` statements. – ceejayoz Jan 28 '19 at 20:59
  • Yep that got rid of that error. Now trying to figure out this error Uncaught Aws\Exception\CredentialsException: Cannot read credentials from /.aws/credentials that file is definitely in that directory – Sebastian Flores Jan 28 '19 at 21:04
  • I hardcoded my keys and it worked, not sure why using the ~/.aws/credentials file did not work – Sebastian Flores Jan 28 '19 at 21:10
  • Chances are your webserver is running as something like a `www-data` user rather than your main user, which'll mean *your* `~` is not *its* `~`. – ceejayoz Jan 28 '19 at 21:16
  • I changed permissions to that directory to be owned by www-data recursively but didnt do anything – Sebastian Flores Jan 28 '19 at 21:24
  • That's not what I'm saying. I'm saying your credentials file may be expected to be somewhere like `/home/www-data/.aws/credentials`. – ceejayoz Jan 28 '19 at 21:27

1 Answers1

0

try this code:

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

use Aws\Ses\SesClient;
use Aws\Exception\AwsException;
Bram Verstraten
  • 1,414
  • 11
  • 24