Skip to content

Instantly share code, notes, and snippets.

@alecs
Created January 8, 2026 15:33
Show Gist options
  • Select an option

  • Save alecs/1337b93b0b1a7ad79fb11557ccea2698 to your computer and use it in GitHub Desktop.

Select an option

Save alecs/1337b93b0b1a7ad79fb11557ccea2698 to your computer and use it in GitHub Desktop.
symfony v6 aws ses mailing
<?php
// test-ses.php
// Require Composer's autoloader
require_once __DIR__ . '/vendor/autoload.php';
// Import AWS SES classes
use AsyncAws\Ses\SesClient;
use AsyncAws\Ses\Input\SendEmailRequest;
use AsyncAws\Ses\ValueObject\Destination;
use AsyncAws\Ses\ValueObject\EmailContent;
use AsyncAws\Ses\ValueObject\Message;
use AsyncAws\Ses\ValueObject\Content;
use AsyncAws\Ses\ValueObject\Body;
// Load environment variables from .env files
if (class_exists('Symfony\Component\Dotenv\Dotenv')) {
$dotenv = new \Symfony\Component\Dotenv\Dotenv();
if (file_exists(__DIR__ . '/.env')) {
$dotenv->load(__DIR__ . '/.env');
}
if (file_exists(__DIR__ . '/.env.local')) {
$dotenv->load(__DIR__ . '/.env.local');
}
}
// Helper function to get environment variable from multiple sources
function getEnvVar($name, $default = null) {
// Check in this priority: shell env, $_ENV, $_SERVER, .env loaded values
return getenv($name) ?: $_ENV[$name] ?? $_SERVER[$name] ?? $default;
}
// Get AWS credentials from environment variables with fallbacks
$awsRegion = getEnvVar('AWS_REGION', 'eu-central-1');
$awsAccessKey = getEnvVar('AWS_ACCESS_KEY_ID');
$awsSecretKey = getEnvVar('AWS_SECRET_ACCESS_KEY');
// Print the credentials we're about to use (for debugging purposes)
echo "Using the following AWS configuration:\n";
echo "Region: $awsRegion\n";
// Don't print full access keys for security reasons, just first few characters
echo "Access Key ID: " . (substr($awsAccessKey, 0, 4) . (strlen($awsAccessKey) > 4 ? '...' : '')) . "\n";
echo "Secret Access Key: " . (substr($awsSecretKey, 0, 4) . (strlen($awsSecretKey) > 4 ? '...' : '')) . "\n";
echo "------------------------------\n";
// Create an AsyncAws SES Client with explicit credentials
$sesClient = new SesClient([
'region' => $awsRegion,
'accessKeyId' => $awsAccessKey,
'accessKeySecret' => $awsSecretKey,
]);
// Email details
$fromEmail = 'no-reply@domain.io'; // Change to your verified sender
$toEmail = 'xxx@domain.io'; // Change to your recipient
try {
// Send the email
$result = $sesClient->sendEmail(new SendEmailRequest([
'FromEmailAddress' => $fromEmail,
'Destination' => new Destination([
'ToAddresses' => [$toEmail],
]),
'Content' => new EmailContent([
'Simple' => new Message([
'Subject' => new Content([
'Data' => 'Test Email from AWS SES via AsyncAws',
]),
'Body' => new Body([
'Text' => new Content([
'Data' => 'This is a test email sent directly using AsyncAws SES client.',
]),
'Html' => new Content([
'Data' => '<p>This is a test email sent directly using <strong>AsyncAws SES</strong> client.</p>',
]),
]),
]),
]),
]));
// Wait for the result
$result->resolve();
echo "Success! Email sent to $toEmail via AWS SES.\n";
} catch (\Exception $e) {
echo "Error: " . $e->getMessage() . "\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment