PHP AWS SES Library

repository·master·Indexed 43 days ago

https://github.com/daniel-zahariev/php-aws-ses

A PHP library providing a REST-based interface to Amazon Simple Email Service (SES). It enables developers to send emails, manage recipients, handle attachments, and manage verified email addresses. Features include support for AWS Signature Version 4, bulk sending mode for performance, and helper methods for tracking account quotas and statistics.

Tokens
6.3K
Snippets
30
Records
31
Agent score
53%

What's inside php-aws-ses

  1. Enable Bulk Sending mode for performance

    master

    When sending hundreds of emails, enable setBulkMode(true) on the SimpleEmailService instance. This reuses the CURL handler to reduce SSL handshakes and improve performance.

    <?php
    
    // Enable bulk sending mode
    $ses->setBulkMode(true);
    
    foreach($messages as $message) {
        $ses->sendEmail($message);
    }
    
    // Disable bulk sending mode
    $ses->setBulkMode(false);
  2. Send a basic email with SimpleEmailService

    master

    To send a simple email, instantiate SimpleEmailServiceMessage to define the content and SimpleEmailService to handle the transmission using your AWS credentials. The sendEmail method returns an array containing the MessageId and RequestId on success.

    <?php
    
    require_once 'vendor/autoload.php';
    
    $m = new SimpleEmailServiceMessage();
    $m->addTo('Recipient Name <recipient@example.com>');
    $m->setFrom('Sender <user@example.com>');
    $m->setSubject('Hello, world!');
    $m->setMessageFromString('This is the message body.');
    
    $ses = new SimpleEmailService('AccessKey', 'SecretKey');
    print_r($ses->sendEmail($m));
  3. Initialize the SimpleEmailService client

    master

    To use the library, instantiate the SimpleEmailService class. You must provide your AWS Access Key and Secret Key. You can also specify the AWS SES host (region) and whether to trigger PHP errors when the API returns an error.

    By default, the client uses AWS_US_EAST_1 and the REQUEST_SIGNATURE_V4 signature version.

    $accessKey = 'YOUR_ACCESS_KEY';
    $secretKey = 'YOUR_SECRET_KEY';
    
    // Initialize with default US East 1 region
    $ses = new SimpleEmailService($accessKey, $secretKey);
    
    // Or initialize with a specific region and error handling preference
    $ses = new SimpleEmailService($accessKey, $secretKey, SimpleEmailService::AWS_EU_WEST_1, true);
  4. Send a basic email

    master

    To send an email, create a SimpleEmailServiceMessage object, configure its properties (recipient, sender, subject, and body), and pass it to the sendEmail method of your SimpleEmailService instance.

    $m = new SimpleEmailServiceMessage();
    $m->addTo('recipient@example.com');
    $m->setFrom('user@example.com');
    $m->setSubject('Hello, world!');
    $m->setMessageFromString('This is the message body.');
    
    print_r($ses->sendEmail($m));
  5. Add attachments to emails

    master

    To send attachments, use addAttachmentFromData or addAttachmentFromFile. When attachments are present, the library uses the SendRawEmail API. You can explicitly enforce raw requests by passing true as the second argument to sendEmail. You can also add inline attachments (e.g., for images in HTML) by specifying the 'inline' type.

    <?php
    
    // Attach from data or file
    $m->addAttachmentFromData('my_text_file.txt', 'Simple content', 'text/plain');
    $m->addAttachmentFromFile('my_PFD_file.pdf', '/path/to/pdf/file', 'application/pdf');
    
    // Enforce raw request if necessary
    $ses->sendEmail($m, true);
    
    // Add an inline file (e.g., for HTML <img> tags)
    // Usage: <img src='cid:logo.png' />
    $m->addAttachmentFromFile('logo.png','path/to/logo.png','application/octet-stream', '<logo.png>' , 'inline');
  6. Set email message body and charset

    master

    The message body can be set using strings, files, or URLs. You can provide both Text and HTML versions simultaneously. Note that setMessageFromString, setMessageFromFile, and setMessageFromURL are mutually exclusive; the last call made will be the one used. You can also specify the charset for the subject and body (defaults to UTF-8).

    <?php
    
    // Set content via string, file, or URL
    $m->setMessageFromString('Text content');
    $m->setMessageFromFile('/path/to/some/file.txt');
    $m->setMessageFromURL('http://example.com/somefile.txt');
    
    // Set both Text and HTML versions
    $m->setMessageFromString($text, $html);
    $m->setMessageFromFile($textfilepath, $htmlfilepath);
    $m->setMessageFromURL($texturl, $htmlurl);
    
    // Set encoding (defaults to UTF-8)
    $m->setSubjectCharset('ISO-8859-1');
    $m->setMessageCharset('ISO-8859-1');
  7. Use SES Helper Methods

    master

    The SimpleEmailService class provides helper methods to manage verified email addresses and check account quotas/statistics.

    <?php
    
    // Email Verification
    $ses->listVerifiedEmailAddresses();
    $ses->deleteVerifiedEmailAddress('user@example.com');
    $ses->verifyEmailAddress('user@example.com');
    
    // Quotas and Statistics
    $ses->getSendQuota();
    $ses->getSendStatistics();
  8. Manage email recipients

    master

    You can add recipients to several lists: To, CC, BCC, and Reply-To. Methods like addTo, addCC, addBCC, and addReplyTo accept either a single string (optionally including a name like 'Jim Carrey <jim@example.com>') or an array of strings.

    <?php
    
    $m = new SimpleEmailServiceMessage();
    
    // Add many Recipients
    $m->addTo(array('dwight@example.com', 'angela@example.com'));
    
    // Add CC
    $m->addCC('holly@example.com');
    $m->addCC(array('kelly@example.com', 'ryan@example.com'));
    
    // Add BCC
    $m->addBCC('michael@example.com');
    $m->addBCC(array('kevin@example.com', 'oscar@example.com'));
    
    // Add Reply-To
    $m->addReplyTo('andy@example.com');
    $m->addReplyTo(array('stanley@example.com', 'erin@example.com'));
    
    // Add with names
    $m->addTo('Jim Carrey <jim@example.com>');
  9. Configure Configuration Sets and Message Tags

    master

    You can associate messages with an AWS SES Configuration Set or attach custom metadata using Message Tags.

    <?php
    
    // Configuration Sets
    $m->setConfigurationSet('myConfigurationSet');
    $m->setConfigurationSet(null); // Reset
    
    // Message Tags
    $m->setMessageTag('key', 'value');
    $tag = $m->getMessageTag('key');
    $m->removeMessageTag('key');
    
    // Bulk Message Tags (merges with current tags)
    $m->setMessageTags(array('key1' => 'value1', 'key2' => 'value2'));
    $tags = $m->getMessageTags();
    $m->removeMessageTags();
  10. Verify and manage email addresses

    master

    Before sending emails, you must verify email addresses through AWS SES. You can request verification for an address, list all currently verified addresses, or remove an address from the verified list.

    Note: Every request returns a RequestId which can be used for troubleshooting with AWS.

    // Request verification for an email address
    print_r($ses->verifyEmailAddress('user@example.com'));
    
    // List all verified email addresses
    print_r($ses->listVerifiedEmailAddresses());
    
    // Remove an address from the verified list
    $ses->deleteVerifiedEmailAddress('user@example.com');