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. $errors = $this->check_for_errors( $action_settings ); 
  42.  
  43. $headers = $this->_get_headers( $action_settings ); 
  44.  
  45. if ( has_filter( 'ninja_forms_get_fields_sorted' ) ) { 
  46. $fields_by_key = array(); 
  47. foreach( $data[ 'fields' ] as $field ) { 
  48. if( is_null( $field ) ) continue; 
  49. if( is_array( $field ) ) { 
  50. if( ! isset( $field[ 'key' ] ) ) continue; 
  51. $key = $field[ 'key' ]; 
  52. } else { 
  53. $key = $field->get_setting('key'); 
  54. $fields_by_key[ $key ] = $field; 
  55. $data[ 'fields' ] = apply_filters( 'ninja_forms_get_fields_sorted', array(), $data[ 'fields' ], $fields_by_key, $form_id ); 
  56.  
  57. $attachments = $this->_get_attachments( $action_settings, $data ); 
  58.  
  59. if( 'html' == $action_settings[ 'email_format' ] ) { 
  60. $message = $action_settings['email_message']; 
  61. } else { 
  62. $message = $this->format_plain_text_message( $action_settings[ 'email_message_plain' ] ); 
  63.  
  64. $message = apply_filters( 'ninja_forms_action_email_message', $message, $data, $action_settings ); 
  65.  
  66. try { 
  67. $sent = wp_mail($action_settings['to'], $action_settings['email_subject'], $message, $headers, $attachments); 
  68. } catch ( Exception $e ) { 
  69. $sent = false; 
  70. $errors[ 'email_sent' ] = $e->getMessage(); 
  71.  
  72. $data[ 'actions' ][ 'email' ][ 'to' ] = $action_settings['to']; 
  73. $data[ 'actions' ][ 'email' ][ 'sent' ] = $sent; 
  74. $data[ 'actions' ][ 'email' ][ 'headers' ] = $headers; 
  75. $data[ 'actions' ][ 'email' ][ 'attachments' ] = $attachments; 
  76.  
  77. if( $errors ) { 
  78. $data[ 'errors' ][ 'form' ] = $errors; 
  79.  
  80. if ( ! empty( $attachments ) ) { 
  81. $this->_drop_csv(); 
  82.  
  83. return $data; 
  84.  
  85. protected function check_for_errors( $action_settings ) 
  86. $errors = array(); 
  87.  
  88. $email_address_settings = array( 'to', 'from_address', 'reply_to', 'cc', 'bcc' ); 
  89.  
  90. foreach( $email_address_settings as $setting ) { 
  91. if( ! isset( $action_settings[ $setting ] ) ) continue; 
  92. if( ! $action_settings[ $setting ] ) continue; 
  93.  
  94.  
  95. $email_addresses = is_array( $action_settings[ $setting ] ) ? $action_settings[ $setting ] : explode( ', ', $action_settings[ $setting ] ); 
  96. foreach( (array) $email_addresses as $email ) { 
  97. $email = trim( $email ); 
  98. if ( false !== strpos( $email, '<' ) && false !== strpos( $email, '>' ) ) { 
  99. preg_match('/(?<=<).*?(?=>)/', $email, $email); 
  100. $email = $email[ 0 ]; 
  101. if( ! is_email( $email ) ) { 
  102. $errors[ 'email_' . $email ] = sprintf( __( 'Your email action "%s" has an invalid value for the "%s" setting. Please check this setting and try again.', 'ninja-forms'), $action_settings[ 'label' ], $setting ); 
  103.  
  104. return $errors; 
  105.  
  106. private function _get_headers( $settings ) 
  107. $headers = array(); 
  108.  
  109. $headers[] = 'Content-Type: text/' . $settings[ 'email_format' ]; 
  110. $headers[] = 'charset=UTF-8'; 
  111.  
  112. $headers[] = $this->_format_from( $settings ); 
  113.  
  114. $headers = array_merge( $headers, $this->_format_recipients( $settings ) ); 
  115.  
  116. return $headers; 
  117.  
  118. private function _get_attachments( $settings, $data ) 
  119. $attachments = array(); 
  120.  
  121. if( 1 == $settings[ 'attach_csv' ] ) { 
  122. $attachments[] = $this->_create_csv( $data[ 'fields' ] ); 
  123.  
  124. if( ! isset( $settings[ 'id' ] ) ) $settings[ 'id' ] = ''; 
  125.  
  126. $attachments = apply_filters( 'ninja_forms_action_email_attachments', $attachments, $data, $settings ); 
  127.  
  128. return $attachments; 
  129.  
  130. private function _format_from( $settings ) 
  131. $from_name = get_bloginfo( 'name', 'raw' ); 
  132. $from_name = apply_filters( 'ninja_forms_action_email_from_name', $from_name ); 
  133. $from_name = ( $settings[ 'from_name' ] ) ? $settings[ 'from_name' ] : $from_name; 
  134.  
  135. $from_address = get_bloginfo( 'admin_email' ); 
  136. $from_address = apply_filters( 'ninja_forms_action_email_from_address', $from_address ); 
  137. $from_address = ( $settings[ 'from_address' ] ) ? $settings[ 'from_address' ] : $from_address; 
  138.  
  139. return $this->_format_recipient( 'from', $from_address, $from_name ); 
  140.  
  141. private function _format_recipients( $settings ) 
  142. $headers = array(); 
  143.  
  144. $recipient_settings = array( 
  145. 'Cc' => $settings[ 'cc' ],  
  146. 'Bcc' => $settings[ 'bcc' ],  
  147. 'Reply-to' => $settings[ 'reply_to' ],  
  148. ); 
  149.  
  150. foreach( $recipient_settings as $type => $emails ) { 
  151.  
  152. $emails = explode( ', ', $emails ); 
  153.  
  154. foreach( $emails as $email ) { 
  155.  
  156. if( ! $email ) continue; 
  157.  
  158. $matches = array(); 
  159. if (preg_match('/^"?(?<name>[^<"]+)"? <(?<email>[^>]+)>$/', $email, $matches)) { 
  160. $headers[] = $this->_format_recipient($type, $matches['email'], $matches['name']); 
  161. } else { 
  162. $headers[] = $this->_format_recipient($type, $email); 
  163.  
  164. return $headers; 
  165.  
  166. private function _format_recipient( $type, $email, $name = '' ) 
  167. $type = ucfirst( $type ); 
  168.  
  169. if( ! $name ) $name = $email; 
  170.  
  171. $recipient = "$type: $name <$email>"; 
  172.  
  173. return $recipient; 
  174.  
  175. private function _create_csv( $fields ) 
  176. $csv_array = array(); 
  177.  
  178. // Get our current date. 
  179. $date_format = Ninja_Forms()->get_setting( 'date_format' ); 
  180. $today = date( $date_format, current_time( 'timestamp' ) ); 
  181. $csv_array[ 0 ][] = 'Date Submitted'; 
  182. $csv_array[ 1 ][] = $today; 
  183.  
  184. foreach( $fields as $field ) { 
  185.  
  186. $ignore = array( 
  187. 'hr',  
  188. 'submit',  
  189. 'html' 
  190. ); 
  191.  
  192. $ignore = apply_filters( 'ninja_forms_csv_ignore_fields', $ignore ); 
  193.  
  194. if( ! isset( $field[ 'label' ] ) ) continue; 
  195. if( in_array( $field[ 'type' ], $ignore ) ) continue; 
  196.  
  197. $label = ( '' != $field[ 'admin_label' ] ) ? $field[ 'admin_label' ] : $field[ 'label' ]; 
  198.  
  199. $value = WPN_Helper::stripslashes( $field[ 'value' ] ); 
  200. if ( empty( $value ) ) { 
  201. $value = ''; 
  202. if ( is_array( $value ) ) { 
  203. $value = implode( ', ', $value ); 
  204.  
  205. $csv_array[ 0 ][] = $label; 
  206. $csv_array[ 1 ][] = $value; 
  207.  
  208. $csv_content = WPN_Helper::str_putcsv( $csv_array,  
  209. apply_filters( 'ninja_forms_sub_csv_delimiter', ', ' ),  
  210. apply_filters( 'ninja_forms_sub_csv_enclosure', '"' ),  
  211. apply_filters( 'ninja_forms_sub_csv_terminator', "\n" ) 
  212. ); 
  213.  
  214. $upload_dir = wp_upload_dir(); 
  215. $path = trailingslashit( $upload_dir['path'] ); 
  216.  
  217. // create temporary file 
  218. $path = tempnam( $path, 'Sub' ); 
  219. $temp_file = fopen( $path, 'r+' ); 
  220.  
  221. // write to temp file 
  222. fwrite( $temp_file, $csv_content ); 
  223. fclose( $temp_file ); 
  224.  
  225. // find the directory we will be using for the final file 
  226. $path = pathinfo( $path ); 
  227. $dir = $path['dirname']; 
  228. $basename = $path['basename']; 
  229.  
  230. // create name for file 
  231. $new_name = apply_filters( 'ninja_forms_submission_csv_name', 'ninja-forms-submission' ); 
  232.  
  233. // remove a file if it already exists 
  234. if( file_exists( $dir.'/'.$new_name.'.csv' ) ) { 
  235. unlink( $dir.'/'.$new_name.'.csv' ); 
  236.  
  237. // move file 
  238. rename( $dir.'/'.$basename, $dir.'/'.$new_name.'.csv' ); 
  239. return $dir.'/'.$new_name.'.csv'; 
  240.  
  241. /** 
  242. * Function to delete csv file from temp directory after Email Action has completed. 
  243. */ 
  244. private function _drop_csv() 
  245. $upload_dir = wp_upload_dir(); 
  246. $path = trailingslashit( $upload_dir['path'] ); 
  247.  
  248. // create name for file 
  249. $new_name = apply_filters( 'ninja_forms_submission_csv_name', 'ninja-forms-submission' ); 
  250.  
  251. // remove a file if it already exists 
  252. if( file_exists( $path.'/'.$new_name.'.csv' ) ) { 
  253. unlink( $path.'/'.$new_name.'.csv' ); 
  254.  
  255. /** 
  256. * Backwards Compatibility 
  257. */ 
  258.  
  259. private function _backwards_compatibility() 
  260. add_filter( 'ninja_forms_sub_csv_delimiter', array( $this, 'ninja_forms_sub_csv_delimiter' ), 10, 1 ); 
  261. add_filter( 'ninja_sub_csv_enclosure', array( $this, 'ninja_sub_csv_enclosure' ), 10, 1 ); 
  262. add_filter( 'ninja_sub_csv_terminator', array( $this, 'ninja_sub_csv_terminator' ), 10, 1 ); 
  263. add_filter( 'ninja_forms_action_email_attachments', array( $this, 'ninja_forms_action_email_attachments' ), 10, 3 ); 
  264.  
  265. public function ninja_forms_sub_csv_delimiter( $delimiter ) 
  266. return apply_filters( 'nf_sub_csv_delimiter', $delimiter ); 
  267.  
  268. public function ninja_sub_csv_enclosure( $enclosure ) 
  269. return apply_filters( 'nf_sub_csv_enclosure', $enclosure ); 
  270.  
  271. public function ninja_sub_csv_terminator( $terminator ) 
  272. return apply_filters( 'nf_sub_csv_terminator', $terminator ); 
  273.  
  274. public function ninja_forms_action_email_attachments( $attachments, $form_data, $action_settings ) 
  275. return apply_filters( 'nf_email_notification_attachments', $attachments, $action_settings[ 'id' ] ); 
  276.  
  277. private function format_plain_text_message( $message ) 
  278. $message = str_replace( array( '<table>', '</table>', '<tr><td>', '' ), '', $message ); 
  279. $message = str_replace( '</td><td>', ' ', $message ); 
  280. $message = str_replace( '</td></tr>', "\r\n", $message ); 
  281. return strip_tags( $message );