Advertisement
Guest User

Mandrill / Wordpress template parts

a guest
Nov 29th, 2012
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.49 KB | None | 0 0
  1. static function sendEmail( $message, $tags = array(), $template_name = '', $track_opens = true, $track_clicks = true ) {
  2.    
  3.         try {
  4.             // Checking if we are connected to Mandrill
  5.             wpMandrill::getConnected();
  6.            
  7.             if ( !self::isConnected() ) throw new Exception('Invalid API Key');
  8.            
  9.             /************
  10.             *
  11.             *  Processing supplied fields to make them valid for the Mandrill API
  12.             *
  13.             *************************/
  14.                        
  15.             // Checking the user-specified headers
  16.                 if ( empty( $message['headers'] ) ) {
  17.                     $message['headers'] = array();
  18.                 } else {
  19.                     if ( !is_array( $message['headers'] ) ) {
  20.                         $tempheaders = explode( "\n", str_replace( "\r\n", "\n", $message['headers'] ) );
  21.                     } else {
  22.                         $tempheaders = $message['headers'];
  23.                     }
  24.                     $message['headers'] = array();
  25.  
  26.                     // If it's actually got contents
  27.                     if ( !empty( $tempheaders ) ) {
  28.                         // Iterate through the raw headers
  29.                         foreach ( (array) $tempheaders as $header ) {
  30.                             if ( strpos($header, ':') === false ) continue;
  31.  
  32.                             // Explode them out
  33.                             list( $name, $content ) = explode( ':', trim( $header ), 2 );
  34.  
  35.                             // Cleanup crew
  36.                             $name    = trim( $name    );
  37.                             $content = trim( $content );
  38.  
  39.                             switch ( strtolower( $name ) ) {
  40.                                 case 'from':
  41.                                     if ( strpos($content, '<' ) !== false ) {
  42.                                         // So... making my life hard again?
  43.                                         $from_name = substr( $content, 0, strpos( $content, '<' ) - 1 );
  44.                                         $from_name = str_replace( '"', '', $from_name );
  45.                                         $from_name = trim( $from_name );
  46.  
  47.                                         $from_email = substr( $content, strpos( $content, '<' ) + 1 );
  48.                                         $from_email = str_replace( '>', '', $from_email );
  49.                                         $from_email = trim( $from_email );
  50.                                     } else {
  51.                                         $from_name  = '';
  52.                                         $from_email = trim( $content );
  53.                                     }
  54.                                     $message['from_email']  = $from_email;
  55.                                     $message['from_name']   = $from_name;                                  
  56.                                     break;
  57.                                    
  58.                                 case 'bcc':
  59.                                     // TODO: Mandrill's API only accept one BCC address. The other addresses supplied will be silently discarted
  60.                                     $bcc = array_merge( (array) $bcc, explode( ',', $content ) );
  61.                                    
  62.                                     $message['bcc_address'] = $bcc[0];
  63.                                     break;
  64.                                    
  65.                                 case 'reply-to':
  66.                                     $message['headers'][trim( $name )] = trim( $content );
  67.                                     break;
  68.                                    
  69.                                 default:
  70.                                     if ( substr($name,0,2) == 'x-' ) {
  71.                                         $message['headers'][trim( $name )] = trim( $content );
  72.                                     }
  73.                                     break;
  74.                             }
  75.                         }
  76.                     }
  77.                 }
  78.                
  79.             // Adding a Reply-To header if needed.
  80.                 $reply_to = self::getReplyTo();
  81.                 if ( !empty($reply_to) && !in_array( 'reply_to', array_map( 'strtolower', array_keys($message['headers']) ) ) ) {
  82.                     $message['headers']['Reply-To'] = trim(self::getReplyTo());
  83.                 }
  84.  
  85.             // Checking the To: field
  86.                 if( !is_array($message['to']) ) $message['to'] = explode(',', $message['to']);
  87.                
  88.                 $processed_to = array();
  89.                 foreach ( $message['to'] as $email ) {
  90.                     $processed_to[] = array( 'email' => $email );
  91.                 }
  92.                 $message['to'] = $processed_to;
  93.            
  94.             // Checking the From: field
  95.                 if ( empty($message['from_email']) ) $message['from_email'] = self::getFromEmail();
  96.                 if ( empty($message['from_name'] ) ) $message['from_name']  = self::getFromName();
  97.            
  98.             // Checking the tags.
  99.                 $message['tags']        = self::findTags($tags);
  100.            
  101.             // Checking the attachments
  102.                 if ( !empty($message['attachments']) ) {
  103.                     $message['attachments'] = self::processAttachments($message['attachments']);
  104.                     if ( is_wp_error($message['attachments']) ) {
  105.                         throw new Exception('Invalid attachment (check http://eepurl.com/nXMa1 for supported file types).');
  106.                     }
  107.                 }
  108.             // Default values for other parameters
  109.                 $message['auto_text']   = true;
  110.                 $message['track_opens'] = $track_opens;
  111.                 $message['track_clicks']= $track_clicks;
  112.                
  113.             //DS edits for editable fields.
  114.                 if(is_array($message['html'])){
  115.                     if ( self::getnl2br() == 1 ){
  116.                         //run the line breaker for each array value
  117.                         $temp_array = array();
  118.                         foreach ($message['html'] as $key => $value){
  119.                             $temp_array[$key] = nl2br($value);
  120.                         }
  121.                         $message['html'] = $temp_array;
  122.                     }
  123.                 }else{    
  124.             // Common transformations for the HTML part
  125.                 if ( self::getnl2br() == 1 ) $message['html'] = nl2br($message['html']);
  126.                 }
  127.             // Defining template to use            
  128.            
  129.                 $template = '';
  130.                 // If user specified a given template, check if it is valid for this Mandrill account
  131.                 if ( !empty($template_name) && self::isTemplateValid($template_name) ) {
  132.                     $template = $template_name;
  133.                 } else {
  134.                     $template  = self::getTemplate();   // If no template was specified or the specified was invalid, use the general one.
  135.                 }
  136.                 //Check again if its an array
  137.                 if(is_array($message['html'])){
  138.                 if ( $template ) {
  139.                     $template_content = array();
  140.                     foreach($message['html'] as $key => $value){
  141.                          $template_content[] = array('name' => $key, 'content' => $value);
  142.                    
  143.                     }
  144.  
  145.                     $message['html']                = '';
  146.                     $message['template']['name']    = $template;
  147.                     $message['template']['content'] = $template_content;
  148.                 }
  149.                
  150.                
  151.                 }else {
  152.                
  153.                     //DS use default settings if no array was passed
  154.                     if ( $template ) {
  155.                        $template_content = array( array('name' => 'main', 'content' => $message['html']) );
  156.  
  157.                        $message['html']                = '';
  158.                       $message['template']['name']    = $template;
  159.                       $message['template']['content'] = $template_content;
  160.                     }
  161.                 }
  162.             // Letting the user filter/change the message payload
  163.                 if ( defined('WP_DEBUG') && WP_DEBUG !== false ) {
  164.                     error_log( "\nwpMandrill::sendEmail: Before mandrill_payload ".print_r(array_replace($message,array('html' => '', 'text' => '', 'template' => '')),true)."\n" );
  165.                 }
  166.                 $message  = apply_filters('mandrill_payload', $message);
  167.                 if ( defined('WP_DEBUG') && WP_DEBUG !== false ) {
  168.                     error_log( "\nwpMandrill::sendEmail: After mandrill_payload ".print_r(array_replace($message,array('html' => '', 'text' => '', 'template' => '')),true)."\n" );
  169.                 }
  170.                
  171.             // Checking that the final From: email address is valid
  172.                 list($dummy,$domain) = explode('@', $message['from_email']);
  173.                 if ( !self::isDomainEnabled( $domain ) )
  174.                     throw new Exception('The domain of the email you provided ('.$domain.') has not been added to Mandrill yet.');
  175.                    
  176.             // Setting the tags property correctly to be received by the Mandrill's API
  177.                 if ( !is_array($message['tags']['user']) )      $message['tags']['user']        = array();
  178.                 if ( !is_array($message['tags']['general']) )   $message['tags']['general']     = array();
  179.                 if ( !is_array($message['tags']['automatic']) ) $message['tags']['automatic']   = array();
  180.                
  181.                 $message['tags'] = array_merge( $message['tags']['user'], $message['tags']['general'], $message['tags']['automatic'] );
  182.                
  183.             // Sending the message
  184.                 if ( empty($message['template'])  || empty($message['template']['name'])  || empty($message['template']['content']) ) {
  185.                     return self::$mandrill->messages_send($message);
  186.                 } else {
  187.                     $template           = $message['template']['name'];
  188.                     $template_content   = $message['template']['content'];                    
  189.                     unset($message['template']);
  190.                    
  191.                     return self::$mandrill->messages_send_template($template, $template_content, $message);
  192.                 }
  193.            
  194.         } catch ( Exception $e) {
  195.             error_log( "\nwpMandrill::sendEmail: Exception Caught => ".$e->getMessage()."\n" );
  196.             return new WP_Error( $e->getMessage() );
  197.         }
  198.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement