Your Account
Pricing
Features
Features and Benefits of Elastic Email
Customers
A few of our great customers
Blog
Home of Elastic Email's Blog
API Documentation
Get Credit
Purchase credit to send emails
Support
Support and help for Elastic Email
Contact
Contact form to send an email to us
Home
Features
Features and Benefits of Elastic Email
Pricing
Pricing and costs of using Elastic Email
Docs
Documentation of our API
Get Credit
Get Credit for your Elastic Email account
Contact
Contact us
Your Account
Access your elastic email account.
Edit
-
Delete
Text Area - Table of Contents
API-Documentation Table of Contents
Edit
-
Delete
Text Area - More Code Samples (PHP-Zend)
More Code Samples (PHP-Zend)
Edit
-
Delete
Text Area - PHP
PHP
The following is a Zend Framework class for delivering mail via Elastic Email. Special thanks to Marco Pracucci of Spreaker (
http://spreaker.com
) who provided this.
Edit
-
Delete
Html Script Box - New Html Script Box
* @author Rocco Zanni
* @copyright Spreaker * @link www.spreaker.com */ class spEmailTransportElasticEmail extends Zend_Mail_Transport_Abstract { /** * Api send uri * @var string */ private $_api_uri = 'https://api.elasticemail.com/mailer/send'; /** * Api username (email address) * @var string */ private $_api_username; /** * Api secret key * @var string */ private $_api_key; /** * Curl handle */ private $_handle; public function __construct($api_username, $api_key, $config = array()) { if (isset($config['api_uri'])) { $this->_api_uri = $config['api_uri']; } $this->_api_username = $api_username; $this->_api_key = $api_key; $this->_handle = null; } public function __destruct() { if ($this->_handle) { curl_close($this->_handle); $this->_handle = null; } } /** * Send an email via Elastic Mail * * @return void */ public function _sendMail() { // Get handle $handle = $this->_getHandle(); if (!$handle) { throw new Zend_Mail_Transport_Exception('421 - Unable to init curl'); } // Set POST data curl_setopt($handle, CURLOPT_POSTFIELDS, array( 'username' => $this->_api_username, 'api_key' => $this->_api_key, 'from' => $this->_getFrom(), 'from_name' => $this->_getFromName(), 'to' => $this->_getTo(), 'is_html' => $this->_mail->getBodyHtml() ? "true" : "false", 'subject' => $this->_getSubject(), 'body' => $this->_getBody() )); // Exec request $response = curl_exec($handle); // Check response if (!$response || !preg_match('/[\w]+-[\w]+-[\w]+-[\w]+-[\w]+/', $response)) { throw new Zend_Mail_Transport_Exception('421 - Service currently unavailable'); } } /** * Returns the sender mail address */ private function _getFrom() { return $this->_mail->getFrom(); } /** * Returns the sender name */ private function _getFromName() { $headers = $this->_mail->getHeaders(); // Check From header if (!isset($headers['From']) || !is_array($headers['From']) || count($headers['From']) == 0) { $this->_mail->getFrom(); } // Parse From header if (!preg_match('/([\w\s]+) <[^>]+>/', $headers['From'][0], $matches)) { $this->_mail->getFrom(); } return $matches[1]; } /** * Returns the mail body */ private function _getBody() { // Get body if ($this->_mail->getBodyHtml()) { $body = $this->_mail->getBodyHtml(); } else { $body = $this->_mail->getBodyText(); } // Disable default encoding $body->encoding = Zend_Mime::ENCODING_8BIT; return $body->getContent(); } /** * Return semi colon separated list of email recipients */ private function _getTo() { return implode(';', $this->_mail->getRecipients()); } /** * Returns the mail subject */ private function _getSubject() { return $this->_mail->getSubject(); } private function _getHandle() { if ($this->_handle) { return $this->_handle; } // Create handle $this->_handle = curl_init($this->_api_uri); if (!$this->_handle) { return false; } // Set default config curl_setopt_array($this->_handle, array( CURLOPT_RETURNTRANSFER => true, CURLOPT_POST => true, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_TIMEOUT => 10, CURLOPT_HTTPHEADER => array('Connection: keep-alive', 'Keep-Alive: 300') )); return $this->_handle; } }
Terms of Use
Privacy Policy
© Copyright 2010-2011 Geographical Media Inc. -
About Us