/** * @covers FrmNotification::trigger_email */ public function test_trigger_email() { // get the imported form with the email action $form = $this->factory->form->get_object_by_id($this->contact_form_key); // get the email settings $actions = FrmFormAction::get_action_for_form($form->id, 'email'); $this->assertNotEmpty($actions); $entry_data = $this->factory->field->generate_entry_array($form); $entry_id = $this->factory->entry->create($entry_data); $entry = FrmEntry::getOne($entry_id, true); $this->assertNotEmpty($entry); foreach ($actions as $action) { $expected_to = $action->post_content['email_to']; FrmNotification::trigger_email($action, $entry, $form); $this->assertEquals($expected_to, $GLOBALS['phpmailer']->mock_sent[0]['to'][0][0]); //$this->assertNotEmpty( strpos( $GLOBALS['phpmailer']->mock_sent[0]['header'], 'Reply-To: admin@example.org' ) ); // TODO: check email body, reply to, cc, bcc, from } }
public static function autoresponder($entry_id, $form_id) { if (defined('WP_IMPORTING')) { return; } global $frm_entry, $frm_entry_meta; $frm_form = new FrmForm(); $form = $frm_form->getOne($form_id); if (!$form) { return; } $form_options = maybe_unserialize($form->options); if (!isset($form_options['auto_responder']) or !$form_options['auto_responder'] or !isset($form_options['ar_email_message']) or $form_options['ar_email_message'] == '') { return; } //don't continue forward unless a message has been inserted $entry = $frm_entry->getOne($entry_id, true); if (!$entry or $entry->is_draft) { return; } $frm_notification = new FrmNotification(); $entry_ids = array($entry->id); $email_field = isset($form_options['ar_email_to']) ? $form_options['ar_email_to'] : 0; if (preg_match('/|/', $email_field)) { $email_fields = explode('|', $email_field); if (isset($email_fields[1])) { if (isset($entry->metas[$email_fields[0]])) { $add_id = $entry->metas[$email_fields[0]]; $add_id = maybe_unserialize($add_id); if (is_array($add_id)) { foreach ($add_id as $add) { $entry_ids[] = $add; } } else { $entry_ids[] = $add_id; } } $email_field = $email_fields[1]; } unset($email_fields); } $inc_fields = array(); foreach (array($email_field) as $inc_field) { if ($inc_field) { $inc_fields[] = $inc_field; } } $where = "it.item_id in (" . implode(',', $entry_ids) . ")"; if (!empty($inc_fields)) { $inc_fields = implode(',', $inc_fields); $where .= " and it.field_id in ({$inc_fields})"; } $values = $frm_entry_meta->getAll($where, " ORDER BY fi.field_order"); $plain_text = (isset($form_options['ar_plain_text']) and $form_options['ar_plain_text']) ? true : false; $message = apply_filters('frm_ar_message', $form_options['ar_email_message'], array('entry' => $entry, 'form' => $form)); $shortcodes = FrmProAppHelper::get_shortcodes($message, $form_id); $mail_body = FrmProFieldsHelper::replace_shortcodes($message, $entry, $shortcodes); $frm_blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES); $reply_to_name = isset($form_options['ar_reply_to_name']) ? $form_options['ar_reply_to_name'] : $frm_blogname; //default sender name $reply_to = isset($form_options['ar_reply_to']) ? $form_options['ar_reply_to'] : '[admin_email]'; //default sender email foreach ($values as $value) { if ((int) $email_field == $value->field_id) { if ($value->field_type == 'user_id') { $user_data = get_userdata($value->meta_value); $to_email = $user_data->user_email; } else { $val = apply_filters('frm_email_value', maybe_unserialize($value->meta_value), $value, $entry); if (is_email($val)) { $to_email = $val; } } } } if (!isset($to_email)) { return; } if (isset($form_options['ar_email_subject']) and $form_options['ar_email_subject'] != '') { $shortcodes = FrmProAppHelper::get_shortcodes($form_options['ar_email_subject'], $form_id); $subject = FrmProFieldsHelper::replace_shortcodes($form_options['ar_email_subject'], $entry, $shortcodes); } else { $subject = sprintf(__('%1$s Form submitted on %2$s', 'formidable'), $form->name, $frm_blogname); //subject } $subject = apply_filters('frm_ar_subject', $subject, $form); $attachments = apply_filters('frm_autoresponder_attachment', array(), $form); $frm_notification->send_notification_email($to_email, $subject, $mail_body, $reply_to, $reply_to_name, $plain_text, $attachments); return $to_email; }