Commit 52aa1998 authored by Charles Drew's avatar Charles Drew

Added support for attachments

parent 9f501dbd
......@@ -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));
......
......@@ -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);
......
......@@ -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
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment