| 1 |
<?php |
| 2 |
if(!defined('ABSPATH')) die('You are not allowed to call this page directly.'); |
| 3 |
|
| 4 |
class FrmNotification{ |
| 5 |
function __construct(){ |
| 6 |
add_action('frm_after_create_entry', array(&$this, 'entry_created'), 10, 2); |
| 7 |
} |
| 8 |
|
| 9 |
function entry_created($entry_id, $form_id){ |
| 10 |
if (apply_filters('frm_stop_standard_email', false, $entry_id)) return; |
| 11 |
global $frm_entry, $frm_entry_meta; |
| 12 |
|
| 13 |
$entry = $frm_entry->getOne($entry_id, true); |
| 14 |
$frm_form = new FrmForm(); |
| 15 |
$form = $frm_form->getOne($form_id); |
| 16 |
$values = $frm_entry_meta->getAll("it.item_id = $entry_id", " ORDER BY fi.field_order"); |
| 17 |
|
| 18 |
|
| 19 |
if(isset($form->options['notification'])) |
| 20 |
$notification = reset($form->options['notification']); |
| 21 |
else |
| 22 |
$notification = $form->options; |
| 23 |
|
| 24 |
// Set the from and to email names and addresses |
| 25 |
$to_email = $notification['email_to']; |
| 26 |
if ( empty($to_email) ) { |
| 27 |
$to_email = '[admin_email]'; |
| 28 |
} |
| 29 |
|
| 30 |
$to_emails = explode(',', $to_email); |
| 31 |
|
| 32 |
$reply_to = $reply_to_name = ''; |
| 33 |
|
| 34 |
foreach ($values as $value) { |
| 35 |
$val = apply_filters('frm_email_value', maybe_unserialize($value->meta_value), $value, $entry); |
| 36 |
if (is_array($val)) |
| 37 |
$val = implode(', ', $val); |
| 38 |
|
| 39 |
if(isset($notification['reply_to']) and (int)$notification['reply_to'] == $value->field_id and is_email($val)) |
| 40 |
$reply_to = $val; |
| 41 |
|
| 42 |
if(isset($notification['reply_to_name']) and (int)$notification['reply_to_name'] == $value->field_id) |
| 43 |
$reply_to_name = $val; |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
if ( empty($reply_to) && $notification['reply_to'] == 'custom' ) { |
| 48 |
$reply_to = $notification['cust_reply_to']; |
| 49 |
} |
| 50 |
|
| 51 |
if ( empty($reply_to_name) && $notification['reply_to_name'] == 'custom' ){ |
| 52 |
$reply_to_name = $notification['cust_reply_to_name']; |
| 53 |
} |
| 54 |
|
| 55 |
// Set the email message |
| 56 |
$plain_text = (isset($notification['plain_text']) && $notification['plain_text']) ? true : false; |
| 57 |
$mail_body = isset($notification['email_message']) ? $notification['email_message'] : ''; |
| 58 |
|
| 59 |
$mail_body = FrmEntriesHelper::replace_default_message($mail_body, array( |
| 60 |
'id' => $entry->id, 'entry' => $entry, 'plain_text' => $plain_text, |
| 61 |
'user_info' => (isset($notification['inc_user_info']) ? $notification['inc_user_info'] : false), |
| 62 |
) ); |
| 63 |
|
| 64 |
// Set the subject |
| 65 |
$subject = isset($notification['email_subject']) ? $notification['email_subject'] : ''; |
| 66 |
if ( empty($subject) ) { |
| 67 |
$frm_blogname = wp_specialchars_decode( get_option('blogname'), ENT_QUOTES ); |
| 68 |
$subject = sprintf(__('%1$s Form submitted on %2$s', 'formidable'), $form->name, $frm_blogname); |
| 69 |
} |
| 70 |
|
| 71 |
// Send the emails now |
| 72 |
foreach ( (array) $to_emails as $to_email) { |
| 73 |
$this->send_notification_email(trim($to_email), $subject, $mail_body, $reply_to, $reply_to_name, $plain_text); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
function send_notification_email($to_email, $subject, $message, $reply_to='', $reply_to_name='', $plain_text=true, $attachments=array()){ |
| 78 |
$content_type = ($plain_text) ? 'text/plain' : 'text/html'; |
| 79 |
$reply_to_name = ($reply_to_name == '') ? wp_specialchars_decode( get_option('blogname'), ENT_QUOTES ) : $reply_to_name; //senders name |
| 80 |
$reply_to = ($reply_to == '' or $reply_to == '[admin_email]') ? get_option('admin_email') : $reply_to; //senders e-mail address |
| 81 |
|
| 82 |
if($to_email == '[admin_email]') |
| 83 |
$to_email = get_option('admin_email'); |
| 84 |
|
| 85 |
$charset = get_option('blog_charset'); |
| 86 |
$recipient = $to_email; //recipient |
| 87 |
$header = array(); |
| 88 |
$header[] = 'From: "'. $reply_to_name .'" <'. $reply_to .'>'; |
| 89 |
$header[] = 'Reply-To: '. $reply_to; |
| 90 |
$header[] = 'Content-Type: '. $content_type .'; charset="'. $charset . '"'; |
| 91 |
$subject = wp_specialchars_decode(strip_tags(stripslashes($subject)), ENT_QUOTES ); |
| 92 |
|
| 93 |
$message = do_shortcode($message); |
| 94 |
$message = wordwrap($message, 70, "\r\n"); //in case any lines are longer than 70 chars |
| 95 |
if($plain_text) |
| 96 |
$message = wp_specialchars_decode(strip_tags($message), ENT_QUOTES ); |
| 97 |
|
| 98 |
$header = apply_filters('frm_email_header', $header, compact('to_email', 'subject')); |
| 99 |
|
| 100 |
if ( apply_filters('frm_encode_subject', 1, $subject ) ) { |
| 101 |
$subject = '=?'. $charset .'?B?'. base64_encode($subject) .'?='; |
| 102 |
} |
| 103 |
|
| 104 |
remove_filter('wp_mail_from', 'bp_core_email_from_address_filter' ); |
| 105 |
remove_filter('wp_mail_from_name', 'bp_core_email_from_name_filter'); |
| 106 |
|
| 107 |
if (!wp_mail($recipient, $subject, $message, $header, $attachments)){ |
| 108 |
$header = 'From: "'. $reply_to_name .'" <'. $reply_to .'>'. "\r\n"; |
| 109 |
mail($recipient, $subject, $message, $header); |
| 110 |
} |
| 111 |
|
| 112 |
do_action('frm_notification', $recipient, $subject, $message); |
| 113 |
} |
| 114 |
|
| 115 |
} |
| 116 |
|