Exemplo n.º 1
0
 public static function trigger_email($action, $entry, $form)
 {
     if (defined('WP_IMPORTING') && WP_IMPORTING) {
         return;
     }
     global $wpdb;
     $notification = $action->post_content;
     $email_key = $action->ID;
     // Set the subject
     if (empty($notification['email_subject'])) {
         $notification['email_subject'] = sprintf(__('%1$s Form submitted on %2$s', 'formidable'), $form->name, '[sitename]');
     }
     $plain_text = $notification['plain_text'] ? true : false;
     //Filter these fields
     $filter_fields = array('email_to', 'cc', 'bcc', 'reply_to', 'from', 'email_subject', 'email_message');
     add_filter('frm_plain_text_email', $plain_text ? '__return_true' : '__return_false');
     //Get all values in entry in order to get User ID field ID
     $values = FrmEntryMeta::getAll(array('it.field_id !' => 0, 'it.item_id' => $entry->id), ' ORDER BY fi.field_order');
     $user_id_field = $user_id_key = '';
     foreach ($values as $value) {
         if ($value->field_type == 'user_id') {
             $user_id_field = $value->field_id;
             $user_id_key = $value->field_key;
             break;
         }
         unset($value);
     }
     //Filter and prepare the email fields
     foreach ($filter_fields as $f) {
         //Don't allow empty From
         if ($f == 'from' && empty($notification[$f])) {
             $notification[$f] = '[admin_email]';
         } else {
             if (in_array($f, array('email_to', 'cc', 'bcc', 'reply_to', 'from'))) {
                 //Remove brackets
                 //Add a space in case there isn't one
                 $notification[$f] = str_replace('<', ' ', $notification[$f]);
                 $notification[$f] = str_replace(array('"', '>'), '', $notification[$f]);
                 //Switch userID shortcode to email address
                 if (strpos($notification[$f], '[' . $user_id_field . ']') !== false || strpos($notification[$f], '[' . $user_id_key . ']') !== false) {
                     $user_data = get_userdata($entry->metas[$user_id_field]);
                     $user_email = $user_data->user_email;
                     $notification[$f] = str_replace(array('[' . $user_id_field . ']', '[' . $user_id_key . ']'), $user_email, $notification[$f]);
                 }
             }
         }
         $notification[$f] = FrmFieldsHelper::basic_replace_shortcodes($notification[$f], $form, $entry);
     }
     //Put recipients, cc, and bcc into an array if they aren't empty
     $to_emails = self::explode_emails($notification['email_to']);
     $cc = self::explode_emails($notification['cc']);
     $bcc = self::explode_emails($notification['bcc']);
     $to_emails = apply_filters('frm_to_email', $to_emails, $values, $form->id, compact('email_key', 'entry', 'form'));
     // Stop now if there aren't any recipients
     if (empty($to_emails) && empty($cc) && empty($bcc)) {
         return;
     }
     $to_emails = array_unique((array) $to_emails);
     $prev_mail_body = $mail_body = $notification['email_message'];
     $mail_body = FrmEntriesHelper::replace_default_message($mail_body, array('id' => $entry->id, 'entry' => $entry, 'plain_text' => $plain_text, 'user_info' => isset($notification['inc_user_info']) ? $notification['inc_user_info'] : false));
     // Add the user info if it isn't already included
     if ($notification['inc_user_info'] && $prev_mail_body == $mail_body) {
         $data = maybe_unserialize($entry->description);
         $mail_body .= "\r\n\r\n" . __('User Information', 'formidable') . "\r\n";
         $mail_body .= __('IP Address', 'formidable') . ': ' . $entry->ip . "\r\n";
         $mail_body .= __('User-Agent (Browser/OS)', 'formidable') . ': ' . FrmEntryFormat::get_browser($data['browser']) . "\r\n";
         $mail_body .= __('Referrer', 'formidable') . ': ' . $data['referrer'] . "\r\n";
     }
     unset($prev_mail_body);
     // Add attachments
     $attachments = apply_filters('frm_notification_attachment', array(), $form, compact('entry', 'email_key'));
     if (!empty($notification['email_subject'])) {
         $notification['email_subject'] = apply_filters('frm_email_subject', $notification['email_subject'], compact('form', 'entry', 'email_key'));
     }
     // check for a phone number
     foreach ((array) $to_emails as $email_key => $e) {
         if ($e != '[admin_email]' && !is_email($e)) {
             $e = explode(' ', $e);
             //If to_email has name <*****@*****.**> format
             if (is_email(end($e))) {
                 continue;
             }
             do_action('frm_send_to_not_email', array('e' => $e, 'subject' => $notification['email_subject'], 'mail_body' => $mail_body, 'reply_to' => $notification['reply_to'], 'from' => $notification['from'], 'plain_text' => $plain_text, 'attachments' => $attachments, 'form' => $form, 'email_key' => $email_key));
             unset($to_emails[$email_key]);
         }
     }
     // Send the email now
     $sent_to = self::send_email(array('to_email' => $to_emails, 'subject' => $notification['email_subject'], 'message' => $mail_body, 'from' => $notification['from'], 'plain_text' => $plain_text, 'reply_to' => $notification['reply_to'], 'attachments' => $attachments, 'cc' => $cc, 'bcc' => $bcc));
     return $sent_to;
 }