Skip to content
Browse files

Merge pull request #1 from gollyg/multiple_attachments

Change to allow multiple attachments.
  • Loading branch information...
2 parents f9aee7b + dd7dc84 commit 66733145467dbb2fe64b62fa8c6d54ff1504c70a @ngmaloney committed
Showing with 64 additions and 67 deletions.
  1. +64 −67 attachment_email.php
View
131 attachment_email.php
@@ -1,91 +1,88 @@
<?php
/**
- * @File a mime mail attachment class used for sending emails with attachments.
+ * @File a mime mail attachment class used for sending emails with multiple attachments.
*
+ * Forked from https://github.com/ngmaloney/Attachment-Email
* Modified from a version created at http://www.kavoir.com/2009/08/php-email-attachment-class.html
-**/
+ **/
class AttachmentEmail {
- private $from = '';
- private $from_name = '';
- private $reply_to = '';
- private $to = '';
- private $subject = '';
- private $message = '';
- private $attachment = '';
- private $attachment_filename = '';
+ private $to = '';
+ private $from = '';
+ private $subject = '';
+ private $message = '';
+ private $attachments = '';
/**
- * Function for defining email
+ * Constructor.
+ *
* @param $to
* Email recipient address
* @param $from
* Email sender address
* @param $message
- * Message text of body. (currenlty plain text only)
+ * Message text of body. (currently plain text only)
* $param $attachment (optional)
* An array containing attachment file name and path
* array('filename' => 'attachment.pdf', 'uri' => '/tmp/attachment.pdf')
- **/
- public function __construct($to, $from, $subject, $message, $attachment =
- array()) {
- $this->to = $to;
+ **/
+ public function __construct($to, $from, $subject, $message, $attachments = array()) {
+ $this->to = $to;
$this->from = $from;
- $this->subject = $subject;
- $this->message = $message;
- $this->attachment = $attachment['uri'];
- $this->attachment_filename = $attachment['filename'];
- }
+ $this->subject = $subject;
+ $this->message = $message;
+ $this->attachments = $attachments;
+ $this->boundary = md5(date('r', time()));
+ }
/**
- * Hook for sending actual eamil
- **/
+ * Send the email.
+ **/
public function send() {
- if (!empty($this -> attachment)) {
- $filename = empty($this -> attachment_filename) ? basename($this -> attachment) : $this -> attachment_filename ;
- $path = dirname($this -> attachment);
- $mailto = $this -> to;
- $from_mail = $this -> from;
- $from_name = $this -> from_name;
- $replyto = $this -> reply_to;
- $subject = $this -> subject;
- $message = $this -> message;
+ $header = "From: ".($this->from)." <".($this->from).">" . PHP_EOL;
+ $header .= "Reply-To: ".($this->from). PHP_EOL;
- $content = file_get_contents($this->attachment);
- $content = chunk_split(base64_encode($content));
- $uid = md5(uniqid(time()));
- $header = "From: " . $from_mail . "\r\n";
- $header .= "MIME-Version: 1.0\r\n";
- $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
- $header .= "This is a multi-part message in MIME format.\r\n";
- $header .= "--".$uid."\r\n";
- $header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
- $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
- $header .= $message."\r\n\r\n";
- $header .= "--".$uid."\r\n";
- $header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; // use diff. tyoes here
- $header .= "Content-Transfer-Encoding: base64\r\n";
- $header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
- $header .= $content."\r\n\r\n";
- $header .= "--".$uid."--";
- if (mail($mailto, $subject, "", $header)) {
- return true;
- }
- else {
- return false;
- }
- }
- else {
- $header = "From: ".($this -> from_name)." <".($this -> from).">\r\n";
- $header .= "Reply-To: ".($this -> reply_to)."\r\n";
+ if (!empty($this->attachments)) {
+
+ $header .= "Content-Transfer-Encoding: 7bit" . PHP_EOL;
+ $header .= "Content-Type: multipart/mixed; boundary=\"". $this->boundary . '"' . PHP_EOL;
+ $header .= "MIME-Version: 1.0" . PHP_EOL;
- if (mail($this -> to, $this -> subject, $this -> message, $header)) {
- return true;
- } else {
+ $message = "This is a multi-part message in MIME format." . PHP_EOL . PHP_EOL;
+ $message .= "--". $this->boundary . PHP_EOL;
+ $message .= "Content-Transfer-Encoding: binary" . PHP_EOL;
+ $message .= "Content-type:text/plain; charset=iso-8859-1" . PHP_EOL . PHP_EOL;
+ $message .= $this->message . PHP_EOL . PHP_EOL;
+ $message .= $this->get_binary_attachments() . PHP_EOL;
+ $message .= '--'. $this->boundary .'--' . PHP_EOL;
+ $this->message = $message;
+ }
- return false;
- }
+ if (mail($this->to, $this->subject, $this->message, $header)) {
+ return true;
+ }
+ else {
+ return false;
+ }
+ }
+
+ /**
+ * Get the attachments in a base64 format.
+ *
+ * @return string Returns a string representing the attachments.
+ */
+ public function get_binary_attachments() {
+ $output = '';
+ foreach($this->attachments as $attachment) {
- }
- }
-}
+ $attachment_bin = file_get_contents($attachment['path']);
+ $attachment_bin = chunk_split(base64_encode($attachment_bin));
+ $output .= '--'. $this->boundary . PHP_EOL;
+ $output .= 'Content-Type: '. $attachment['type'] .'; name="'. basename($attachment['path']) .'"' . PHP_EOL;
+ $output .= 'Content-Transfer-Encoding: base64' . PHP_EOL;
+ $output .= 'Content-Disposition: attachment' . PHP_EOL . PHP_EOL;
+ $output .= $attachment_bin . PHP_EOL . PHP_EOL;
+ }
+ return $output;
+ }
+}

0 comments on commit 6673314

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