From 52aa19982af7b4dc820b663914853b844ae6cb23 Mon Sep 17 00:00:00 2001 From: Charles Drew <cdrew@directvetmarketing.com> Date: Wed, 1 Apr 2015 15:10:07 -0400 Subject: [PATCH] Added support for attachments --- .../MailGun/Model/Email/Template.php | 9 ++++++++ .../FreeLunchLabs/MailGun/Model/Mailgun.php | 19 +++++++++++++--- .../MailGun/Model/Messagebuilder.php | 22 ++++++++++--------- 3 files changed, 37 insertions(+), 13 deletions(-) diff --git a/app/code/community/FreeLunchLabs/MailGun/Model/Email/Template.php b/app/code/community/FreeLunchLabs/MailGun/Model/Email/Template.php index 6ccdd13..d174ddf 100644 --- a/app/code/community/FreeLunchLabs/MailGun/Model/Email/Template.php +++ b/app/code/community/FreeLunchLabs/MailGun/Model/Email/Template.php @@ -80,6 +80,15 @@ class FreeLunchLabs_MailGun_Model_Email_Template extends Mage_Core_Model_Email_T $message->setHtmlBody($processedTemplateBody); } + //Attachments + if($this->getMail()->hasAttachments) { + foreach($this->getMail()->getParts() as $part) { + if($part->disposition == "attachment") { + $message->addAttachment($part->filename, $part->getRawContent()); + } + } + } + //Add Unique Args $message->addCustomData("message_data", array('id' => 123456)); diff --git a/app/code/community/FreeLunchLabs/MailGun/Model/Mailgun.php b/app/code/community/FreeLunchLabs/MailGun/Model/Mailgun.php index 49ade88..81a8d93 100644 --- a/app/code/community/FreeLunchLabs/MailGun/Model/Mailgun.php +++ b/app/code/community/FreeLunchLabs/MailGun/Model/Mailgun.php @@ -4,7 +4,7 @@ class FreeLunchLabs_MailGun_Model_Mailgun extends Mage_Core_Model_Abstract { public $apiUrl = "https://api.mailgun.net/v2/"; - public function mailgunRequest($type, $domain, $apiKey, $data, $method = Zend_Http_Client::GET, $uriOveride = false) { + public function mailgunRequest($type, $domain, $apiKey, $data, $method = Zend_Http_Client::GET, $uriOveride = false, $files = null) { $client = new Zend_Http_Client(); $client->setAuth("api", $apiKey); @@ -25,6 +25,12 @@ class FreeLunchLabs_MailGun_Model_Mailgun extends Mage_Core_Model_Abstract { $client->setParameterGet($key, $value); } } + + if($files) { + foreach($files as $file) { + $client->setFileUpload($file['filename'], $file['param'], $file['data']); + } + } try { $response = $client->request(); @@ -44,8 +50,15 @@ class FreeLunchLabs_MailGun_Model_Mailgun extends Mage_Core_Model_Abstract { $domain = $message->getStore()->getConfig('mailgun/general/domain'); $apiKey = $message->getStore()->getConfig('mailgun/general/key'); - - $sendResponse = $this->mailgunRequest('messages', $domain, $apiKey, $message->getMessage(), Zend_Http_Client::POST); + $files = null; + + if(count($message->getAttachments())) { + foreach($message->getAttachments() as $attachment) { + $files[] = $attachment; + } + } + + $sendResponse = $this->mailgunRequest('messages', $domain, $apiKey, $message->getMessage(), Zend_Http_Client::POST, false, $files); if($message->getStore()->getConfig('mailgun/events/store')) { Mage::getModel('freelunchlabs_mailgun/email')->saveInitialSend($message, $sendResponse); diff --git a/app/code/community/FreeLunchLabs/MailGun/Model/Messagebuilder.php b/app/code/community/FreeLunchLabs/MailGun/Model/Messagebuilder.php index 5260d03..6eb4de2 100644 --- a/app/code/community/FreeLunchLabs/MailGun/Model/Messagebuilder.php +++ b/app/code/community/FreeLunchLabs/MailGun/Model/Messagebuilder.php @@ -25,6 +25,7 @@ class FreeLunchLabs_MailGun_Model_Messagebuilder extends Varien_Object { protected $message = array(); protected $variables = array(); protected $files = array(); + protected $attachments = array(); protected $counters = array('recipients' => array('to' => 0, 'cc' => 0, 'bcc' => 0), @@ -150,16 +151,13 @@ class FreeLunchLabs_MailGun_Model_Messagebuilder extends Varien_Object { return $this->message['html']; } - public function addAttachment($attachmentPath, $attachmentName = null) { - if (preg_match("/^@/", $attachmentPath)) { - if (isset($this->files["attachment"])) { - $attachment = array('filePath' => $attachmentPath, - 'remoteName' => $attachmentName); - array_push($this->files["attachment"], $attachment); - } else { - $this->files["attachment"] = array(array('filePath' => $attachmentPath, - 'remoteName' => $attachmentName)); - } + public function addAttachment($filename, $data) { + if ($filename != null && $data != null) { + $this->attachments[] = array( + 'filename' => $filename, + 'data' => $data, + 'param' => 'attachment' + ); return true; } else { throw new FreeLunchLabs_MailGun_Model_Exceptions_InvalidParameter(self::INVALID_PARAMETER_ATTACHMENT); @@ -293,4 +291,8 @@ class FreeLunchLabs_MailGun_Model_Messagebuilder extends Varien_Object { return $this->files; } + public function getAttachments() { + return $this->attachments; + } + } \ No newline at end of file -- GitLab