Skip to content
Browse files

Added the ability to chain and send attachments. Boosh!

  • Loading branch information...
1 parent 9119d01 commit efd9700b390f7eca7753dd551ff96f54cb70d27a @shoxty shoxty committed Mar 11, 2011
Showing with 117 additions and 3 deletions.
  1. +20 −1 README.md
  2. +65 −2 class.simple_mail.php
  3. BIN example/lolcat_what.jpg
  4. BIN example/pbXBsZSwgY2hh.jpg
  5. +32 −0 example_attachment.php
View
21 README.md
@@ -18,4 +18,23 @@ Simple Mail Class provides a simple, chainable PHP class for sending basic email
->setMessage('<strong>This is a test message.</strong>')
->setWrap(100)
->send();
- echo ($send) ? 'Email sent successfully' : 'Could not send email';
+ echo ($send) ? 'Email sent successfully' : 'Could not send email';
+
+
+####Sending an Attachment
+
+If you are sending an attachment there is no need to add any addGenericHeader()'s. To properly send the attachments the necessary headers will be set for you. You can also chain as many attachments as you want (see example).
+
+ $mailer = new Simple_Mail(TRUE); // Set to TRUE to enable exception throwing
+ $send = $mailer->setTo('eoghan@eoghanobrien.com', 'Eoghan OBrien')
+ ->setSubject('Test Message')
+ ->setFrom('no-reply@domain.com', 'Domain.com')
+ ->addMailHeader('Reply-To', 'no-reply@domain.com', 'Domain.com')
+ ->addMailHeader('Cc', 'bill@example.com', 'Bill Gates')
+ ->addMailHeader('Bcc', 'steve@example.com', 'Steve Jobs')
+ ->addAttachment('example/pbXBsZSwgY2hh.jpg', 'lolcat_finally_arrived.jpg')
+ ->addAttachment('example/lolcat_what.jpg')
+ ->setMessage('<strong>This is a test message.</strong>')
+ ->setWrap(100)
+ ->send();
+ echo ($send) ? 'Email sent successfully' : 'Could not send email';
View
67 class.simple_mail.php 100644 → 100755
@@ -48,6 +48,24 @@ class Simple_Mail
*/
protected $_throwExceptions = FALSE;
+ /**
+ * @var string $_attachment (default value: array())
+ * @access protected
+ */
+ protected $_attachment = array();
+
+ /**
+ * @var string $_attachmentPath (default value: array())
+ * @access protected
+ */
+ protected $_attachmentPath = array();
+
+ /**
+ * @var string $attachment_filename (default value: array())
+ * @access protected
+ */
+ protected $_attachmentFilename = array();
+
/**
* __construct function.
@@ -140,6 +158,27 @@ public function setMessage($message)
}
/**
+ * addAttachment function.
+ *
+ * @access public
+ * @param string $message
+ * @return void
+ */
+ public function addAttachment($path, $filename = NULL)
+ {
+ $this->_attachmentPath[] = $path;
+ $this->_attachmentFilename[] = empty($filename) ? basename($path) : $filename;
+
+ $fileSize = filesize($path);
+ $handle = fopen($path, "r");
+ $attachment = fread($handle, $fileSize);
+ fclose($handle);
+ $this->_attachment[] = chunk_split(base64_encode($attachment));
+
+ return $this;
+ }
+
+ /**
* setFrom function.
*
* @access public
@@ -234,10 +273,34 @@ public function setWrap($wrap = 70)
* @return void
*/
public function send()
- {
+ {
$headers = ( !empty($this->_headers) ) ? join("\r\n", $this->_headers) : array();
- $send = mail($this->_to, $this->_subject, wordwrap($this->_message, $this->_wrap), $headers);
+ if($this->_attachment)
+ {
+ $uid = md5(uniqid(time()));
+ $headers .= "MIME-Version: 1.0\r\n";
+ $headers .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
+ $headers .= "This is a multi-part message in MIME format.\r\n";
+ $headers .= "--".$uid."\r\n";
+ $headers .= "Content-type:text/html; charset=\"utf-8\"\r\n";
+ $headers .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
+ $headers .= $this->_message."\r\n\r\n";
+ $headers .= "--".$uid."\r\n";
+ foreach($this->_attachmentFilename as $key => $value)
+ {
+ $headers .= "Content-Type: application/octet-stream; name=\"".$value."\"\r\n";
+ $headers .= "Content-Transfer-Encoding: base64\r\n";
+ $headers .= "Content-Disposition: attachment; filename=\"".$value."\"\r\n\r\n";
+ $headers .= $this->_attachment[$key]."\r\n\r\n";
+ $headers .= "--".$uid."\r\n";
+ }
+ $send = mail($this->_to, $this->_subject, "", $headers);
+ }
+ else
+ {
+ $send = mail($this->_to, $this->_subject, wordwrap($this->_message, $this->_wrap), $headers);
+ }
if ( ! $send && $this->_throwExceptions) {
throw new Exception('Email failed to send');
View
BIN example/lolcat_what.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN example/pbXBsZSwgY2hh.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
32 example_attachment.php
@@ -0,0 +1,32 @@
+<?php
+
+require 'class.simple_mail.php';
+echo '<h1>Simple Mail</h1>';
+
+$mailer = new Simple_Mail(TRUE); // Set to TRUE to enable exception throwing
+
+// if you are sending an attachment there is no need to add any addGenericHeader()'s.
+$send = $mailer->setTo('shoxty@gmail.com', 'Eoghan OBrien')
+ ->setSubject('Test Message')
+ ->setFrom('no-reply@domain.com', 'Domain.com')
+ ->addMailHeader('Reply-To', 'no-reply@domain.com', 'Domain.com')
+/*
+ ->addMailHeader('Cc', 'bill@example.com', 'Bill Gates')
+ ->addMailHeader('Bcc', 'steve@example.com', 'Steve Jobs')
+*/
+ ->addAttachment('example/pbXBsZSwgY2hh.jpg', 'lolcat_finally_arrived.jpg')
+ ->addAttachment('example/lolcat_what.jpg')
+ ->setMessage('<strong>This is a test message.</strong>')
+ ->setWrap(100)
+ ->send();
+
+/* $mailer->debug(); */
+
+if ($send) {
+ echo 'Email sent successfully';
+}
+else {
+ echo 'Could not send email';
+}
+
+?>

0 comments on commit efd9700

Please sign in to comment.
Something went wrong with that request. Please try again.