<?php
namespace AppBundle\Helpers;
use Aws\Sdk;
use Aws\Ses\Exception\SesException;
use Aws\Ses\SesClient;
use Psr\Log\LoggerInterface;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
class MailerHelper
{
const CHARSET = 'UTF-8';
public function sendEmail(
$toAddresses,
$fromAddress,
$subject,
$body,
$key = 'AKIAIPF2PPEPUDSB64IA',
$secret = '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'
) {
$to = [];
foreach ($toAddresses as $address) {
if (strpos($address, '<') > 0) {
$arr = explode('<',$address );
$address = $arr[1];
$address = str_replace([' ','>'], '', $address);
}
$to[] = $address;
}
$sdk = new Sdk();
$messageId = null;
try {
$result = $sdk->createSes([
'region' => 'us-east-1',
'version' => 'latest',
'credentials' => [
'key' => $key,
'secret' => $secret,
],
])->sendEmail([
'Destination' => [
'ToAddresses' => $to,
],
'Source' => $fromAddress,
'Message' => [
'Subject' => [
'Charset' => self::CHARSET,
'Data' => $subject,
],
'Body' => [
'Html' => [
'Charset' => self::CHARSET,
'Data' => $body,
],
'HTML' => [
'Charset' => self::CHARSET,
'Data' => $body,
],
],
],
]);
$messageId = $result->get('MessageId');
} catch (SesException $e) {
return(sprintf('Email not sent. [%s]', $e->getAwsErrorMessage()));
}
return $messageId;
}
function getEmails($type='UNSEEN', $mark = false) {
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = 'supportdesk@artefactsys.com';
$password = 'FOxP%87g3Q9P9jGS$q';
$inbox = imap_open($hostname,$username,$password);
$eArr = [];
$emails = imap_search($inbox,$type); // ALL/UNSEEN/FLAGGED
if (!!$emails) {
foreach ($emails as $email_number) {
$overview = imap_fetch_overview($inbox, $email_number, 0);
$message = imap_fetchbody($inbox, $email_number, 2);
$output = [];
$output['seen'] = $overview[0]->seen ? 'read' : 'unread';
$output['subject'] = $overview[0]->subject;
$output['from'] = $overview[0]->from;
$output['date'] = $overview[0]->date;
$output['body'] = $message;
$output['to'] = $username;
$eArr[] = $output;
if ($mark) {
imap_setflag_full($inbox, $email_number, "\\Seen \\Flagged", ST_UID);
}
}
}
imap_close($inbox);
return $eArr;
}
public function sendEmailwithAttachment(
$recipient,
$sender,
$sendername,
$cc,
$cc2,
$subject,
$htmlbody,
$textbody,
$att,
$key = 'AKIAIPF2PPEPUDSB64IA',
$secret = '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'
) {
$configset = 'ConfigSet';
// Create an SesClient.
$client = new SesClient([
'region' => 'us-east-1',
'version' => 'latest',
'credentials' => [
'key' => $key,
'secret' => $secret,
],
]);
// Create a new PHPMailer object.
$mail = new PHPMailer;
// Add components to the email.
$mail->setFrom($sender, $sendername);
$mail->addAddress($recipient);
$mail->Subject = $subject;
$mail->Body = $htmlbody;
$mail->AltBody = $textbody;
$mail->addAttachment($att);
//$mail->addCustomHeader('X-SES-CONFIGURATION-SET', $configset);
if ($cc) $mail->AddCC($cc);
if ($cc2) $mail->AddCC($cc2);
// Attempt to assemble the above components into a MIME message.
if (!$mail->preSend()) {
echo $mail->ErrorInfo;
} else {
// Create a new variable that contains the MIME message.
$message = $mail->getSentMIMEMessage();
}
// Try to send the message.
try {
$result = $client->sendRawEmail([
'RawMessage' => [
'Data' => $message
]
]);
// If the message was sent, show the message ID.
$messageId = $result->get('MessageId');
echo("Email sent! Message ID: $messageId" . "\n");
} catch (SesException $error) {
// If the message was not sent, show a message explaining what went wrong.
echo("The email was not sent. Error message: "
. $error->getAwsErrorMessage() . "\n");
}
}
}