| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\App\Services\FormBuilder\Notifications; |
| 4 |
|
| 5 |
use FluentForm\App\Modules\Form\FormFieldsParser; |
| 6 |
use FluentForm\App\Services\Emogrifier\Emogrifier; |
| 7 |
use FluentForm\Framework\Foundation\Application; |
| 8 |
use FluentForm\Framework\Helpers\ArrayHelper; |
| 9 |
use FluentForm\View; |
| 10 |
|
| 11 |
class EmailNotification |
| 12 |
{ |
| 13 |
/** |
| 14 |
* FluentForm\Framework\Foundation\Application |
| 15 |
* @var $app |
| 16 |
*/ |
| 17 |
protected $app = null; |
| 18 |
|
| 19 |
/** |
| 20 |
* Biuld the instance of this class |
| 21 |
* @param FluentForm\Framework\Foundation\Application $app |
| 22 |
* @return $this |
| 23 |
*/ |
| 24 |
public function __construct(Application $app) |
| 25 |
{ |
| 26 |
$this->app = $app; |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Send the email notification |
| 31 |
* @param array $notification [Notification settings from form meta] |
| 32 |
* @param array $submittedData [User submitted form data] |
| 33 |
* @param \StdClass $form [The form object from database] |
| 34 |
* @return bool |
| 35 |
*/ |
| 36 |
public function notify($notification, $submittedData, $form, $entryId = false) |
| 37 |
{ |
| 38 |
$isSendAsPlain = ArrayHelper::get($notification, 'asPlainText') == 'yes'; |
| 39 |
|
| 40 |
$isSendAsPlain = apply_filters('fluenform_send_plain_html_email', $isSendAsPlain, $form, $notification); |
| 41 |
|
| 42 |
$headers = $this->getHeaders($notification, $isSendAsPlain); |
| 43 |
|
| 44 |
$attachments = $this->app->applyFilters( |
| 45 |
'fluentform_filter_email_attachments', |
| 46 |
isset($notification['attachments']) ? $notification['attachments'] : [], |
| 47 |
$notification, |
| 48 |
$form, |
| 49 |
$submittedData |
| 50 |
); |
| 51 |
$emailBody = $notification['message']; |
| 52 |
|
| 53 |
if ($isSendAsPlain) { |
| 54 |
$emailBody = strip_tags($emailBody); |
| 55 |
} else { |
| 56 |
$emailBody = $this->getEmailWithTemplate($emailBody, $form, $notification); |
| 57 |
} |
| 58 |
|
| 59 |
if (ArrayHelper::get($notification, 'sendTo.type') == 'field' && !empty($notification['sendTo']['field'])) { |
| 60 |
$notification['sendTo']['email'] = ArrayHelper::get($submittedData, $notification['sendTo']['field']); |
| 61 |
} |
| 62 |
|
| 63 |
if (!$notification['sendTo']['email'] || !$notification['subject']) { |
| 64 |
do_action('ff_log_data', [ |
| 65 |
'parent_source_id' => $form->id, |
| 66 |
'source_type' => 'submission_item', |
| 67 |
'source_id' => $entryId, |
| 68 |
'component' => 'EmailNotification', |
| 69 |
'status' => 'error', |
| 70 |
'title' => 'Email sending skipped', |
| 71 |
'description' => "Email skipped to send because email/subject may not valid.<br />Subject: {$notification['subject']}. <br/>Email: " . $notification['sendTo']['email'], |
| 72 |
]); |
| 73 |
return false; |
| 74 |
} |
| 75 |
|
| 76 |
if ($entryId) { |
| 77 |
do_action('ff_log_data', [ |
| 78 |
'parent_source_id' => $form->id, |
| 79 |
'source_type' => 'submission_item', |
| 80 |
'source_id' => $entryId, |
| 81 |
'component' => 'EmailNotification', |
| 82 |
'status' => 'info', |
| 83 |
'title' => 'Email sending initiated', |
| 84 |
'description' => "Email Notification broadcasted to " . $notification['sendTo']['email'] . ".<br />Subject: {$notification['subject']}", |
| 85 |
]); |
| 86 |
|
| 87 |
/* |
| 88 |
* Inline email logger. It will work fine hopefully |
| 89 |
*/ |
| 90 |
add_action('wp_mail_failed', function ($error) use ($notification, $form, $entryId) { |
| 91 |
|
| 92 |
$failedMailSubject = ArrayHelper::get($error->error_data, 'wp_mail_failed.subject'); |
| 93 |
if ($failedMailSubject == $notification['subject']) { |
| 94 |
$reason = $error->get_error_message(); |
| 95 |
do_action('ff_log_data', [ |
| 96 |
'parent_source_id' => $form->id, |
| 97 |
'source_type' => 'submission_item', |
| 98 |
'source_id' => $entryId, |
| 99 |
'component' => 'EmailNotification', |
| 100 |
'status' => 'failed', |
| 101 |
'title' => 'Email sending failed', |
| 102 |
'description' => "Email Notification failed to sent.<br />Subject: {$notification['subject']}. <br/>Reason: " . $reason, |
| 103 |
]); |
| 104 |
} |
| 105 |
}, 10, 1); |
| 106 |
} |
| 107 |
|
| 108 |
$sendEmail = explode(',', $notification['sendTo']['email']); |
| 109 |
if (count($sendEmail) > 1) { |
| 110 |
$notification['sendTo']['email'] = $sendEmail; |
| 111 |
} |
| 112 |
|
| 113 |
return wp_mail( |
| 114 |
$notification['sendTo']['email'], |
| 115 |
$notification['subject'], |
| 116 |
$emailBody, |
| 117 |
$headers, |
| 118 |
$attachments |
| 119 |
); |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* @param $formId |
| 124 |
* @return array |
| 125 |
* @todo: Implement Caching mechanism so we don't have to parse these things for every request |
| 126 |
*/ |
| 127 |
private function getFormInputsAndLabels($form) |
| 128 |
{ |
| 129 |
$formInputs = FormFieldsParser::getInputs($form); |
| 130 |
|
| 131 |
$inputLabels = FormFieldsParser::getAdminLabels($form, $formInputs); |
| 132 |
|
| 133 |
return [ |
| 134 |
'inputs' => $formInputs, |
| 135 |
'labels' => $inputLabels |
| 136 |
]; |
| 137 |
} |
| 138 |
|
| 139 |
public function getEmailWithTemplate($emailBody, $form, $notification) |
| 140 |
{ |
| 141 |
$originalEmailBody = $emailBody; |
| 142 |
$emailHeader = apply_filters('fluentform_email_header', '', $form, $notification); |
| 143 |
$emailFooter = apply_filters('fluentform_email_footer', '', $form, $notification); |
| 144 |
|
| 145 |
if (empty($emailHeader)) { |
| 146 |
$emailHeader = View::make('email.template.header', array( |
| 147 |
'form' => $form, |
| 148 |
'notification' => $notification |
| 149 |
)); |
| 150 |
} |
| 151 |
|
| 152 |
if (empty($emailFooter)) { |
| 153 |
$emailFooter = View::make('email.template.footer', array( |
| 154 |
'form' => $form, |
| 155 |
'notification' => $notification, |
| 156 |
'footerText' => $this->getFooterText($form, $notification) |
| 157 |
)); |
| 158 |
} |
| 159 |
|
| 160 |
$css = View::make('email.template.styles'); |
| 161 |
$css = apply_filters('fluentform_email_styles', $css, $form, $notification); |
| 162 |
$emailBody = $emailHeader . $emailBody . $emailFooter; |
| 163 |
|
| 164 |
ob_start(); |
| 165 |
try { |
| 166 |
// apply CSS styles inline for picky email clients |
| 167 |
$emogrifier = new Emogrifier($emailBody, $css); |
| 168 |
$emailBody = $emogrifier->emogrify(); |
| 169 |
} catch (\Exception $e) { |
| 170 |
|
| 171 |
} |
| 172 |
$maybeError = ob_get_clean(); |
| 173 |
|
| 174 |
if ($maybeError) { |
| 175 |
return $originalEmailBody; |
| 176 |
} |
| 177 |
|
| 178 |
return $emailBody; |
| 179 |
} |
| 180 |
|
| 181 |
private function getFooterText($form, $notification) |
| 182 |
{ |
| 183 |
$option = get_option('_fluentform_global_form_settings'); |
| 184 |
if ($option && !empty($option['misc']['email_footer_text'])) { |
| 185 |
$footerText = $option['misc']['email_footer_text']; |
| 186 |
} else { |
| 187 |
$footerText = '© ' . get_bloginfo('name', 'display') . '.'; |
| 188 |
} |
| 189 |
|
| 190 |
return apply_filters('fluentform_email_template_footer_text', $footerText, $form, $notification); |
| 191 |
} |
| 192 |
|
| 193 |
private function getHeaders($notification, $isSendAsPlain = false) |
| 194 |
{ |
| 195 |
$headers = [ |
| 196 |
'Content-Type: text/html; charset=utf-8' |
| 197 |
]; |
| 198 |
|
| 199 |
if ($isSendAsPlain) { |
| 200 |
$headers = []; |
| 201 |
} |
| 202 |
|
| 203 |
$fromEmail = $notification['fromEmail']; |
| 204 |
|
| 205 |
if (!is_email($fromEmail)) { |
| 206 |
$fromEmail = false; |
| 207 |
} |
| 208 |
|
| 209 |
if ($notification['fromName'] && $fromEmail) { |
| 210 |
$headers[] = "From: {$notification['fromName']} <{$fromEmail}>"; |
| 211 |
} elseif ($fromEmail) { |
| 212 |
$headers[] = "From: <{$fromEmail}>"; |
| 213 |
} |
| 214 |
|
| 215 |
if (!empty($notification['bcc'])) { |
| 216 |
$bccEmail = $notification['bcc']; |
| 217 |
$headers[] = 'bcc: ' . $bccEmail; |
| 218 |
} |
| 219 |
|
| 220 |
if (!empty($notification['cc'])) { |
| 221 |
$ccEmail = $notification['cc']; |
| 222 |
$headers[] = 'cc: ' . $ccEmail; |
| 223 |
} |
| 224 |
|
| 225 |
if ($notification['replyTo'] && is_email($notification['replyTo'])) { |
| 226 |
$headers[] = "Reply-To: <".$notification['replyTo'].">"; |
| 227 |
} |
| 228 |
|
| 229 |
$headers = $this->app->applyFilters( |
| 230 |
'fluenttform_email_header', $headers, $notification |
| 231 |
); |
| 232 |
|
| 233 |
return $headers; |
| 234 |
} |
| 235 |
|
| 236 |
} |
| 237 |
|