From ea6f60246dd74daa489fb3661be9430142d4b359 Mon Sep 17 00:00:00 2001 From: Benjamin Milde Date: Sat, 1 Oct 2016 20:28:54 +0200 Subject: [PATCH 1/2] Implement attachment support for core WireMail so we can start using a common interface between different wiremail implementations --- wire/core/WireMail.php | 59 ++++++++++++++++++++++++++++----- wire/core/WireMailInterface.php | 10 ++++++ 2 files changed, 61 insertions(+), 8 deletions(-) diff --git a/wire/core/WireMail.php b/wire/core/WireMail.php index 89e54ff0..935d0bc7 100644 --- a/wire/core/WireMail.php +++ b/wire/core/WireMail.php @@ -53,6 +53,7 @@ class WireMail extends WireData implements WireMailInterface { 'bodyHTML' => '', 'header' => array(), 'param' => array(), + 'attachments' => array(), ); public function __construct() { @@ -313,6 +314,26 @@ class WireMail extends WireData implements WireMailInterface { } else { $this->mail['param'][] = $value; } + return $this; + } + + /** + * Add a file to be attached to the email + * + * Note: multiple calls will append attachments. + * To remove the supplied attachments, specify NULL as the value. + * This function may only be applicable to PHP mail(). + * + * @param string $value + * @return this + * + */ + public function attachment($value) { + if(is_null($value)) { + $this->mail['attachments'] = array(); + } else if(is_file($value)) { + $this->mail['attachments'][] = $value; + } return $this; } @@ -345,21 +366,43 @@ class WireMail extends WireData implements WireMailInterface { $text = $this->body; $html = $this->bodyHTML; - if($this->bodyHTML) { + if($this->bodyHTML || count($this->attachments)) { if(!strlen($text)) $text = strip_tags($html); + $contentType = count($this->attachments) ? 'multipart/mixed' : 'multipart/alternative'; $boundary = "==Multipart_Boundary_x" . md5(time()) . "x"; $header .= "\r\nMIME-Version: 1.0"; - $header .= "\r\nContent-Type: multipart/alternative;\r\n boundary=\"$boundary\""; + $header .= "\r\nContent-Type: $contentType;\r\n boundary=\"$boundary\""; + + // Plain Text $body = "This is a multi-part message in MIME format.\r\n\r\n" . "--$boundary\r\n" . "Content-Type: text/plain; charset=\"utf-8\"\r\n" . "Content-Transfer-Encoding: 7bit\r\n\r\n" . - "$text\r\n\r\n" . - "--$boundary\r\n" . - "Content-Type: text/html; charset=\"utf-8\"\r\n" . - "Content-Transfer-Encoding: 7bit\r\n\r\n" . - "$html\r\n\r\n" . - "--$boundary--\r\n"; + "$text\r\n\r\n"; + + // HTML + if($this->bodyHTML){ + $body .= "--$boundary\r\n" . + "Content-Type: text/html; charset=\"utf-8\"\r\n" . + "Content-Transfer-Encoding: 7bit\r\n\r\n" . + "$html\r\n\r\n"; + } + + // Attachments + foreach ($this->attachments as $file) { + $content = file_get_contents($file); + $content = chunk_split(base64_encode($content)); + $filename = basename($file); + + $body .= "--$boundary\r\n" . + "Content-Type: application/octet-stream; name=\"$filename\"\r\n" . + "Content-Transfer-Encoding: base64\r\n" . + "Content-Disposition: attachment; filename=\"$filename\"\r\n\r\n" . + "$content\r\n\r\n"; + } + + $body .= "--$boundary--\r\n"; + } else { $header .= "\r\nContent-Type: text/plain; charset=\"utf-8\""; $body = $text; diff --git a/wire/core/WireMailInterface.php b/wire/core/WireMailInterface.php index 03329d1f..328e10d1 100644 --- a/wire/core/WireMailInterface.php +++ b/wire/core/WireMailInterface.php @@ -89,6 +89,16 @@ interface WireMailInterface { */ public function param($value); + /** + * Add a file to be attached to the email + * + * + * @param string $value + * @return this + * + public function attachment($value); + */ + /** * Send the email * From 2fe4ff02f2186c83f9f03922bc34b8cb884e260c Mon Sep 17 00:00:00 2001 From: Benjamin Milde Date: Sat, 1 Oct 2016 20:35:09 +0200 Subject: [PATCH 2/2] Allow for alternative names to be supplied --- wire/core/WireMail.php | 8 ++++---- wire/core/WireMailInterface.php | 3 ++- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/wire/core/WireMail.php b/wire/core/WireMail.php index 935d0bc7..a92892ae 100644 --- a/wire/core/WireMail.php +++ b/wire/core/WireMail.php @@ -328,11 +328,12 @@ class WireMail extends WireData implements WireMailInterface { * @return this * */ - public function attachment($value) { + public function attachment($value, $filename = '') { if(is_null($value)) { $this->mail['attachments'] = array(); } else if(is_file($value)) { - $this->mail['attachments'][] = $value; + $filename = $filename ?: basename($value); + $this->mail['attachments'][$filename] = $value; } return $this; } @@ -389,10 +390,9 @@ class WireMail extends WireData implements WireMailInterface { } // Attachments - foreach ($this->attachments as $file) { + foreach ($this->attachments as $filename => $file) { $content = file_get_contents($file); $content = chunk_split(base64_encode($content)); - $filename = basename($file); $body .= "--$boundary\r\n" . "Content-Type: application/octet-stream; name=\"$filename\"\r\n" . diff --git a/wire/core/WireMailInterface.php b/wire/core/WireMailInterface.php index 328e10d1..937ff269 100644 --- a/wire/core/WireMailInterface.php +++ b/wire/core/WireMailInterface.php @@ -94,9 +94,10 @@ interface WireMailInterface { * * * @param string $value + * @param string $filename * @return this * - public function attachment($value); + public function attachment($value, $filename = ''); */ /**