Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static function sendEmail( $message, $tags = array(), $template_name = '', $track_opens = true, $track_clicks = true ) {
- try {
- // Checking if we are connected to Mandrill
- wpMandrill::getConnected();
- if ( !self::isConnected() ) throw new Exception('Invalid API Key');
- /************
- *
- * Processing supplied fields to make them valid for the Mandrill API
- *
- *************************/
- // Checking the user-specified headers
- if ( empty( $message['headers'] ) ) {
- $message['headers'] = array();
- } else {
- if ( !is_array( $message['headers'] ) ) {
- $tempheaders = explode( "\n", str_replace( "\r\n", "\n", $message['headers'] ) );
- } else {
- $tempheaders = $message['headers'];
- }
- $message['headers'] = array();
- // If it's actually got contents
- if ( !empty( $tempheaders ) ) {
- // Iterate through the raw headers
- foreach ( (array) $tempheaders as $header ) {
- if ( strpos($header, ':') === false ) continue;
- // Explode them out
- list( $name, $content ) = explode( ':', trim( $header ), 2 );
- // Cleanup crew
- $name = trim( $name );
- $content = trim( $content );
- switch ( strtolower( $name ) ) {
- case 'from':
- if ( strpos($content, '<' ) !== false ) {
- // So... making my life hard again?
- $from_name = substr( $content, 0, strpos( $content, '<' ) - 1 );
- $from_name = str_replace( '"', '', $from_name );
- $from_name = trim( $from_name );
- $from_email = substr( $content, strpos( $content, '<' ) + 1 );
- $from_email = str_replace( '>', '', $from_email );
- $from_email = trim( $from_email );
- } else {
- $from_name = '';
- $from_email = trim( $content );
- }
- $message['from_email'] = $from_email;
- $message['from_name'] = $from_name;
- break;
- case 'bcc':
- // TODO: Mandrill's API only accept one BCC address. The other addresses supplied will be silently discarted
- $bcc = array_merge( (array) $bcc, explode( ',', $content ) );
- $message['bcc_address'] = $bcc[0];
- break;
- case 'reply-to':
- $message['headers'][trim( $name )] = trim( $content );
- break;
- default:
- if ( substr($name,0,2) == 'x-' ) {
- $message['headers'][trim( $name )] = trim( $content );
- }
- break;
- }
- }
- }
- }
- // Adding a Reply-To header if needed.
- $reply_to = self::getReplyTo();
- if ( !empty($reply_to) && !in_array( 'reply_to', array_map( 'strtolower', array_keys($message['headers']) ) ) ) {
- $message['headers']['Reply-To'] = trim(self::getReplyTo());
- }
- // Checking the To: field
- if( !is_array($message['to']) ) $message['to'] = explode(',', $message['to']);
- $processed_to = array();
- foreach ( $message['to'] as $email ) {
- $processed_to[] = array( 'email' => $email );
- }
- $message['to'] = $processed_to;
- // Checking the From: field
- if ( empty($message['from_email']) ) $message['from_email'] = self::getFromEmail();
- if ( empty($message['from_name'] ) ) $message['from_name'] = self::getFromName();
- // Checking the tags.
- $message['tags'] = self::findTags($tags);
- // Checking the attachments
- if ( !empty($message['attachments']) ) {
- $message['attachments'] = self::processAttachments($message['attachments']);
- if ( is_wp_error($message['attachments']) ) {
- throw new Exception('Invalid attachment (check http://eepurl.com/nXMa1 for supported file types).');
- }
- }
- // Default values for other parameters
- $message['auto_text'] = true;
- $message['track_opens'] = $track_opens;
- $message['track_clicks']= $track_clicks;
- //DS edits for editable fields.
- if(is_array($message['html'])){
- if ( self::getnl2br() == 1 ){
- //run the line breaker for each array value
- $temp_array = array();
- foreach ($message['html'] as $key => $value){
- $temp_array[$key] = nl2br($value);
- }
- $message['html'] = $temp_array;
- }
- }else{
- // Common transformations for the HTML part
- if ( self::getnl2br() == 1 ) $message['html'] = nl2br($message['html']);
- }
- // Defining template to use
- $template = '';
- // If user specified a given template, check if it is valid for this Mandrill account
- if ( !empty($template_name) && self::isTemplateValid($template_name) ) {
- $template = $template_name;
- } else {
- $template = self::getTemplate(); // If no template was specified or the specified was invalid, use the general one.
- }
- //Check again if its an array
- if(is_array($message['html'])){
- if ( $template ) {
- $template_content = array();
- foreach($message['html'] as $key => $value){
- $template_content[] = array('name' => $key, 'content' => $value);
- }
- $message['html'] = '';
- $message['template']['name'] = $template;
- $message['template']['content'] = $template_content;
- }
- }else {
- //DS use default settings if no array was passed
- if ( $template ) {
- $template_content = array( array('name' => 'main', 'content' => $message['html']) );
- $message['html'] = '';
- $message['template']['name'] = $template;
- $message['template']['content'] = $template_content;
- }
- }
- // Letting the user filter/change the message payload
- if ( defined('WP_DEBUG') && WP_DEBUG !== false ) {
- error_log( "\nwpMandrill::sendEmail: Before mandrill_payload ".print_r(array_replace($message,array('html' => '', 'text' => '', 'template' => '')),true)."\n" );
- }
- $message = apply_filters('mandrill_payload', $message);
- if ( defined('WP_DEBUG') && WP_DEBUG !== false ) {
- error_log( "\nwpMandrill::sendEmail: After mandrill_payload ".print_r(array_replace($message,array('html' => '', 'text' => '', 'template' => '')),true)."\n" );
- }
- // Checking that the final From: email address is valid
- list($dummy,$domain) = explode('@', $message['from_email']);
- if ( !self::isDomainEnabled( $domain ) )
- throw new Exception('The domain of the email you provided ('.$domain.') has not been added to Mandrill yet.');
- // Setting the tags property correctly to be received by the Mandrill's API
- if ( !is_array($message['tags']['user']) ) $message['tags']['user'] = array();
- if ( !is_array($message['tags']['general']) ) $message['tags']['general'] = array();
- if ( !is_array($message['tags']['automatic']) ) $message['tags']['automatic'] = array();
- $message['tags'] = array_merge( $message['tags']['user'], $message['tags']['general'], $message['tags']['automatic'] );
- // Sending the message
- if ( empty($message['template']) || empty($message['template']['name']) || empty($message['template']['content']) ) {
- return self::$mandrill->messages_send($message);
- } else {
- $template = $message['template']['name'];
- $template_content = $message['template']['content'];
- unset($message['template']);
- return self::$mandrill->messages_send_template($template, $template_content, $message);
- }
- } catch ( Exception $e) {
- error_log( "\nwpMandrill::sendEmail: Exception Caught => ".$e->getMessage()."\n" );
- return new WP_Error( $e->getMessage() );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement