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