WPCF7_Mail

The Contact Form 7 WPCF7 Mail class.

Defined (1)

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

/includes/mail.php  
  1. class WPCF7_Mail { 
  2.  
  3. private static $current = null; 
  4.  
  5. private $name = ''; 
  6. private $template = array(); 
  7. private $use_html = false; 
  8. private $exclude_blank = false; 
  9.  
  10. public static function get_current() { 
  11. return self::$current; 
  12.  
  13. public static function send( $template, $name = '' ) { 
  14. self::$current = new self( $name, $template ); 
  15. return self::$current->compose(); 
  16.  
  17. private function __construct( $name, $template ) { 
  18. $this->name = trim( $name ); 
  19. $this->use_html = ! empty( $template['use_html'] ); 
  20. $this->exclude_blank = ! empty( $template['exclude_blank'] ); 
  21.  
  22. $this->template = wp_parse_args( $template, array( 
  23. 'subject' => '',  
  24. 'sender' => '',  
  25. 'body' => '',  
  26. 'recipient' => '',  
  27. 'additional_headers' => '',  
  28. 'attachments' => '',  
  29. ) ); 
  30.  
  31. public function name() { 
  32. return $this->name; 
  33.  
  34. public function get( $component, $replace_tags = false ) { 
  35. $use_html = ( $this->use_html && 'body' == $component ); 
  36. $exclude_blank = ( $this->exclude_blank && 'body' == $component ); 
  37.  
  38. $template = $this->template; 
  39. $component = isset( $template[$component] ) ? $template[$component] : ''; 
  40.  
  41. if ( $replace_tags ) { 
  42. $component = $this->replace_tags( $component, array( 
  43. 'html' => $use_html,  
  44. 'exclude_blank' => $exclude_blank,  
  45. ) ); 
  46.  
  47. if ( $use_html 
  48. && ! preg_match( '%<html[>\s].*</html>%is', $component ) ) { 
  49. $component = $this->htmlize( $component ); 
  50.  
  51. return $component; 
  52.  
  53. private function htmlize( $body ) { 
  54. $header = apply_filters( 'wpcf7_mail_html_header',  
  55. '<!doctype html> 
  56. <html xmlns="http://www.w3.org/1999/xhtml"> 
  57. <head> 
  58. <title>' . esc_html( $this->get( 'subject', true ) ) . '</title> 
  59. </head> 
  60. <body> 
  61. ', $this ); 
  62.  
  63. $footer = apply_filters( 'wpcf7_mail_html_footer',  
  64. '</body> 
  65. </html>', $this ); 
  66.  
  67. $html = $header . wpautop( $body ) . $footer; 
  68. return $html; 
  69.  
  70. private function compose( $send = true ) { 
  71. $components = array( 
  72. 'subject' => $this->get( 'subject', true ),  
  73. 'sender' => $this->get( 'sender', true ),  
  74. 'body' => $this->get( 'body', true ),  
  75. 'recipient' => $this->get( 'recipient', true ),  
  76. 'additional_headers' => $this->get( 'additional_headers', true ),  
  77. 'attachments' => $this->attachments(),  
  78. ); 
  79.  
  80. $components = apply_filters( 'wpcf7_mail_components',  
  81. $components, wpcf7_get_current_contact_form(), $this ); 
  82.  
  83. if ( ! $send ) { 
  84. return $components; 
  85.  
  86. $subject = wpcf7_strip_newline( $components['subject'] ); 
  87. $sender = wpcf7_strip_newline( $components['sender'] ); 
  88. $recipient = wpcf7_strip_newline( $components['recipient'] ); 
  89. $body = $components['body']; 
  90. $additional_headers = trim( $components['additional_headers'] ); 
  91. $attachments = $components['attachments']; 
  92.  
  93. $headers = "From: $sender\n"; 
  94.  
  95. if ( $this->use_html ) { 
  96. $headers .= "Content-Type: text/html\n"; 
  97. $headers .= "X-WPCF7-Content-Type: text/html\n"; 
  98. } else { 
  99. $headers .= "X-WPCF7-Content-Type: text/plain\n"; 
  100.  
  101. if ( $additional_headers ) { 
  102. $headers .= $additional_headers . "\n"; 
  103.  
  104. return wp_mail( $recipient, $subject, $body, $headers, $attachments ); 
  105.  
  106. public function replace_tags( $content, $args = '' ) { 
  107. if ( true === $args ) { 
  108. $args = array( 'html' => true ); 
  109.  
  110. $args = wp_parse_args( $args, array( 
  111. 'html' => false,  
  112. 'exclude_blank' => false,  
  113. ) ); 
  114.  
  115. return wpcf7_mail_replace_tags( $content, $args ); 
  116.  
  117. private function attachments( $template = null ) { 
  118. if ( ! $template ) { 
  119. $template = $this->get( 'attachments' ); 
  120.  
  121. $attachments = array(); 
  122.  
  123. if ( $submission = WPCF7_Submission::get_instance() ) { 
  124. $uploaded_files = $submission->uploaded_files(); 
  125.  
  126. foreach ( (array) $uploaded_files as $name => $path ) { 
  127. if ( false !== strpos( $template, "[${name}]" ) 
  128. && ! empty( $path ) ) { 
  129. $attachments[] = $path; 
  130.  
  131. foreach ( explode( "\n", $template ) as $line ) { 
  132. $line = trim( $line ); 
  133.  
  134. if ( '[' == substr( $line, 0, 1 ) ) { 
  135. continue; 
  136.  
  137. $path = path_join( WP_CONTENT_DIR, $line ); 
  138.  
  139. if ( @is_readable( $path ) && @is_file( $path ) ) { 
  140. $attachments[] = $path; 
  141.  
  142. return $attachments;