Commit 7f69ed59 authored by Charles Drew's avatar Charles Drew

Initial Committ

parent 1a6fa5dd
The MIT License (MIT)
Copyright (c) 2014 chuckdrew
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
\ No newline at end of file
mailgin_magento
===============
Mailgun extension for Magento
<?php
class FreeLunchLabs_MailGun_Block_Adminhtml_Customer_Email extends Mage_Adminhtml_Block_Widget_Grid_Container {
protected function _construct() {
parent::_construct();
$this->_blockGroup = 'freelunchlabs_mailgun';
$this->_controller = 'adminhtml_customer_email';
$this->_headerText = false;
}
}
\ No newline at end of file
<?php
class FreeLunchLabs_MailGun_Block_Adminhtml_Customer_Email_Grid extends Mage_Adminhtml_Block_Widget_Grid {
public function __construct() {
parent::__construct();
$this->setId('mailgun_email_grid');
$this->setDefaultSort('id');
$this->setDefaultDir('ASC');
$this->setUseAjax(true);
$this->setFilterVisibility(true);
}
protected function _prepareCollection() {
$collection = Mage::getResourceModel('freelunchlabs_mailgun/email_collection')->getGridCollection();
//Filter on Customer
$collection->addFieldToFilter('customer_id', Mage::registry('current_customer')->getId());
$this->setCollection($collection);
return parent::_prepareCollection();
}
protected function _prepareColumns() {
$this->addColumn('subject', array(
'header' => 'Subject',
'type' => 'text',
'index' => 'subject'
));
$this->addColumn('mailgun_id', array(
'header' => 'Mailgun ID',
'type' => 'text',
'index' => 'mailgun_id'
));
$this->addColumn('current_status', array(
'header' => 'Latest Status',
'index' => 'current_status',
'renderer' => 'FreeLunchLabs_MailGun_Block_Adminhtml_Event_Renderer_Type'
));
$this->addColumn('date_sent', array(
'header' => 'Date Sent',
'type' => 'datetime',
'index' => 'date_sent'
));
return parent::_prepareColumns();
}
public function getRowUrl($row) {
return $this->getUrl('*/emailTracking/emailDetail', array(
'id' => $row->getId()
));
}
public function getGridUrl() {
return $this->getUrl('*/emailtracking/emailGrid', array(
'_current' => true,
'id' => Mage::registry('current_customer')->getId()
));
}
}
\ No newline at end of file
<?php
class FreeLunchLabs_MailGun_Block_Adminhtml_Customer_Tab_Mailgun extends Mage_Adminhtml_Block_Template implements Mage_Adminhtml_Block_Widget_Tab_Interface {
public function _construct() {
parent::_construct();
$this->setTemplate('mailgun/customer/tab/tab.phtml');
}
public function getTabLabel() {
return $this->__('Email Tracking');
}
public function getTabTitle() {
return $this->__('View emails sent to this customer and the email history');
}
public function canShowTab() {
return Mage::getStoreConfig('mailgun/events/store');
}
public function isHidden() {
return false;
}
public function getAfter() {
return 'tags';
}
public function getGrid() {
$gridBlock = $this->getLayout()->createBlock('freelunchlabs_mailgun/adminhtml_customer_email');
return $gridBlock->getGridHtml();
}
}
\ No newline at end of file
<?php
class FreeLunchLabs_MailGun_Block_Adminhtml_Emailtracking extends Mage_Adminhtml_Block_Template {
function getFetchActivityUrl() {
return $this->getUrl('*/*/getEmailEvents');
}
public function getEmailDetail() {
$email = Mage::registry('current_email');
if ($email->getCustomerId()) {
$customer = Mage::getModel('customer/customer')->load($email->getCustomerId());
$email->setCustomer($customer);
}
return $email;
}
public function getBackButtonHtml() {
return $this->getLayout()->createBlock('adminhtml/widget')->getButtonHtml('Back', 'history.back()', 'back');
}
public function getViewEmailBodyButtonHtml($emailId) {
$url = $this->getUrl('*/*/emailView', array(
'id' => $emailId
));
$onClick = "window.open('{$url}','name','width=700,height=800')";
return $this->getLayout()->createBlock('adminhtml/widget')->getButtonHtml('View Email Body', $onClick);
}
public function getEditCustomerUrl($customerId) {
return $url = $this->getUrl('*/customer/edit', array(
'id' => $customerId
));
}
public function getCustomerGroupName($customerGroupId) {
return Mage::getModel('customer/group')->load($customerGroupId)->getCustomerGroupCode();
}
public function formatCustomerCreateDate($createdTimestamp) {
return Mage::helper('core')->formatDate($createdTimestamp, Mage_Core_Model_Locale::FORMAT_TYPE_MEDIUM, true);
}
public function getFetch24HoursOfEmailActivityButton() {
$onClick = "setLocation('{$this->getFetchActivityUrl()}')";
return $this->getLayout()->createBlock('adminhtml/widget')->getButtonHtml('Fetch Past 24 Hours of Email Activity', $onClick);
}
public function getDeleteEmailTrackingLogsDaysButton() {
$days = Mage::getStoreConfig('mailgun/events/days');
if ($days) {
$url = $this->getUrl('*/*/deleteEmailTrackingLogsDays');
$onClick = "confirmSetLocation('Are you sure?', '{$url}')";
return $this->getLayout()->createBlock('adminhtml/widget')->getButtonHtml("Delete Email Records Older Than {$days} Days", $onClick, 'delete');
} else {
return "";
}
}
public function getDeleteAllEmailTrackingLogsButton() {
$url = $this->getUrl('*/*/deleteEmailTrackingLogs');
$onClick = "confirmSetLocation('Are you sure?', '{$url}')";
return $this->getLayout()->createBlock('adminhtml/widget')->getButtonHtml('Delete All Email Records', $onClick, 'delete');
}
}
<?php
class FreeLunchLabs_MailGun_Block_Adminhtml_Event_Container extends Mage_Adminhtml_Block_Widget_Grid_Container {
protected function _construct() {
parent::_construct();
$this->_blockGroup = 'freelunchlabs_mailgun';
$this->_controller = 'adminhtml_event';
$this->_headerText = false;
}
}
\ No newline at end of file
<?php
class FreeLunchLabs_MailGun_Block_Adminhtml_Event_Grid extends Mage_Adminhtml_Block_Widget_Grid {
public function __construct() {
parent::__construct();
$this->setId('mailgun_event_grid');
$this->setDefaultSort('timestamp');
$this->setDefaultDir('ASC');
$this->setUseAjax(false);
$this->setFilterVisibility(false);
}
protected function _prepareCollection() {
$collection = Mage::getResourceModel('freelunchlabs_mailgun/event_collection');
$collection->addFieldToFilter('email_id', Mage::registry('current_email')->getId());
$this->setCollection($collection);
return parent::_prepareCollection();
}
protected function _prepareColumns() {
$this->addColumn('event_type', array(
'header' => 'Event',
'index' => 'event_type',
'renderer' => 'FreeLunchLabs_MailGun_Block_Adminhtml_Event_Renderer_Type'
));
$this->addColumn('timestamp', array(
'header' => 'Event Time',
'index' => 'timestamp',
'renderer' => 'FreeLunchLabs_MailGun_Block_Adminhtml_Event_Renderer_Timestamp'
));
return parent::_prepareColumns();
}
public function getRowUrl($row) {
return null;
}
public function getGridUrl() {
return $this->getUrl('*/emailTracking/emailDetail', array(
'_current' => true,
'id' => Mage::registry('current_email')->getId()
));
}
}
\ No newline at end of file
<?php
class FreeLunchLabs_MailGun_Block_Adminhtml_Event_Renderer_Timestamp extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{
public function render(Varien_Object $row)
{
$value = $row->getData($this->getColumn()->getIndex());
$date = Mage::getSingleton('core/date')->date(null, $value);
return Mage::helper('core')->formatDate(new Zend_Date($date), 'medium', true);
}
}
\ No newline at end of file
<?php
class FreeLunchLabs_MailGun_Block_Adminhtml_Event_Renderer_Type extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{
public function render(Varien_Object $row)
{
$value = $row->getData($this->getColumn()->getIndex());
return ucwords($value);
}
}
\ No newline at end of file
<?php
class FreeLunchLabs_MailGun_Block_Adminhtml_Global_Container extends Mage_Adminhtml_Block_Widget_Grid_Container {
protected function _construct() {
parent::_construct();
$this->_blockGroup = 'freelunchlabs_mailgun';
$this->_controller = 'adminhtml_global';
$this->_headerText = false;
}
}
\ No newline at end of file
<?php
class FreeLunchLabs_MailGun_Block_Adminhtml_Global_Grid extends Mage_Adminhtml_Block_Widget_Grid {
public function __construct() {
parent::__construct();
$this->setId('mailgun_global_grid');
$this->setDefaultSort('date_sent');
$this->setDefaultDir('ASC');
$this->setUseAjax(false);
$this->setFilterVisibility(true);
}
protected function _prepareCollection() {
$collection = Mage::getResourceModel('freelunchlabs_mailgun/email_collection')->getGridCollection();
$this->setCollection($collection);
return parent::_prepareCollection();
}
protected function _prepareColumns() {
$this->addColumn('subject', array(
'header' => 'Subject',
'type' => 'text',
'index' => 'subject'
));
$this->addColumn('email_address', array(
'header' => 'Recipient Address',
'type' => 'email',
'index' => 'email_address'
));
$this->addColumn('mailgun_id', array(
'header' => 'Mailgun ID',
'type' => 'text',
'index' => 'mailgun_id'
));
$this->addColumn('current_status', array(
'header' => 'Latest Status',
'index' => 'current_status',
'renderer' => 'FreeLunchLabs_MailGun_Block_Adminhtml_Event_Renderer_Type'
));
$this->addColumn('date_sent', array(
'header' => 'Date Sent',
'type' => 'datetime',
'index' => 'date_sent'
));
return parent::_prepareColumns();
}
public function getRowUrl($row) {
return $this->getUrl('*/emailTracking/emailDetail', array(
'id' => $row->getId()
));
}
public function getGridUrl() {
return $this->getUrl('*/emailtracking');
}
}
\ No newline at end of file
<?php
class FreeLunchLabs_MailGun_Helper_Data extends Mage_Core_Helper_Abstract {
}
\ No newline at end of file
<?php
class FreeLunchLabs_MailGun_Model_Email extends Mage_Core_Model_Abstract {
protected function _construct() {
$this->_init('freelunchlabs_mailgun/email');
}
public function saveInitialSend($message, $sendResponse) {
$customer = Mage::getModel('customer/customer');
$customer->setWebsiteId($message->getStore()->getWebsite()->getId());
$customer->loadByEmail($message->getPrimaryRecipient());
if ($customer) {
$this->setCustomerId($customer->getId());
}
$this->setEmailAddress($message->getPrimaryRecipient());
$this->setMailgunId(str_replace(array("<", ">"), "", $sendResponse->id));
$this->setSubject($message->getSubject());
$this->setBody($message->getHtmlBody());
$this->setDateSent(Mage::getSingleton('core/date')->gmtTimestamp());
$this->save();
Mage::getModel('freelunchlabs_mailgun/event')->logEmailEvent($this->getId(), FreeLunchLabs_MailGun_Model_Event::PUSHED, $this);
}
public function loadByMailgunIdAndRecipient($mailgunId, $recipient) {
$collection = $this->getCollection();
$collection->addFieldToFilter('mailgun_id', $mailgunId);
$collection->addFieldToFilter('email_address', $recipient);
return $collection->getFirstItem();
}
public function deleteEmailTrackingLogsDays() {
$days = Mage::getStoreConfig('mailgun/events/days');
if ($days) {
$this->deleteEmailTrackingLogs($days);
}
}
public function deleteEmailTrackingLogs($days = false) {
$this->getResource()->deleteEmailTrackingLogs($days);
}
}
\ No newline at end of file
<?php
class FreeLunchLabs_MailGun_Model_Email_Template extends Mage_Core_Model_Email_Template {
var $bcc = null;
var $replyto = null;
var $returnPath = null; //This is not used because MailGun overides it for their internal purposes.
public function send($email, $name = null, array $variables = array()) {
//Get appropriate store
if(isset($variables['store'])) {
$store = $variables['store'];
} elseif($this->getDesignConfig()->getStore()) {
$store = Mage::getModel('core/store')->load($this->getDesignConfig()->getStore());
} else {
$store = Mage::app()->getStore();
}
if ($store->getConfig('mailgun/general/active')) {
if (!$this->isValidForSend()) {
Mage::logException(new Exception('This letter cannot be sent.'));
return false;
}
$message = Mage::getModel('freelunchlabs_mailgun/messagebuilder');
//Recipient(s)
$emails = array_values((array) $email);
$names = is_array($name) ? $name : (array) $name;
$names = array_values($names);
foreach ($emails as $key => $email) {
if (!isset($names[$key])) {
$names[$key] = substr($email, 0, strpos($email, '@'));
}
}
$variables['email'] = reset($emails);
$variables['name'] = reset($names);
//Add To Recipients
$isPrimary = true;
foreach ($emails as $key => $email) {
if($isPrimary) {
//Add primary recipient
$message->setPrimaryRecipient($email);
}
$isPrimary = false;
$message->addToRecipient($names[$key] . " <" . $email . ">");
}
//Subject
$subject = $this->getProcessedTemplateSubject($variables);
$message->setSubject($subject);
//From Name
$message->setFromAddress($this->getSenderName() . " <" . $this->getSenderEmail() . ">");
//Bcc
if (is_array($this->bcc)) {
foreach ($this->bcc as $bcc_email) {
$message->addBccRecipient($bcc_email);
}
} elseif ($this->bcc) {
$message->addBccRecipient($this->bcc);
}
//Reply To
if (!is_null($this->replyto)) {
$message->setReplyToAddress($this->replyto);
}
//Message Body
$this->setUseAbsoluteLinks(true);
$processedTemplateBody = $this->getProcessedTemplate($variables, true);
if ($this->isPlain()) {
$message->setTextBody($processedTemplateBody);
} else {
$message->setHtmlBody($processedTemplateBody);
}
//Add Unique Args
$message->addCustomData("message_data", array('id' => 123456));
//Tag message with type
$message->addTag($this->getTemplateId());
$message->addTag($store->getConfig('mailgun/general/tag'));
if($store->getConfig('mailgun/events/opens')) {
$message->setOpenTracking(true);
}
if($store->getConfig('mailgun/events/clicks')) {
$message->setClickTracking(true);
}
//Set store
$message->setStore($store);
//Send it!
try {
Mage::getModel('freelunchlabs_mailgun/mailgun')->send($message);
$this->_mail = null;
} catch (Exception $e) {
$this->_mail = null;
Mage::logException($e);
return false;
}
return true;
} else {
return parent::send($email, $name, $variables);
}
}
public function addBcc($bcc) {
$this->bcc = $bcc;
return parent::addBcc($bcc);
}
public function setReturnPath($email) {
$this->returnPath = $email;
return parent::setReturnPath($email);
}
public function setReplyTo($email) {
$this->replyto = $email;
return parent::setReplyTo($email);
}
}
\ No newline at end of file
<?php
class FreeLunchLabs_MailGun_Model_Event extends Mage_Core_Model_Abstract {
//Event types
const PUSHED = "pushed";
const ACCEPTED = "accepted";
const REJECTED = "rejected";
const DELIVERED = "delivered";
const FAILED = "failed";
const OPENED = "opened";
const CLICKED = "clicked";
const UNSUBSCRIBED = "unsubscribed";
const COMPLAINED = "complained";
const STORED = "stored";
protected function _construct() {
$this->_init('freelunchlabs_mailgun/event');
}
public function logEmailEvent($emailId, $eventType, $email = false) {
$this->setEmailId($emailId);
$this->setEventType($eventType);
$this->setTimestamp(Mage::getSingleton('core/date')->gmtTimestamp());
$this->save();
}
public function loadByTimestampAndEmailId($timestamp, $emailId) {
$collection = $this->getCollection();
$collection->addFieldToFilter('timestamp', $timestamp);
$collection->addFieldToFilter('email_id', $emailId);
return $collection->getFirstItem();
}
}
\ No newline at end of file
<?php
class FreeLunchLabs_MailGun_Model_Exceptions_InvalidParameter extends \Exception{}
?>
\ No newline at end of file
<?php
class FreeLunchLabs_MailGun_Model_Exceptions_InvalidParameterType extends \Exception{}
?>
\ No newline at end of file
<?php
class FreeLunchLabs_MailGun_Model_Exceptions_MissingRequiredMIMEParameters extends \Exception{}
?>
\ No newline at end of file
<?php
class FreeLunchLabs_MailGun_Model_Exceptions_TooManyParameters extends \Exception{}
?>
\ No newline at end of file
<?php
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) {
$client = new Zend_Http_Client();
$client->setAuth("api", $apiKey);
$client->setMethod($method);
if($uriOveride) {
$client->setUri($uriOveride);
} else {
$client->setUri($this->apiUrl . $domain . "/" . $type);