Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

sauravs012's avatar

AWS PHP SDK email attachment Issue

I'm using below code to send email with SES using AWS-PHP-SDK. But I don't know how we can pass an attachment to sendEmail function of SES.


// If necessary, modify the path in the require statement below to refer to the 
// location of your Composer autoload.php file.
require 'vendor/autoload.php';

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

// 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 [email protected] with your "From" address.
// This address must be verified with Amazon SES.
$sender_email = '[email protected]';

// 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 = ['[email protected]','[email protected]'];

// 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";
}
0 likes
2 replies
LaryAI's avatar
Level 58

To add an attachment to the email, you can use the createMimeMessage() method of the Aws\Ses\Message class to create a MIME message with the attachment, and then pass the resulting message to the sendRawEmail() method of the Aws\Ses\SesClient class.

Here's an example of how to modify the code to add an attachment:

// If necessary, modify the path in the require statement below to refer to the 
// location of your Composer autoload.php file.
require 'vendor/autoload.php';

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

// 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 [email protected] with your "From" address.
// This address must be verified with Amazon SES.
$sender_email = '[email protected]';

// 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 = ['[email protected]','[email protected]'];

// 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';

// Create a message with an attachment
$message = new Message([
    'Subject' => [
        'Data' => $subject,
        'Charset' => $char_set,
    ],
    'Body' => [
        'Html' => [
            'Data' => $html_body,
            'Charset' => $char_set,
        ],
        'Text' => [
            'Data' => $plaintext_body,
            'Charset' => $char_set,
        ],
    ],
]);
$message->addAttachment('path/to/attachment', 'filename.pdf', 'application/pdf');

try {
    // Convert the message to a raw email
    $raw_email = $message->createMimeMessage()->getRawMessage();

    // Send the raw email
    $result = $SesClient->sendRawEmail([
        'Source' => $sender_email,
        'Destinations' => $recipient_emails,
        'RawMessage' => [
            'Data' => $raw_email,
        ],
        // 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";
}

In this example, the addAttachment() method is used to add an attachment to the message. The first argument is the path to the file, the second argument is the filename that will be used in the email, and the third argument is the MIME type of the file.

The createMimeMessage() method is then called to create a MIME message with the attachment, and the getRawMessage() method is called to get the raw email data.

Finally, the sendRawEmail() method is used to send the raw email data. The Source and Destinations parameters are used to specify the sender and recipients of the email, and the RawMessage parameter is used to specify the raw email data.

sauravs012's avatar

I'm using latest verion of PHP-AWS-SDK I use your code. getting below error

<b>Fatal error</b>:  Class 'Aws\Ses\Message' not found

Please or to participate in this conversation.