'How to send a simple email in AWS Pinpoint using the PHP SDK?
The official AWS PHP SDK Pinpoint documentation is so dense that even sending a simple email seems like a daunting task :)
https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-pinpoint-2016-12-01.html#sendmessages
$result = $client->sendMessages([
'ApplicationId' => '<string>', // REQUIRED
'MessageRequest' => [ // REQUIRED
'Addresses' => [
'<__string>' => [
'BodyOverride' => '<string>',
'ChannelType' => 'GCM|APNS|APNS_SANDBOX|APNS_VOIP|APNS_VOIP_SANDBOX|ADM|SMS|VOICE|EMAIL|BAIDU|CUSTOM',
'Context' => ['<string>', ...],
'RawContent' => '<string>',
'Substitutions' => [
'<__string>' => ['<string>', ...],
// ...
],
'TitleOverride' => '<string>',
],
// ...
],
'Context' => ['<string>', ...],
'Endpoints' => [
'<__string>' => [
'BodyOverride' => '<string>',
'Context' => ['<string>', ...],
'RawContent' => '<string>',
'Substitutions' => [
'<__string>' => ['<string>', ...],
// ...
],
'TitleOverride' => '<string>',
],
// ...
],
'MessageConfiguration' => [ // REQUIRED
'EmailMessage' => [
'Body' => '<string>',
'FeedbackForwardingAddress' => '<string>',
'FromAddress' => '<string>',
'RawEmail' => [
'Data' => <string || resource || Psr\Http\Message\StreamInterface>,
],
'ReplyToAddresses' => ['<string>', ...],
'SimpleEmail' => [
'HtmlPart' => [
'Charset' => '<string>',
'Data' => '<string>',
],
'Subject' => [
'Charset' => '<string>',
'Data' => '<string>',
],
'TextPart' => [
'Charset' => '<string>',
'Data' => '<string>',
],
],
'Substitutions' => [
'<__string>' => ['<string>', ...],
// ...
],
],
],
'TemplateConfiguration' => [
'EmailTemplate' => [
'Name' => '<string>',
'Version' => '<string>',
],
'PushTemplate' => [
'Name' => '<string>',
'Version' => '<string>',
],
'SMSTemplate' => [
'Name' => '<string>',
'Version' => '<string>',
],
'VoiceTemplate' => [
'Name' => '<string>',
'Version' => '<string>',
],
],
'TraceId' => '<string>',
],
]);
Does someone have a working code snippet just for sending a simple email using the PHP SDK v3?
Solution 1:[1]
The following php script demonstrates how you can use AWS PHP SDK v3 to send emails using Amazon Pinpoint service using the sendMessage API.
Feel free to fork it from gists and customize to fit your need.
<?php
require 'vendor/autoload.php';
use Aws\Pinpoint\PinpointClient;
use Aws\Exception\AwsException;
/**
* @author syumaK(Amos Syuma)
* Date: March 24, 2020
* Description: A php script that uses AWS PHP SDK for Pinpoint service to send an email message.
*
*/
/**
* This code expects that you have AWS credentials set up per:
* https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/guide_credentials.html
*/
// Instantiate a client with the credentials from the credential profiles (.aws/credentials) file.
$settings = (array(
'profile' => 'syumaK',
'region' => 'us-east-1',
'version' => 'latest',
));
$pinpointClient = new Aws\Pinpoint\PinpointClient($settings);
# The "From" address. This address has to be verified in Amazon Pinpoint in the region you're using to send email.
$SENDER = "redacted";
# The addresses on the "To" line. If your Amazon Pinpoint account is in the sandbox, these addresses also have to be verified.
$TOADDRESS = "redacted";
try {
$result = $pinpointClient->sendMessages([
'ApplicationId' => '4fd13xxxxxxxxx', // REQUIRED
'MessageRequest' => [ // REQUIRED
'Addresses' => [
$TOADDRESS => [
'ChannelType' => 'EMAIL',
],
],
'MessageConfiguration' => [ // REQUIRED
'EmailMessage' => [
'FromAddress' => $SENDER,
],
],
'TemplateConfiguration' => [ // REQUIRED
'EmailTemplate' => [
'Name' => 'One',
'Version' => '1',
],
],
],
]);
print $result;
} catch (AwsException $e){
// output error message if fails
error_log($e->getMessage());
}
?>
Tested the above code snippet using the following environment spec:
Ubuntu: 18.04
Apache2: 2.4.18
aws-sdk-php": "^3.108"
Hope this helps!
Solution 2:[2]
Based on syumaK answer, I finally got it working with this snippet:
<?php
require 'vendor/autoload.php';
use Aws\Pinpoint\PinpointClient;
use Aws\Exception\AwsException;
/**
* @author syumaK(Amos Syuma)
* Date: March 24, 2020
* Description: A php script that uses AWS PHP SDK for Pinpoint service to send an email message.
*
*/
/**
* This code expects that you have AWS credentials set up per:
* https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/guide_credentials.html
*/
// Instantiate a client with the credentials from the credential profiles (.aws/credentials) file.
$settings = (array(
'profile' => 'syumaK',
'region' => 'us-east-1',
'version' => 'latest',
));
$pinpointClient = new Aws\Pinpoint\PinpointClient($settings);
# The "From" address. This address has to be verified in Amazon Pinpoint in the region you're using to send email.
$SENDER = "redacted";
# The addresses on the "To" line. If your Amazon Pinpoint account is in the sandbox, these addresses also have to be verified.
$TOADDRESS = "redacted";
try {
$result = $pinpointClient->sendMessages([
'ApplicationId' => 'REPLACE WITH APP ID (NOT NAME)',
'MessageRequest' => [ // REQUIRED
'Addresses' => [
$TOADDRESS => [
'ChannelType' => 'EMAIL',
],
],
'MessageConfiguration' => [ // REQUIRED
'EmailMessage' => [
'FromAddress' => $SENDER,
'SimpleEmail' => [
'HtmlPart' => [
'Charset' => 'utf-8',
'Data' => 'my sample html',
],
'Subject' => [
'Charset' => 'utf-8',
'Data' => 'my sample subject',
],
'TextPart' => [
'Charset' => 'utf-8',
'Data' => 'my sample text',
]
]
],
],
]
]);
print $result;
} catch (AwsException $e){
// output error message if fails
error_log($e->getMessage());
}
?>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | syumaK |
| Solution 2 | antoniotajuelo |
