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 V.2
<?php
/** example.com/members/mod/amazon/amazon-test.php */
include_once(dirname(__FILE__) . '/functions/amazonSes.php');
include_once(dirname(__FILE__) . '/functions/test-function.php');
$from = 'noreply@my-site.com';
$to_address = 'test@gexample.com';
$subject = "My Test Subject";
$textMSG = "Dear Text Friend,\r\nThis email was send with Amazon SES using the AWS SDK for PHP";
$textMSG .= test();
/** ======= Amazon SES ====== */
$xx = mailAmazonSES($to_address,$from,$subject,$textMSG);
echo $xx."<br>";
$textMSG .= $xx.' ++mail';
echo $textMSG."<br>";
/** ======= mail php ====== */
$headers = "From: $from";
mail($to_address,$subject,$textMSG,$headers);
?>
<?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' => [
/*'Html' => [
'Charset' => $charset,
'Data' => $htmlMSG,
],*/
'Text' => [
'Charset' => $charset,
'Data' => $textMSG,
],
],
'Subject' => [
'Charset' => $charset,
'Data' => $subject,
],
],
'Source' => $from,
]);
$messageId = $result->get('MessageId');
return " === Email sent! Message ID: $messageId";
} catch (SesException $error) {
return " === The email was not sent. Error message: ".$error->getAwsErrorMessage();
}
}
<?php
/** example.com/members/mod/amazon/start.php */
//include_once(dirname(__FILE__) . '/functions/amazonSes.php');
//include_once(dirname(__FILE__) . '/functions/test-function.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'];
/** ============== temporary test =============== */
include_once(dirname(__FILE__) . '/functions/test-function.php');
$message .= test();
/** ==========AMAZON SES ====================== */
require_once(dirname(__FILE__) . '/functions/amazonSes.php');
$xx = mailAmazonSES($to_address,$from,$subject,$message);
if(empty($xx)){
$yy = ' empty';
}else{
$yy = $xx;
}
$message .= $yy;
/** ========== mail PHP ====================== */
$headers = "From: $from";
mail($to_address,$subject,$message,$headers);
return true;
});
}
<?php
/** example.com/members/mod/amazon_ses/function/test-function.php */
function test(){
return ' === test===';
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment