Advertisement
Guest User

Untitled

a guest
Aug 29th, 2016
385
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.81 KB | None | 0 0
  1.     public function send($to, $subject, $from, $html = '', $attachments = array(), $returnPath = '')
  2.     {
  3.         if ($returnPath)
  4.         {
  5.             $returnPath = <<<EOF
  6.  
  7. Return-Path: {$returnPath}
  8. EOF;
  9.         }
  10.  
  11.         //Create a random boundary
  12.         $boundary = sha1(rand() . time() . 'Arron');
  13.  
  14.         //lets get started, the headers come first. Pay attention to the blank lines, they are important.
  15.         //Following the headers is our first part of the email. The plain text version.
  16.         $rawEmail = <<<EOE
  17.  
  18. Subject: {$subject}
  19. MIME-Version: 1.0
  20. Content-type: multipart/alternative; boundary="{$boundary}"{$returnPath}
  21.  
  22. EOE;
  23.         // if we have some html set, lets create a new part and add it
  24.         if ($html)
  25.         {
  26.             $rawEmail .= <<<EOE
  27.  
  28. --{$boundary}
  29. Content-Type: text/html; charset=iso-8859-1
  30.  
  31. {$html}
  32.  
  33. EOE;
  34.         }
  35.  
  36.         // loop through our attachments
  37.         foreach ($attachments as $attachment)
  38.         {
  39.             // ensure we can access the file
  40.             if (file_exists($attachment))
  41.             {
  42.                 // get all the meta information we need
  43.                 $contentType        = $this->mimeType($attachment);
  44.                 $size               = filesize($attachment);
  45.                 $attachmentName     = basename($attachment);
  46.                 //base64 encode our attachment content
  47.                 $attachmentContent  = base64_encode(file_get_contents($attachment));
  48.  
  49.                 $rawEmail .= <<<EOE
  50. --{$boundary}
  51. Content-Type: {$contentType}; name="{$attachmentName}"
  52. Content-Description: "{$attachmentName}"
  53. Content-Disposition: attachment; filename="{$attachmentName}"; size={$size};
  54. Content-Transfer-Encoding: base64
  55.  
  56. {$attachmentContent}
  57.  
  58. EOE;
  59.             }
  60.         }
  61.  
  62.         //finish off our email with the boundary
  63.         $rawEmail .= <<<EOE
  64.  
  65. --{$boundary}--
  66.  
  67. EOE;
  68.  
  69.         error_log($rawEmail, 3, 'emailraw.log');
  70.  
  71.         // set up the arguments to pass to the client. You can set 'Source' in
  72.         // here, but I encountered errors. So found setting it in the headers worked
  73.         // best.
  74.         $args = array(
  75.             'Source'        => $from,
  76.             'Destinations'  => array($to),
  77.             'RawMessage'    => array(
  78.                 'Data' => $rawEmail
  79.             )
  80.         );
  81.  
  82.         try
  83.         {
  84.             $response = $this->getClient()->sendRawEmail($args);
  85.             return $response->get('MessageId');
  86.         }
  87.         catch (SesException $mrEx) // @todo it will be MessageRejectedException but I don't find as a class
  88.         {
  89.             $this->log('Unable to send email: Rejected. ' . $mrEx->getMessage());
  90.         }
  91.         catch (\Exception $ex)
  92.         {
  93.             $this->log('Unable to send email: Unknown. ' . $ex->getMessage());
  94.         }
  95.  
  96.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement