Skip to content

Instantly share code, notes, and snippets.

@kangks
Last active October 31, 2023 14:01
Show Gist options
  • Save kangks/eda238bdee360070244ff9e53fc71312 to your computer and use it in GitHub Desktop.
Save kangks/eda238bdee360070244ff9e53fc71312 to your computer and use it in GitHub Desktop.
AWS PinpointEmailClient with SDK for PHP
  1. Download as zip with command curl -LJO https://gist.github.com/kangks/eda238bdee360070244ff9e53fc71312/archive/7ae513002e69388b438dd2b3e6994b9d2abbb801.zip
  2. Extract and run docker-compose up from the folder
  3. For first run, wait for the composer to finish download all dependencies
  4. Open a browser and enter http://localhost/pinpoint.php as the url
{
"require": {
"aws/aws-sdk-php": "^3.209"
},
"autoload": {
"psr-0": {
"": "*"
},
"psr-4": {
"Aws\\Resource\\": "src/"
}
}
}
version: "3.7"
services:
php:
image: arm64v8/php:8.1.25-apache-bullseye
ports:
- 80:80
volumes:
- ./aws_credentials:/.aws
- .:/var/www/html
composer:
image: composer:1.9
command: ["composer", "install"]
volumes:
- .:/app
<?php
require 'vendor/autoload.php';
use Aws\Exception\AwsException;
use Aws\PinpointEmail\PinpointEmailClient;
$settings = (array(
'profile' => 'pinpoint',
'region' => 'us-east-1',
'version' => '2018-07-26',
));
$pinpointClient = new Aws\PinpointEmail\PinpointEmailClient($settings);
?>
<?php
# The "From" address. This address has to be verified in Amazon Pinpoint in the region you're using to send email.
$SENDER = "sender@maildrop.cc";
# The addresses on the "To" line. If your Amazon Pinpoint account is in the sandbox, these addresses also have to be verified.
$TOADDRESS = "receiver@maildrop.cc";
?>
<form method="post">
From: <input type="text" name="SENDER" value="<?php echo $SENDER;?>">
To: <input type="text" name="TOADDRESS" value="<?php echo $TOADDRESS;?>">
<input type="radio" name="content" value="simple">Simple
<input type="radio" name="content" value="template">Template
<input type="submit" name="send" value="Send Email"/>
</form>
<br/>
<br/>
<?php
// https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-pinpoint-email-2018-07-26.html#sendemail
if(isset($_POST['send'])) {
$selected_content = $_POST['content'];
$content = '';
if ($selected_content == 'simple') {
$content = array(
'Simple' => [
'Body' => [ // REQUIRED
'Html' => [
'Charset' => 'utf8',
'Data' => 'click <a href="https://doit.com/">here</a>', // REQUIRED
]
],
'Subject' => [ // REQUIRED
'Charset' => 'utf8',
'Data' => 'from Pinpoint', // REQUIRED
],
]);
} else if ($selected_content == 'template') {
$content = array(
'Template' => [
'TemplateArn' => 'arn:aws:mobiletargeting:<AWS Region>:<AWS Account ID>:templates/template/EMAIL',
'TemplateData' => json_encode (new stdClass)
]
);
};
try {
$result = $pinpointClient->sendEmail([
'Content' =>
$content,
'Destination' => [
'ToAddresses' => [$TOADDRESS],
],
'FromEmailAddress' => $SENDER
]);
print $result;
} catch (AwsException $e){
error_log($e->getMessage());
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment