| 1 |
<?php |
| 2 |
|
| 3 |
use AcyMailing\Classes\CampaignClass; |
| 4 |
use AcyMailing\Classes\ListClass; |
| 5 |
use AcyMailing\Classes\MailClass; |
| 6 |
use AcyMailing\Helpers\MailerHelper; |
| 7 |
|
| 8 |
trait ForwardMailboxAction |
| 9 |
{ |
| 10 |
protected function initMailboxActions(): void |
| 11 |
{ |
| 12 |
$this->mailboxActions = [ |
| 13 |
'forward_specific' => acym_translation('ACYM_FORWARD_EMAIL'), |
| 14 |
'forward_list' => acym_translation('ACYM_FORWARD_TO_A_LIST'), |
| 15 |
]; |
| 16 |
} |
| 17 |
|
| 18 |
public function displayMailboxActionOptions_forward_specific(string $fieldName): void |
| 19 |
{ |
| 20 |
?> |
| 21 |
<input type="text" |
| 22 |
name="<?php echo esc_attr($fieldName.'[addresses]'); ?>" |
| 23 |
placeholder="[email protected],[email protected]"><br /> |
| 24 |
<?php |
| 25 |
$this->displayForwardTemplateChoice($fieldName); |
| 26 |
} |
| 27 |
|
| 28 |
public function displayMailboxActionOptions_forward_list(string $fieldName): void |
| 29 |
{ |
| 30 |
$listClass = new ListClass(); |
| 31 |
?> |
| 32 |
<div class="intext_select_mailbox cell"> |
| 33 |
<?php acym_select( |
| 34 |
$listClass->getAllWithIdName(), |
| 35 |
$fieldName.'[list_id]', |
| 36 |
null, |
| 37 |
['class' => 'acym__select'], |
| 38 |
'value', |
| 39 |
'text', |
| 40 |
null, |
| 41 |
false, |
| 42 |
true |
| 43 |
); ?> |
| 44 |
</div> |
| 45 |
<br /><br /> |
| 46 |
<?php |
| 47 |
$this->displayForwardTemplateChoice($fieldName); |
| 48 |
} |
| 49 |
|
| 50 |
private function displayForwardTemplateChoice(string $fieldName): void |
| 51 |
{ |
| 52 |
$mailClass = new MailClass(); |
| 53 |
$templates = $mailClass->getTemplatesForMailboxAction(); |
| 54 |
|
| 55 |
$firstOption = new stdClass(); |
| 56 |
$firstOption->id = 0; |
| 57 |
$firstOption->name = acym_translation('ACYM_CHOOSE_TEMPLATE'); |
| 58 |
array_unshift($templates, $firstOption); |
| 59 |
|
| 60 |
$templateSelect = '<div class="intext_select_mailbox cell">'.acym_select( |
| 61 |
$templates, |
| 62 |
$fieldName.'[template_id]', |
| 63 |
null, |
| 64 |
['class' => 'acym__select'], |
| 65 |
'id', |
| 66 |
'name' |
| 67 |
).'</div>'; |
| 68 |
|
| 69 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- The select is built and escaped by acym_select just above, and its position in the sentence depends on the language. |
| 70 |
echo acym_translationSprintf('ACYM_INCLUDE_IN_TEMPLATE', $templateSelect); |
| 71 |
|
| 72 |
acym_info(['textShownInTooltip' => 'ACYM_INCLUDE_IN_TEMPLATE_DESC']); |
| 73 |
} |
| 74 |
|
| 75 |
public function onAcymMailboxActionSummaryListing(&$action, &$result) |
| 76 |
{ |
| 77 |
if (!empty($action['forward_list'])) { |
| 78 |
$listClass = new ListClass(); |
| 79 |
$list = $listClass->getOneById($action['forward_list']['list_id']); |
| 80 |
|
| 81 |
if (empty($list)) { |
| 82 |
return; |
| 83 |
} |
| 84 |
|
| 85 |
$result[] = acym_translationSprintf('ACYM_FORWARD_TO_A_LIST_X', $list->name); |
| 86 |
} |
| 87 |
|
| 88 |
if (!empty($action['forward_specific'])) { |
| 89 |
$result[] = acym_translationSprintf('ACYM_FORWARD_EMAIL_X', $action['forward_specific']['addresses']); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
public function onAcymMailboxAction_forward_specific(&$action, &$report, &$executedActions, $mailboxHelper) |
| 94 |
{ |
| 95 |
$newMail = $this->prepareForward($action, $report, $executedActions, $mailboxHelper); |
| 96 |
|
| 97 |
if (empty($newMail)) { |
| 98 |
$report[] = [ |
| 99 |
'message' => acym_translation('ACYM_COULD_NOT_FORWARD_EMAIL'), |
| 100 |
'success' => false, |
| 101 |
]; |
| 102 |
|
| 103 |
return; |
| 104 |
} |
| 105 |
|
| 106 |
$newMail->id = $this->saveNewMail($newMail); |
| 107 |
|
| 108 |
if (empty($newMail->id)) { |
| 109 |
return; |
| 110 |
} |
| 111 |
|
| 112 |
$receivers = explode(',', $action['addresses']); |
| 113 |
$mailerHelper = new MailerHelper(); |
| 114 |
$mailerHelper->autoAddUser = true; |
| 115 |
$mailerHelper->isForward = true; |
| 116 |
$mailInfo = $newMail->subject.' ('.$newMail->id.')'; |
| 117 |
foreach ($receivers as $oneReceiver) { |
| 118 |
if ($mailerHelper->sendOne($newMail->id, $oneReceiver)) { |
| 119 |
$report[] = [ |
| 120 |
'message' => acym_translationSprintf('ACYM_SEND_SUCCESS', $mailInfo, esc_attr($oneReceiver)), |
| 121 |
'success' => true, |
| 122 |
]; |
| 123 |
} else { |
| 124 |
$report[] = [ |
| 125 |
'message' => acym_translationSprintf('ACYM_SEND_ERROR', $mailInfo, esc_attr($oneReceiver)), |
| 126 |
'success' => false, |
| 127 |
]; |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
$mailClass = new MailClass(); |
| 132 |
$mailClass->delete([$newMail->id]); |
| 133 |
} |
| 134 |
|
| 135 |
public function onAcymMailboxAction_forward_list(&$action, &$report, &$executedActions, $mailboxHelper) |
| 136 |
{ |
| 137 |
$newMail = $this->prepareForward($action, $report, $executedActions, $mailboxHelper); |
| 138 |
|
| 139 |
if (empty($newMail)) { |
| 140 |
$report[] = [ |
| 141 |
'message' => acym_translation('ACYM_COULD_NOT_FORWARD_EMAIL'), |
| 142 |
'success' => false, |
| 143 |
]; |
| 144 |
|
| 145 |
return; |
| 146 |
} |
| 147 |
|
| 148 |
if (!empty($mailboxHelper->_message->fromaddress)) { |
| 149 |
$newMail->body = str_replace('{mailheader:from}', htmlspecialchars($mailboxHelper->_message->fromaddress, ENT_QUOTES, 'UTF-8'), $newMail->body); |
| 150 |
} |
| 151 |
if (!empty($mailboxHelper->_message->toaddress)) { |
| 152 |
$newMail->body = str_replace('{mailheader:to}', htmlspecialchars($mailboxHelper->_message->toaddress, ENT_QUOTES, 'UTF-8'), $newMail->body); |
| 153 |
} |
| 154 |
|
| 155 |
if (!empty($mailboxHelper->_message->ccaddress)) { |
| 156 |
$newMail->body = str_replace('{mailheader:cc}', htmlspecialchars($mailboxHelper->_message->ccaddress, ENT_QUOTES, 'UTF-8'), $newMail->body); |
| 157 |
} |
| 158 |
|
| 159 |
if (!empty($mailboxHelper->_message->Date)) { |
| 160 |
$newMail->body = str_replace('{mailheader:date}', acym_date(strtotime($mailboxHelper->_message->Date), 'd.m.Y H:i'), $newMail->body); |
| 161 |
} |
| 162 |
|
| 163 |
// Clean tags if we couldn't replace them |
| 164 |
$newMail->body = str_replace( |
| 165 |
[ |
| 166 |
'{mailheader:from}', |
| 167 |
'{mailheader:to}', |
| 168 |
'{mailheader:cc}', |
| 169 |
'{mailheader:date}', |
| 170 |
], |
| 171 |
'', |
| 172 |
$newMail->body |
| 173 |
); |
| 174 |
|
| 175 |
$newMail->id = $this->saveNewMail($newMail); |
| 176 |
|
| 177 |
if (!$newMail->id) { |
| 178 |
return; |
| 179 |
} |
| 180 |
|
| 181 |
$newCampaign = new stdClass(); |
| 182 |
$newCampaign->mail_id = $newMail->id; |
| 183 |
$newCampaign->sending_date = acym_date('now', 'Y-m-d H:i:s', false); |
| 184 |
$newCampaign->sending_type = CampaignClass::SENDING_TYPE_NOW; |
| 185 |
|
| 186 |
$campaignClass = new CampaignClass(); |
| 187 |
$newCampaign->id = $campaignClass->save($newCampaign); |
| 188 |
|
| 189 |
if (empty($newCampaign->id)) { |
| 190 |
$report[] = [ |
| 191 |
'message' => acym_translation('ACYM_COULD_NOT_CREATE_CAMPAIGN'), |
| 192 |
'success' => false, |
| 193 |
]; |
| 194 |
|
| 195 |
return; |
| 196 |
} |
| 197 |
|
| 198 |
$listAssigned = $campaignClass->manageListsToCampaign([$action['list_id']], $newMail->id); |
| 199 |
|
| 200 |
if (!$listAssigned) { |
| 201 |
$report[] = [ |
| 202 |
'message' => acym_translation('ACYM_COULD_NOT_ASSIGN_LIST'), |
| 203 |
'success' => false, |
| 204 |
]; |
| 205 |
|
| 206 |
return; |
| 207 |
} |
| 208 |
|
| 209 |
$statusSent = $campaignClass->send($newCampaign->id); |
| 210 |
|
| 211 |
if (!$statusSent) { |
| 212 |
$report[] = [ |
| 213 |
'message' => acym_translation('ACYM_COULD_NOT_SEND_CAMPAIGN'), |
| 214 |
'success' => false, |
| 215 |
]; |
| 216 |
} else { |
| 217 |
$report[] = [ |
| 218 |
'message' => acym_translation('ACYM_CAMPAIGN_ADDED_QUEUE'), |
| 219 |
'success' => true, |
| 220 |
]; |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
private function prepareForward($action, &$report, &$executedActions, $mailboxHelper) |
| 225 |
{ |
| 226 |
if (!isset($action['template_id'])) { |
| 227 |
$executedActions = false; |
| 228 |
$report[] = [ |
| 229 |
'message' => acym_translation('ACYM_COULD_NOT_FIND_TEMPLATE'), |
| 230 |
'success' => false, |
| 231 |
]; |
| 232 |
|
| 233 |
return false; |
| 234 |
} |
| 235 |
|
| 236 |
// We're going to forward the email, save its attachments and embed images |
| 237 |
$mailboxHelper->decodeMessage(true); |
| 238 |
|
| 239 |
$subject = $mailboxHelper->_message->subject; |
| 240 |
|
| 241 |
//If the option remove subject is set |
| 242 |
if (!empty($mailboxHelper->action->conditions['subject_remove'])) { |
| 243 |
if (!empty($mailboxHelper->action->conditions['subject_text'])) { |
| 244 |
$subject = str_replace($mailboxHelper->action->conditions['subject_text'], '', $mailboxHelper->_message->subject); |
| 245 |
} elseif (!empty($mailboxHelper->action->conditions['subject_regex'])) { |
| 246 |
$subject = preg_replace($mailboxHelper->action->conditions['subject_regex'], '', $mailboxHelper->_message->subject); |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
$newMail = new stdClass(); |
| 251 |
if (empty($action['template_id'])) { |
| 252 |
$newMail->name = acym_translationSprintf('ACYM_FORWARD_SUBJECT', $subject); |
| 253 |
$newMail->body = empty($mailboxHelper->_message->html) ? $mailboxHelper->_message->text : $mailboxHelper->_message->html; |
| 254 |
} else { |
| 255 |
$mailClass = new MailClass(); |
| 256 |
$newMail = $mailClass->getOneById($action['template_id']); |
| 257 |
|
| 258 |
if (strpos($newMail->body, '{emailcontent}') === false) { |
| 259 |
$executedActions = false; |
| 260 |
$report[] = [ |
| 261 |
'message' => acym_translation('ACYM_TEMPLATE_DOES_NOT_CONTAIN_EMAILCONTENT'), |
| 262 |
'success' => false, |
| 263 |
]; |
| 264 |
|
| 265 |
return false; |
| 266 |
} else { |
| 267 |
$content = empty($mailboxHelper->_message->html) ? $mailboxHelper->_message->text : $mailboxHelper->_message->html; |
| 268 |
$newMail->body = str_replace('{emailcontent}', $content, $newMail->body); |
| 269 |
} |
| 270 |
unset($newMail->id); |
| 271 |
} |
| 272 |
|
| 273 |
$newMail->subject = $subject; |
| 274 |
$newMail->type = mailClass::TYPE_MAILBOX_ACTION; |
| 275 |
$newMail->creation_date = acym_date('now', 'Y-m-d H:i:s', false); |
| 276 |
|
| 277 |
if (!empty($mailboxHelper->action->senderfrom)) { |
| 278 |
$newMail->from_email = $mailboxHelper->decodeHeader($mailboxHelper->_message->header->from_email); |
| 279 |
$newMail->from_name = acym_stripTags($mailboxHelper->decodeHeader($mailboxHelper->_message->header->from_name)); |
| 280 |
} |
| 281 |
|
| 282 |
if (!empty($mailboxHelper->action->senderto)) { |
| 283 |
$newMail->reply_to_email = $mailboxHelper->decodeHeader($mailboxHelper->_message->header->from_email); |
| 284 |
$newMail->reply_to_name = acym_stripTags($mailboxHelper->decodeHeader($mailboxHelper->_message->header->from_name)); |
| 285 |
} |
| 286 |
|
| 287 |
if (!empty($mailboxHelper->attachments)) { |
| 288 |
$newMail->attachments = $mailboxHelper->getAttachments(); |
| 289 |
} |
| 290 |
|
| 291 |
return $newMail; |
| 292 |
} |
| 293 |
|
| 294 |
public function saveNewMail($newMail) |
| 295 |
{ |
| 296 |
$mailClass = new MailClass(); |
| 297 |
$newMail->id = $mailClass->save($newMail); |
| 298 |
|
| 299 |
if (empty($newMail->id)) { |
| 300 |
return false; |
| 301 |
} |
| 302 |
|
| 303 |
return $newMail->id; |
| 304 |
} |
| 305 |
} |
| 306 |
|