NF_Actions_Email

Class NF_Action_Email.

Defined (1)

The class is defined in the following location(s).

/includes/Actions/Email.php  
  1. final class NF_Actions_Email extends NF_Abstracts_Action 
  2. /** 
  3. * @var string 
  4. */ 
  5. protected $_name = 'email'; 
  6.  
  7. /** 
  8. * @var array 
  9. */ 
  10. protected $_tags = array(); 
  11.  
  12. /** 
  13. * @var string 
  14. */ 
  15. protected $_timing = 'late'; 
  16.  
  17. /** 
  18. * @var int 
  19. */ 
  20. protected $_priority = 10; 
  21.  
  22. /** 
  23. * Constructor 
  24. */ 
  25. public function __construct() 
  26. parent::__construct(); 
  27.  
  28. $this->_nicename = __( 'Email', 'ninja-forms' ); 
  29.  
  30. $settings = Ninja_Forms::config( 'ActionEmailSettings' ); 
  31.  
  32. $this->_settings = array_merge( $this->_settings, $settings ); 
  33.  
  34. $this->_backwards_compatibility(); 
  35.  
  36. /** 
  37. * PUBLIC METHODS 
  38. */ 
  39.  
  40. public function process( $action_settings, $form_id, $data ) 
  41. $headers = $this->_get_headers( $action_settings ); 
  42.  
  43. $attachments = $this->_get_attachments( $action_settings, $data ); 
  44.  
  45. if( 'html' == $action_settings[ 'email_format' ] ) { 
  46. $message = $action_settings['email_message']; 
  47. } else { 
  48. $message = $this->format_plain_text_message( $action_settings[ 'email_message_plain' ] ); 
  49.  
  50. $sent = wp_mail( 
  51. $action_settings['to'],  
  52. $action_settings['email_subject'],  
  53. $message,  
  54. $headers,  
  55. $attachments 
  56. ); 
  57.  
  58. $data[ 'actions' ][ 'email' ][ 'to' ] = $action_settings['to']; 
  59. $data[ 'actions' ][ 'email' ][ 'sent' ] = $sent; 
  60. $data[ 'actions' ][ 'email' ][ 'headers' ] = $headers; 
  61. $data[ 'actions' ][ 'email' ][ 'attachments' ] = $attachments; 
  62.  
  63. return $data; 
  64.  
  65. private function _get_headers( $settings ) 
  66. $headers = array(); 
  67.  
  68. $headers[] = 'Content-Type: text/' . $settings[ 'email_format' ]; 
  69. $headers[] = 'charset=UTF-8'; 
  70.  
  71. $headers[] = $this->_format_from( $settings ); 
  72.  
  73. $headers = array_merge( $headers, $this->_format_recipients( $settings ) ); 
  74.  
  75. return $headers; 
  76.  
  77. private function _get_attachments( $settings, $data ) 
  78. $attachments = array(); 
  79.  
  80. if( 1 == $settings[ 'attach_csv' ] ) { 
  81. $attachments[] = $this->_create_csv( $data[ 'fields' ] ); 
  82.  
  83. if( ! isset( $settings[ 'id' ] ) ) $settings[ 'id' ] = ''; 
  84.  
  85. $attachments = apply_filters( 'ninja_forms_action_email_attachments', $attachments, $data, $settings ); 
  86.  
  87. return $attachments; 
  88.  
  89. private function _format_from( $settings ) 
  90. $from_name = get_bloginfo( 'name', 'raw' ); 
  91. $from_name = apply_filters( 'ninja_forms_action_email_from_name', $from_name ); 
  92. $from_name = ( $settings[ 'from_name' ] ) ? $settings[ 'from_name' ] : $from_name; 
  93.  
  94. $from_address = get_bloginfo( 'admin_email' ); 
  95. $from_address = apply_filters( 'ninja_forms_action_email_from_address', $from_address ); 
  96. $from_address = ( $settings[ 'from_address' ] ) ? $settings[ 'from_address' ] : $from_address; 
  97.  
  98. return $this->_format_recipient( 'from', $from_address, $from_name ); 
  99.  
  100. private function _format_recipients( $settings ) 
  101. $headers = array(); 
  102.  
  103. $recipient_settings = array( 
  104. 'Cc' => $settings[ 'cc' ],  
  105. 'Bcc' => $settings[ 'bcc' ],  
  106. 'Reply-to' => $settings[ 'reply_to' ],  
  107. ); 
  108.  
  109. foreach( $recipient_settings as $type => $emails ) { 
  110.  
  111. $emails = explode( ', ', $emails ); 
  112.  
  113. foreach( $emails as $email ) { 
  114.  
  115. if( ! $email ) continue; 
  116.  
  117. $headers[] = $this->_format_recipient($type, $email); 
  118.  
  119. return $headers; 
  120.  
  121. private function _format_recipient( $type, $email, $name = '' ) 
  122. $type = ucfirst( $type ); 
  123.  
  124. if( ! $name ) $name = $email; 
  125.  
  126. $recipient = "$type: $name <$email>"; 
  127.  
  128. return $recipient; 
  129.  
  130. private function _create_csv( $fields ) 
  131. $csv_array = array(); 
  132.  
  133. foreach( $fields as $field ) { 
  134.  
  135. if( ! isset( $field[ 'label' ] ) ) continue; 
  136.  
  137. $csv_array[ 0 ][] = $field[ 'label' ]; 
  138. $csv_array[ 1 ][] = WPN_Helper::stripslashes( $field[ 'value' ] ); 
  139.  
  140. $csv_content = WPN_Helper::str_putcsv( $csv_array,  
  141. apply_filters( 'ninja_forms_sub_csv_delimiter', ', ' ),  
  142. apply_filters( 'ninja_forms_sub_csv_enclosure', '"' ),  
  143. apply_filters( 'ninja_forms_sub_csv_terminator', "\n" ) 
  144. ); 
  145.  
  146. $upload_dir = wp_upload_dir(); 
  147. $path = trailingslashit( $upload_dir['path'] ); 
  148.  
  149. // create temporary file 
  150. $path = tempnam( $path, 'Sub' ); 
  151. $temp_file = fopen( $path, 'r+' ); 
  152.  
  153. // write to temp file 
  154. fwrite( $temp_file, $csv_content ); 
  155. fclose( $temp_file ); 
  156.  
  157. // find the directory we will be using for the final file 
  158. $path = pathinfo( $path ); 
  159. $dir = $path['dirname']; 
  160. $basename = $path['basename']; 
  161.  
  162. // create name for file 
  163. $new_name = apply_filters( 'ninja_forms_submission_csv_name', 'ninja-forms-submission' ); 
  164.  
  165. // remove a file if it already exists 
  166. if( file_exists( $dir.'/'.$new_name.'.csv' ) ) { 
  167. unlink( $dir.'/'.$new_name.'.csv' ); 
  168.  
  169. // move file 
  170. rename( $dir.'/'.$basename, $dir.'/'.$new_name.'.csv' ); 
  171. return $dir.'/'.$new_name.'.csv'; 
  172.  
  173. /** 
  174. * Backwards Compatibility 
  175. */ 
  176.  
  177. private function _backwards_compatibility() 
  178. add_filter( 'ninja_forms_sub_csv_delimiter', array( $this, 'ninja_forms_sub_csv_delimiter' ), 10, 1 ); 
  179. add_filter( 'ninja_sub_csv_enclosure', array( $this, 'ninja_sub_csv_enclosure' ), 10, 1 ); 
  180. add_filter( 'ninja_sub_csv_terminator', array( $this, 'ninja_sub_csv_terminator' ), 10, 1 ); 
  181. add_filter( 'ninja_forms_action_email_attachments', array( $this, 'ninja_forms_action_email_attachments' ), 10, 3 ); 
  182.  
  183. public function ninja_forms_sub_csv_delimiter( $delimiter ) 
  184. return apply_filters( 'nf_sub_csv_delimiter', $delimiter ); 
  185.  
  186. public function ninja_sub_csv_enclosure( $enclosure ) 
  187. return apply_filters( 'nf_sub_csv_enclosure', $enclosure ); 
  188.  
  189. public function ninja_sub_csv_terminator( $terminator ) 
  190. return apply_filters( 'nf_sub_csv_terminator', $terminator ); 
  191.  
  192. public function ninja_forms_action_email_attachments( $attachments, $form_data, $action_settings ) 
  193. return apply_filters( 'nf_email_notification_attachments', $attachments, $action_settings[ 'id' ] ); 
  194.  
  195. private function format_plain_text_message( $message ) 
  196. $message = str_replace( array( '<table>', '</table>', '<tr><td>', '' ), '', $message ); 
  197. $message = str_replace( '</td><td>', ' ', $message ); 
  198. $message = str_replace( '</td></tr>', "\r\n", $message ); 
  199. return strip_tags( $message );