示例#1
0
 static function prepare_email()
 {
     // Send the email, etc.
     self::$email_fields['full_message'] = self::$email_msg;
     if ('true' == self::$form_options['email_html']) {
         self::$email_msg = str_replace(array("\r\n", "\r", "\n"), "<br>", self::$email_msg);
         self::$email_msg = '<html><body>' . self::$php_eol . self::$email_msg . '</body></html>' . self::$php_eol;
     }
     // wordwrap email message
     self::$email_msg = wordwrap(self::$email_msg, 70, self::$php_eol);
     // See if the email should be sent
     $email_off = 0;
     if (self::$form_options['redirect_enable'] == 'true' && self::$form_options['redirect_query'] == 'true' && self::$form_options['redirect_email_off'] == 'true') {
         $email_off = 1;
     }
     if (self::$form_options['export_email_off'] == 'true') {
         $email_off = 1;
     }
     if (self::$form_options['silent_send'] != 'off' && self::$form_options['silent_email_off'] == 'true') {
         $email_off = 1;
     }
     $ctf_email_on_this_domain = self::$form_options['email_from'];
     // optional
     // ***** Prepare the email header *****
     // *** From name and email
     $no_name = 0;
     if (empty(self::$email_fields['from_name'])) {
         // if name field was disabled, the from name will be ~ because we don't know the users name.
         self::$email_header['from_name'] = '~';
         self::$email_fields['from_name'] = '~';
         $no_name = 1;
     } else {
         self::$email_header['from_name'] = self::$email_fields['from_name'];
     }
     $no_email = 0;
     if (empty(self::$email_fields['from_email'])) {
         // if email field was disabled, the from email will be admin email because we don't know the users email.
         self::$email_header['from_email'] = get_option('admin_email');
         self::$email_fields['from_email'] = get_option('admin_email');
         $no_email = 1;
     } else {
         self::$email_header['from_email'] = self::$email_fields['from_email'];
     }
     if (!empty($ctf_email_on_this_domain)) {
         // Set the Return-path as specified in settings
         if (!preg_match("/,/", $ctf_email_on_this_domain)) {
             // just an email: user1@example.com
             self::$email_fields['mail_sender'] = $ctf_email_on_this_domain;
             if (self::$form_options['email_from_enforced'] == 'true') {
                 self::$email_header['from_email'] = $ctf_email_on_this_domain;
             }
         } else {
             // name and email: webmaster,user1@example.com
             list($key, $value) = explode(",", $ctf_email_on_this_domain);
             $key = trim($key);
             $value = trim($value);
             self::$email_fields['mail_sender'] = $value;
             if ($no_name) {
                 self::$email_header['from_name'] = $key;
             }
             if ($no_email || self::$form_options['email_from_enforced'] == 'true') {
                 self::$email_header['from_email'] = $value;
             }
         }
     }
     // hook for modifying the from_name and from_email
     self::$email_fields['from_name'] = apply_filters('si_contact_from_name', self::$email_fields['from_name'], self::$form_id_num);
     self::$email_fields['from_email'] = apply_filters('si_contact_from_email', self::$email_fields['from_email'], self::$form_id_num);
     if (!$email_off) {
         if ($no_name && self::$email_header['from_name'] == '~') {
             // they had no name field
             $header_php = 'From: ' . self::$email_header['from_email'] . ' <' . self::$email_header['from_email'] . ">\n";
         } else {
             $header_php = 'From: ' . self::$email_header['from_name'] . ' <' . self::$email_header['from_email'] . ">\n";
         }
         // header for php mail only
         $header = '';
         // for php mail and wp_mail
         // *** To name and email, including cc and bcc
         // process $mail_to user1@example.com,[cc]user2@example.com,[cc]user3@example.com,[bcc]user4@example.com,[bcc]user5@example.com
         // some are cc, some are bcc
         $mail_to_arr = explode(',', self::$email_fields['email_to']);
         self::$email_fields['email_to'] = trim($mail_to_arr[0]);
         unset($mail_to_arr[0]);
         $ctf_email_address_cc = '';
         // XXX add send a copy feature?
         //This is a bit of a hack, but it will send a carbon copy to the sender:
         // $ctf_email_address_cc .= "$email,";
         $ctf_email_address_bcc = self::$form_options['email_bcc'];
         if ($ctf_email_address_bcc != '') {
             $ctf_email_address_bcc = $ctf_email_address_bcc . ',';
         }
         foreach ($mail_to_arr as $key => $this_mail_to) {
             if (preg_match("/\\[bcc\\]/i", $this_mail_to)) {
                 $this_mail_to = str_replace('[bcc]', '', $this_mail_to);
                 $ctf_email_address_bcc .= "{$this_mail_to},";
             } else {
                 $this_mail_to = str_replace('[cc]', '', $this_mail_to);
                 $ctf_email_address_cc .= "{$this_mail_to},";
             }
         }
         if (!empty($ctf_email_address_cc)) {
             $ctf_email_address_cc = rtrim($ctf_email_address_cc, ',');
             $header .= "Cc: {$ctf_email_address_cc}\n";
         }
         if (!empty($ctf_email_address_bcc)) {
             $ctf_email_address_bcc = rtrim($ctf_email_address_bcc, ',');
             $header .= "Bcc: {$ctf_email_address_bcc}\n";
         }
         // *** Reply to and X-Sender
         if (!empty(self::$form_options['email_reply_to'])) {
             // custom reply_to
             $header .= 'Reply-To: ' . self::$form_options['email_reply_to'] . "\n";
         } else {
             $header .= 'Reply-To: ' . self::$email_fields['from_email'] . "\n";
         }
         if ($ctf_email_on_this_domain != '') {
             // Return-path address setting
             $header .= 'X-Sender: ' . self::$email_fields['mail_sender'] . "\n";
             $header .= 'Return-Path: ' . self::$email_fields['mail_sender'] . "\n";
         }
         // *** Email Subject
         // subject can include posted data names feature:
         if (self::$selected_subject != '') {
             $subj = self::$form_options['email_subject'] . ' ' . self::$selected_subject;
         } else {
             if (isset(self::$form_data['subject']) && self::$form_data['subject'] != '') {
                 $subj = self::$form_options['email_subject'] . ' ' . self::$form_data['subject'];
             } else {
                 $subj = self::$form_options['email_subject'];
             }
         }
         // was not required, use the options
         foreach (self::$email_fields as $key => $data) {
             if (in_array($key, array('message', 'full_message', 'akismet'))) {
                 // disallow these
                 continue;
             }
             if (is_string($data)) {
                 $subj = str_replace('[' . $key . ']', $data, $subj);
             }
         }
         $posted_form_name = self::$form_options['form_name'] != '' ? self::$form_options['form_name'] : sprintf(__('Form: %d', 'si-contact-form'), self::$form_id_num);
         $subj = str_replace('[form_label]', $posted_form_name, $subj);
         // filter hook for modifying the email subject(great for adding a ticket number)
         $subj = apply_filters('si_contact_email_subject', $subj, self::$form_id_num);
         if (!empty(self::$akismet_spam_subject)) {
             // Akismet: Spam -
             $subj = self::$akismet_spam_subject . $subj;
         }
         self::$email_fields['subject'] = $subj;
         // ***** Send the email *****
         // Send html email?
         if (self::$form_options['email_html'] == 'true') {
             $header .= 'Content-type: text/html; charset=' . get_option('blog_charset') . self::$php_eol;
         } else {
             $header .= 'Content-type: text/plain; charset=' . get_option('blog_charset') . self::$php_eol;
         }
         // filter hook for modifying the email header
         $header = apply_filters('si_contact_email_header', $header, self::$form_id_num);
         // not sure if this is needed because the From: header is always set
         //@ini_set( 'sendmail_from', self::$email_fields['from_email'] );
         /*  print $header;
              print '----';
              print $header_php;
             exit;*/
         // Check for safe mode
         $safe_mode = (bool) @ini_get('safe_mode') === false ? 0 : 1;
         if (self::$form_options['php_mailer_enable'] == 'php') {
             // sending with php mail
             $header_php .= $header;
             if ($ctf_email_on_this_domain != '' && !$safe_mode) {
                 // Pass the Return-Path via sendmail's -f command.
                 @mail(self::$email_fields['email_to'], self::$email_fields['subject'], self::$email_msg, $header_php, '-f ' . self::$email_fields['mail_sender']);
             } else {
                 // the fifth parameter is not allowed in safe mode, and don't use it if Return-path address not set
                 @mail(self::$email_fields['email_to'], self::$email_fields['subject'], self::$email_msg, $header_php);
             }
         } else {
             // sending with wp_mail
             if ($no_name && self::$email_header['from_name'] == '~') {
                 self::$email_set_wp['from_name'] = self::$email_header['from_email'];
             } else {
                 self::$email_set_wp['from_name'] = self::$email_header['from_name'];
             }
             add_filter('wp_mail_from_name', 'FSCF_Process::set_wp_from_name', 1);
             self::$email_set_wp['from_email'] = self::$email_header['from_email'];
             add_filter('wp_mail_from', 'FSCF_Process::set_wp_from_email', 1);
             if ($ctf_email_on_this_domain != '') {
                 // Add an action on phpmailer_init to add Sender $this->si_contact_mail_sender for Return-path in wp_mail
                 // this helps spf checking when the Sender email address matches the site domain name
                 self::$email_set_wp['mail_sender'] = self::$email_fields['mail_sender'];
                 add_action('phpmailer_init', 'FSCF_Process::set_wp_mail_sender', 1);
             }
             $send_attachments = true;
             //filter hook to disable emailing the attachment (useful for when email_keep_attachments is checked and you don't want it emailed).
             $send_attachments = apply_filters('si_contact_send_attachments', $send_attachments, self::$form_id_num);
             if (self::$uploaded_files && $send_attachments) {
                 $attach_this_mail = array();
                 foreach (self::$uploaded_files as $path) {
                     $attach_this_mail[] = $path;
                 }
                 @wp_mail(self::$email_fields['email_to'], self::$email_fields['subject'], self::$email_msg, $header, $attach_this_mail);
             } else {
                 //echo 'header:'.$header.'<br>'.'to:'.self::$email_fields['email_to'].'<br>'.'subject:'.self::$email_fields['subject'].'<br>'.'message:'.self::$email_msg.'<br>';
                 @wp_mail(self::$email_fields['email_to'], self::$email_fields['subject'], self::$email_msg, $header);
             }
         }
     }
     // end if (!$email_off) {
     // Confirmation email (used to be called "autoresponder")
     if (self::$form_options['auto_respond_enable'] == 'true' && !$no_email && !empty(self::$form_options['auto_respond_subject']) && !empty(self::$form_options['auto_respond_message'])) {
         $subj = self::$form_options['auto_respond_subject'];
         $msg = self::$form_options['auto_respond_message'];
         // self::$email_fields is an array of the form name value pairs
         // autoresponder can include posted data, tags are set on form settings page
         foreach (self::$email_fields as $key => $data) {
             if (in_array($key, array('message', 'full_message', 'akismet'))) {
                 // disallow these
                 continue;
             }
             if (is_string($data)) {
                 $subj = str_replace('[' . $key . ']', $data, $subj);
                 $msg = str_replace('[' . $key . ']', $data, $msg);
             }
         }
         $subj = str_replace('[form_label]', $posted_form_name, $subj);
         // Remove all empty field tags unmatched in posted data (for the fields not required)
         self::set_tags_array();
         foreach (self::$av_tags_arr as $i) {
             $msg = str_replace('[' . $i . "]\r\n", '', $msg);
             $msg = str_replace('[' . $i . ']', '', $msg);
         }
         foreach (self::$av_tags_subj_arr as $i) {
             $subj = str_replace('[' . $i . ']', '', $subj);
         }
         // wordwrap email message
         $msg = wordwrap($msg, 70, self::$php_eol);
         $header = '';
         $header_php = '';
         // Prepare the email header
         $header_php = 'From: ' . self::$form_options['auto_respond_from_name'] . ' <' . self::$form_options['auto_respond_from_email'] . ">\n";
         $header .= 'Reply-To: ' . self::$form_options['auto_respond_reply_to'] . "\n";
         //	$header .= 'X-Sender: ' . self::$form_options['auto_respond_from_email'] . "\n";
         //	$header .= 'Return-Path: ' . self::$form_options['auto_respond_from_email'] . "\n";
         if ($ctf_email_on_this_domain != '') {
             //  Return-path address setting
             $header .= 'X-Sender: ' . self::$email_fields['mail_sender'] . "\n";
             $header .= 'Return-Path: ' . self::$email_fields['mail_sender'] . "\n";
         }
         if (self::$form_options['auto_respond_html'] == 'true') {
             $header .= 'Content-type: text/html; charset=' . get_option('blog_charset') . self::$php_eol;
         } else {
             $header .= 'Content-type: text/plain; charset=' . get_option('blog_charset') . self::$php_eol;
         }
         // XXX some of this duplicate code.  someday could make into send_email function
         // XXX 09/01/2013 not sure if this is needed because the From: header is always set
         //@ini_set( 'sendmail_from', self::$form_options['auto_respond_from_email'] );
         if (self::$form_options['php_mailer_enable'] == 'php') {
             // autoresponder sending with php
             $header_php .= $header;
             if ($ctf_email_on_this_domain != '' && !$safe_mode) {
                 // Pass the Return-Path via sendmail's -f command.
                 @mail(self::$email_fields['from_email'], $subj, $msg, $header_php, '-f ' . self::$email_fields['mail_sender']);
             } else {
                 // the fifth parameter is not allowed in safe mode, and don't use it if Return-path address not set
                 @mail(self::$email_fields['from_email'], $subj, $msg, $header_php);
             }
         } else {
             // autoresponder sending with wp_mail
             self::$email_set_wp['from_name'] = self::$form_options['auto_respond_from_name'];
             add_filter('wp_mail_from_name', 'FSCF_Process::set_wp_from_name', 1);
             self::$email_set_wp['from_email'] = self::$form_options['auto_respond_from_email'];
             add_filter('wp_mail_from', 'FSCF_Process::set_wp_from_email', 1);
             @wp_mail(self::$email_fields['from_email'], $subj, $msg, $header);
         }
     }
     // end if confirmation email (autoresponder)
     // added optional condition for silent send
     $silent_ok = 1;
     if (!empty(self::$form_options['silent_conditional_field']) && !empty(self::$form_options['silent_conditional_value'])) {
         if (isset(self::$email_fields[self::$form_options['silent_conditional_field']]) && self::$email_fields[self::$form_options['silent_conditional_field']] == self::$form_options['silent_conditional_value']) {
             $silent_ok = 1;
         } else {
             $silent_ok = 0;
         }
     }
     // filter hook for modifying the email_fields array
     self::$email_fields = apply_filters('si_contact_email_fields_posted', self::$email_fields, self::$form_id_num);
     // Silent sending?
     if (self::$form_options['silent_send'] == 'get' && !empty(self::$form_options['silent_url']) && $silent_ok) {
         // build query string
         $query_string = self::export_convert(self::$email_fields, self::$form_options['silent_rename'], self::$form_options['silent_ignore'], self::$form_options['silent_add'], 'query');
         //echo $query_string;
         if (!preg_match("/\\?/", self::$form_options['silent_url'])) {
             $silent_result = wp_remote_get(self::$form_options['silent_url'] . '?' . $query_string, array('timeout' => 20, 'sslverify' => false));
         } else {
             $silent_result = wp_remote_get(self::$form_options['silent_url'] . '&' . $query_string, array('timeout' => 20, 'sslverify' => false));
         }
         if (!is_wp_error($silent_result)) {
             $silent_result = wp_remote_retrieve_body($silent_result);
         }
         //echo $silent_result;
     }
     if (self::$form_options['silent_send'] == 'post' && !empty(self::$form_options['silent_url']) && $silent_ok) {
         // build post_array
         $post_array = self::export_convert(self::$email_fields, self::$form_options['silent_rename'], self::$form_options['silent_ignore'], self::$form_options['silent_add'], 'array');
         $silent_result = wp_remote_post(self::$form_options['silent_url'], array('body' => $post_array, 'timeout' => 20, 'sslverify' => false));
         if (!is_wp_error($silent_result)) {
             $silent_result = wp_remote_retrieve_body($silent_result);
         }
         //echo $silent_result;
     }
     // Export option
     // filter posted data based on admin settings
     $posted_data_export = self::export_convert(self::$email_fields, self::$form_options['export_rename'], self::$form_options['export_ignore'], self::$form_options['export_add'], 'array');
     // hook for other plugins to use (just after message posted)
     $fsctf_posted_data = (object) array('form_number' => self::$form_id_num, 'title' => self::$form_options['form_name'], 'posted_data' => $posted_data_export, 'uploaded_files' => (array) self::$uploaded_files);
     do_action_ref_array('fsctf_mail_sent', array(&$fsctf_posted_data));
 }