Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public function send($to, $subject, $from, $html = '', $attachments = array(), $returnPath = '')
- {
- if ($returnPath)
- {
- $returnPath = <<<EOF
- Return-Path: {$returnPath}
- EOF;
- }
- //Create a random boundary
- $boundary = sha1(rand() . time() . 'Arron');
- //lets get started, the headers come first. Pay attention to the blank lines, they are important.
- //Following the headers is our first part of the email. The plain text version.
- $rawEmail = <<<EOE
- Subject: {$subject}
- MIME-Version: 1.0
- Content-type: multipart/alternative; boundary="{$boundary}"{$returnPath}
- EOE;
- // if we have some html set, lets create a new part and add it
- if ($html)
- {
- $rawEmail .= <<<EOE
- --{$boundary}
- Content-Type: text/html; charset=iso-8859-1
- {$html}
- EOE;
- }
- // loop through our attachments
- foreach ($attachments as $attachment)
- {
- // ensure we can access the file
- if (file_exists($attachment))
- {
- // get all the meta information we need
- $contentType = $this->mimeType($attachment);
- $size = filesize($attachment);
- $attachmentName = basename($attachment);
- //base64 encode our attachment content
- $attachmentContent = base64_encode(file_get_contents($attachment));
- $rawEmail .= <<<EOE
- --{$boundary}
- Content-Type: {$contentType}; name="{$attachmentName}"
- Content-Description: "{$attachmentName}"
- Content-Disposition: attachment; filename="{$attachmentName}"; size={$size};
- Content-Transfer-Encoding: base64
- {$attachmentContent}
- EOE;
- }
- }
- //finish off our email with the boundary
- $rawEmail .= <<<EOE
- --{$boundary}--
- EOE;
- error_log($rawEmail, 3, 'emailraw.log');
- // set up the arguments to pass to the client. You can set 'Source' in
- // here, but I encountered errors. So found setting it in the headers worked
- // best.
- $args = array(
- 'Source' => $from,
- 'Destinations' => array($to),
- 'RawMessage' => array(
- 'Data' => $rawEmail
- )
- );
- try
- {
- $response = $this->getClient()->sendRawEmail($args);
- return $response->get('MessageId');
- }
- catch (SesException $mrEx) // @todo it will be MessageRejectedException but I don't find as a class
- {
- $this->log('Unable to send email: Rejected. ' . $mrEx->getMessage());
- }
- catch (\Exception $ex)
- {
- $this->log('Unable to send email: Unknown. ' . $ex->getMessage());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement