Skip to content

Instantly share code, notes, and snippets.

@reuvenz
Last active Jul 21, 2018
Embed
What would you like to do?
Elgg-AmazonSES, Integration
<?php
/** example.com/members/mod/amazon/amazon-test.php */
include_once(dirname(__FILE__) . '/functions/amazonSes.php');
$from = 'noreply@example.com';
$to_address = 'test@example.com';
$subject = "My Test Subject";
$textMSG = "Dear Text Friend,\r\nThis email was send with Amazon SES using the AWS SDK for PHP";
mailAmazonSES($to_address,$from,$subject,$textMSG);
?>
<?php
/** example.com/members/mod/amazon/functions/amazonSes.php */
use Aws\Ses\SesClient;
use Aws\Ses\Exception\SesException;
function mailAmazonSES($to,$from,$subject,$textMSG) {
include_once './Amazon-AWS-SDK/aws-autoloader.php';
$client = SesClient::factory(array(
'version'=> 'latest',
'region' => 'us-east-1'
));
$charset = 'UTF-8';
try {
$result = $client->sendEmail([
'Destination' => [
'ToAddresses' => [
$to,
],
],
'Message' => [
'Body' => [
'Text' => [
'Charset' => $charset,
'Data' => $textMSG,
],
],
'Subject' => [
'Charset' => $charset,
'Data' => $subject,
],
],
'Source' => $from,
]);
$messageId = $result->get('MessageId');
//echo("Email sent! Message ID: $messageId"."\n");
} catch (SesException $error) {
//echo("The email was not sent. Error message: ".$error->getAwsErrorMessage()."\n");
}
}
<?php
/** example.com/members/mod/amazon/start.php */
elgg_register_event_handler('init', 'system', 'amazon_ses_init');
function amazon_ses_init() {
elgg_register_plugin_hook_handler('email', 'system', function($hook, $type, $returnvalue, $params) {
if ($returnvalue === true) {
return;
}
$to_address = '';
$contact = $returnvalue['to'];
$containsName = preg_match('/<(.*)>/', $contact, $matches) == 1;
if ($containsName) {
$to_address = $matches[1];
} else {
$to_address = $contact;
}
if (!$to_address) {
return $returnvalue;
}
$users = get_user_by_email($to_address);
$recipient = $users[0];
if (!($recipient instanceof ElggUser)) {
return $returnvalue;
}
$message = $returnvalue['body'];
$subject = $returnvalue['subject'];
$from = $returnvalue['from'];
/** ==========AMAZON SES Comment/Uncomment ====================== */
include_once(dirname(__FILE__) . '/functions/amazonSes.php');
mailAmazonSES($to_address,$from,$subject,$message);
/** ==========mail PHP Uncomment/Comment ====================== */
/*$headers = "From: $from";
mail($to_address,$subject,$message,$headers);*/
return true;
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment