src/AppBundle/Helpers/MailerHelper.php line 52

Open in your IDE?
  1. <?php
  2. namespace AppBundle\Helpers;
  3. use Aws\Sdk;
  4. use Aws\Ses\Exception\SesException;
  5. use Aws\Ses\SesClient;
  6. use Psr\Log\LoggerInterface;
  7. use PHPMailer\PHPMailer\PHPMailer;
  8. use PHPMailer\PHPMailer\Exception;
  9. class MailerHelper
  10. {
  11.     const CHARSET 'UTF-8';
  12.     public function sendEmail(
  13.         $toAddresses,
  14.         $fromAddress,
  15.         $subject,
  16.         $body,
  17.         $key 'AKIAIPF2PPEPUDSB64IA',
  18.         $secret 'lzvnKnTUio04OfRoFxVa0hhRUcP3McXIhYDDGJvX'
  19.     ) {
  20.             $to = [];
  21.             foreach ($toAddresses as $address) {
  22.                 if (strpos($address'<') > 0) {
  23.                     $arr explode('<',$address );
  24.                     $address $arr[1];
  25.                     $address str_replace([' ','>'], ''$address);
  26.                 }
  27.                 $to[] = $address;
  28.             }
  29.         $sdk = new Sdk();
  30.         $messageId null;
  31.         try {
  32.             $result $sdk->createSes([
  33.                 'region' => 'us-east-1',
  34.                 'version' => 'latest',
  35.                 'credentials' => [
  36.                     'key' => $key,
  37.                     'secret' => $secret,
  38.                     ],
  39.             ])->sendEmail([
  40.                 'Destination' => [
  41.                     'ToAddresses' => $to,
  42.                 ],
  43.                 'Source' => $fromAddress,
  44.                 'Message' => [
  45.                     'Subject' => [
  46.                         'Charset' => self::CHARSET,
  47.                         'Data' => $subject,
  48.                     ],
  49.                     'Body' => [
  50.                         'Html' => [
  51.                             'Charset' => self::CHARSET,
  52.                             'Data' => $body,
  53.                         ],
  54.                         'HTML' => [
  55.                             'Charset' => self::CHARSET,
  56.                             'Data' => $body,
  57.                         ],
  58.                     ],
  59.                 ],
  60.             ]);
  61.             $messageId $result->get('MessageId');
  62.         } catch (SesException $e) {
  63.             return(sprintf('Email not sent. [%s]'$e->getAwsErrorMessage()));
  64.         }
  65.         return $messageId;
  66.     }
  67.     function getEmails($type='UNSEEN'$mark false) {
  68.         $hostname '{imap.gmail.com:993/imap/ssl}INBOX';
  69.         $username 'supportdesk@artefactsys.com';
  70.         $password 'FOxP%87g3Q9P9jGS$q';
  71.         $inbox imap_open($hostname,$username,$password);
  72.         $eArr = [];
  73.         $emails imap_search($inbox,$type); // ALL/UNSEEN/FLAGGED
  74.         if (!!$emails) {
  75.             foreach ($emails as $email_number) {
  76.                 $overview imap_fetch_overview($inbox$email_number0);
  77.                 $message imap_fetchbody($inbox$email_number2);
  78.                 $output = [];
  79.                 $output['seen'] = $overview[0]->seen 'read' 'unread';
  80.                 $output['subject'] = $overview[0]->subject;
  81.                 $output['from'] = $overview[0]->from;
  82.                 $output['date'] = $overview[0]->date;
  83.                 $output['body'] = $message;
  84.                 $output['to'] = $username;
  85.                 $eArr[] = $output;
  86.                 if ($mark) {
  87.                     imap_setflag_full($inbox$email_number"\\Seen \\Flagged"ST_UID);
  88.                 }
  89.             }
  90.         }
  91.         imap_close($inbox);
  92.         return $eArr;
  93.     }
  94.     public function sendEmailwithAttachment(
  95.         $recipient,
  96.         $sender,
  97.         $sendername,
  98.         $cc,
  99.         $cc2,
  100.         $subject,
  101.         $htmlbody,
  102.         $textbody,
  103.         $att,
  104.         $key 'AKIAIPF2PPEPUDSB64IA',
  105.         $secret 'lzvnKnTUio04OfRoFxVa0hhRUcP3McXIhYDDGJvX'
  106.     ) {
  107.         $configset 'ConfigSet';
  108. // Create an SesClient.
  109.         $client = new SesClient([
  110.             'region' => 'us-east-1',
  111.             'version' => 'latest',
  112.             'credentials' => [
  113.               'key' => $key,
  114.               'secret' => $secret,
  115.             ],
  116.         ]);
  117. // Create a new PHPMailer object.
  118.         $mail = new PHPMailer;
  119. // Add components to the email.
  120.         $mail->setFrom($sender$sendername);
  121.         $mail->addAddress($recipient);
  122.         $mail->Subject $subject;
  123.         $mail->Body $htmlbody;
  124.         $mail->AltBody $textbody;
  125.         $mail->addAttachment($att);
  126.         //$mail->addCustomHeader('X-SES-CONFIGURATION-SET', $configset);
  127.         if ($cc$mail->AddCC($cc);
  128.         if ($cc2$mail->AddCC($cc2);
  129. // Attempt to assemble the above components into a MIME message.
  130.         if (!$mail->preSend()) {
  131.             echo $mail->ErrorInfo;
  132.         } else {
  133.             // Create a new variable that contains the MIME message.
  134.             $message $mail->getSentMIMEMessage();
  135.         }
  136. // Try to send the message.
  137.         try {
  138.             $result $client->sendRawEmail([
  139.                 'RawMessage' => [
  140.                     'Data' => $message
  141.                 ]
  142.             ]);
  143.             // If the message was sent, show the message ID.
  144.             $messageId $result->get('MessageId');
  145.             echo("Email sent! Message ID: $messageId"\n");
  146.         } catch (SesException $error) {
  147.             // If the message was not sent, show a message explaining what went wrong.
  148.             echo("The email was not sent. Error message: "
  149.                 $error->getAwsErrorMessage() . "\n");
  150.         }
  151.     }
  152. }