/** * _send * * Send email campaign that is triggered by the "trigger email". * * @param Array $triggerrecord Trigger record * @param Array $queuerecord Queue record * @param Array $newsletter Newsletter record * @return Array Returns a status result that contains the sending result, and the email address that get sent */ private function _send($triggerrecord, $queuerecord, $newsletter) { static $prevUserID = 0; static $prevQueueID = 0; static $userPause = 0; static $subscriberAPI = null; $triggerid = $triggerrecord['triggeremailsid']; $newsletterid = $newsletter['newsletterid']; $recipientid = $queuerecord['recipient']; $user = null; $email = null; $customfields = array(); $return = array( 'email' => '', 'message' => '', 'listid' => 0, 'result' => false ); if (is_null($subscriberAPI)) { $subscriberAPI = new Subscribers_API(); } /** * Get owner of trigger email */ $user = $this->_cacheUser($triggerrecord['ownerid']); if (empty($user)) { trigger_error('Cannot get trigger owner record', E_USER_NOTICE); return $return; } /** * ----- */ /** * Get email object, and setup * This will setup emails and setup everything that it needs to have according to the newsletter record. */ $email = new SS_Email_API(); $email->Set('CharSet', SENDSTUDIO_CHARSET); if (!SENDSTUDIO_SAFE_MODE) { $email->Set('imagedir', TEMP_DIRECTORY . '/triggeremails.' . $triggerrecord['queueid']); } else { $email->Set('imagedir', TEMP_DIRECTORY . '/send'); } if (SENDSTUDIO_FORCE_UNSUBLINK) { $email->ForceLinkChecks(true); } $email->Set('Subject', $newsletter['subject']); /** * Setup attachments */ $email->ClearAttachments(); $tempAttachments = SendStudio_Functions::GetAttachments('newsletters', $newsletterid, true); if ($tempAttachments) { $path = $tempAttachments['path']; $files = $tempAttachments['filelist']; foreach ($files as $p => $file) { $email->AddAttachment($path . DIRECTORY_SEPARATOR . $file); } } /** * ----- */ /** * Set up the contents of the newsletter and the formatting (ie. multipart/html only/text only) */ $format = $newsletter['format']; $email->Set('Multipart', false); if ($format == 'b' || $format == 't') { if ($newsletter['textbody']) { $email->AddBody('text', $newsletter['textbody']); $email->AppendBody('text', $user->Get('textfooter')); $email->AppendBody('text', stripslashes(SENDSTUDIO_TEXTFOOTER)); } } if ($format == 'b' || $format == 'h') { if ($newsletter['htmlbody']) { $email->AddBody('html', $newsletter['htmlbody']); $email->AppendBody('html', $user->Get('htmlfooter')); $email->AppendBody('html', stripslashes(SENDSTUDIO_HTMLFOOTER)); } } if ($format == 'b') { if ($newsletter['textbody'] && $newsletter['htmlbody']) { $email->Set('Multipart', true); } else { $email->Set('Multipart', false); } } /** * ----- */ // Setup custom fields $customfields = $email->GetCustomFields(); if (!is_a($email, 'SS_Email_API')) { trigger_error('Cannot instantiate email object to be used to send emails', E_USER_NOTICE); return $return; } $email->Debug = $this->_debugMode; /** * ----- */ /** * Set up email object for any headers and SMTP server details. * These setup will take account of each trigger records (ie. owner and user as well as stats and other sending related headers) */ /** * Calculate pause time to make sure it complies with the throttling */ $pause = $pausetime = 0; if ($user->perhour > 0) { $pause = $user->perhour; } // in case the system rate is less than the user rate, lower it. if (SENDSTUDIO_MAXHOURLYRATE > 0) { if ($pause == 0) { $pause = SENDSTUDIO_MAXHOURLYRATE; } else { $pause = min($pause, SENDSTUDIO_MAXHOURLYRATE); } } if ($pause > 0) { $pausetime = 3600 / $pause; } $userPause = $pausetime; $this->_log('UserPause is set to ' . $pausetime); /** * ----- */ $email->SetSmtp(SENDSTUDIO_SMTP_SERVER, SENDSTUDIO_SMTP_USERNAME, @base64_decode(SENDSTUDIO_SMTP_PASSWORD), SENDSTUDIO_SMTP_PORT); if ($user->smtpserver) { $email->SetSmtp($user->smtpserver, $user->smtpusername, $user->smtppassword, $user->smtpport); } // If queue has changed since previous send, update email class for queue related information $email->TrackOpens((array_key_exists('trackopens', $triggerrecord['triggeractions']['send']) && $triggerrecord['triggeractions']['send']['trackopens'] == '1')); $email->TrackLinks((array_key_exists('tracklinks', $triggerrecord['triggeractions']['send']) && $triggerrecord['triggeractions']['send']['tracklinks'] == '1')); $email->Set('statid', $triggerrecord['statid']); $email->Set('FromName', $triggerrecord['triggeractions']['send']['sendfromname']); $email->Set('FromAddress', $triggerrecord['triggeractions']['send']['sendfromemail']); $email->Set('ReplyTo', $triggerrecord['triggeractions']['send']['replyemail']); $email->Set('BounceAddress', $triggerrecord['triggeractions']['send']['bounceemail']); $email->Set('EmbedImages', ((array_key_exists('embedimages', $triggerrecord['triggeractions']['send'])) && $triggerrecord['triggeractions']['send']['embedimages'] == '1')); $email->Set('Multipart', ((array_key_exists('multipart', $triggerrecord['triggeractions']['send'])) && $triggerrecord['triggeractions']['send']['multipart'] == '1')); /** * ----- */ /** * Set up recipient */ $subscriberinfo = $subscriberAPI->LoadSubscriberList($recipientid, 0, true); if (empty($subscriberinfo)) { trigger_error('Cannot fetch recipient details', E_USER_NOTICE); return $return; } $listinfo = $this->_cacheListGet($subscriberinfo['listid']); if (empty($listinfo)) { trigger_error('Unable to load recipient list details', E_USER_NOTICE); return $return; } // List ID for the particualar subscriber $email->Set('listids', $subscriberinfo['listid']); $subscriberinfo['ipaddress'] = $subscriberinfo['confirmip']; if (!$subscriberinfo['ipaddress']) { $subscriberinfo['ipaddress'] = $subscriberinfo['requestip']; } if (!$subscriberinfo['ipaddress']) { $subscriberinfo['ipaddress'] = ''; } $subscriberinfo['subscriberid'] = $recipientid; $subscriberinfo['newsletter'] = $triggerrecord['triggeractions']['send']['newsletterid']; $subscriberinfo['listid'] = $subscriberinfo['listid']; $subscriberinfo['statid'] = $triggerrecord['statid']; $subscriberinfo['listname'] = $listinfo['name']; $subscriberinfo['companyname'] = $listinfo['companyname']; $subscriberinfo['companyaddress'] = $listinfo['companyaddress']; $subscriberinfo['companyphone'] = $listinfo['companyphone']; if (!isset($subscriberinfo['CustomFields']) && empty($subscriberinfo['CustomFields'])) { $subscriberinfo['CustomFields'] = array(); /** * If the subscriber has no custom fields coming from the database, then set up blank placeholders. * If they have no custom fields in the database, they have no records in the 'all_customfields' array - so we need to fill it up with blank entries. */ foreach ($customfields as $fieldid => $fieldname) { $subscriberinfo['CustomFields'][] = array( 'fieldid' => $fieldid, 'fieldname' => $fieldname, 'fieldtype' => 'text', 'defaultvalue' => '', 'fieldsettings' => '', 'subscriberid' => $recipientid, 'data' => '' ); } } else { require_once(SENDSTUDIO_API_DIRECTORY.'/customfields_date.php'); foreach($subscriberinfo['CustomFields'] as $ar_key => $ar_value){ if($ar_value['fieldtype'] == 'date'){ $cfdateapi = new CustomFields_Date_API($ar_value['fieldid']); $real_order = $cfdateapi->GetRealValue($ar_value['data']); $subscriberinfo['CustomFields'][$ar_key]['data'] = $real_order; } } } // TODO refactor $name = ''; $firstname_field = $triggerrecord['triggeractions']['send']['firstnamefield']; if ($firstname_field) { foreach ($subscriberinfo['CustomFields'] as $p => $details) { if ($details['fieldid'] == $firstname_field && $details['data'] != '') { $name = $details['data']; break; } } } $lastname_field = $triggerrecord['triggeractions']['send']['lastnamefield']; if ($lastname_field) { foreach ($subscriberinfo['CustomFields'] as $p => $details) { if ($details['fieldid'] == $lastname_field && $details['data'] != '') { $name .= ' ' . $details['data']; break; } } } if (trim($name) == '') { $name = false; } // -- // SetupDynamicContentFields $dctEventListId = (is_array($subscriberinfo['listid']))?$subscriberinfo['listid']:array($subscriberinfo['listid']); $dctEvent =new EventData_IEM_ADDON_DYNAMICCONTENTTAGS_REPLACETAGCONTENT(); $dctEvent->lists = $dctEventListId; $dctEvent->info = array($subscriberinfo); $dctEvent->trigger(); // -- $email->ClearRecipients(); $email->AddRecipient($subscriberinfo['emailaddress'], $name, $subscriberinfo['format'], $subscriberinfo['subscriberid']); $email->AddDynamicContentInfo($dctEvent->contentTobeReplaced); $email->AddCustomFieldInfo($subscriberinfo['emailaddress'], $subscriberinfo); $status = $email->Send(true, true); if (!$status['success']) { list($return['email'], $return['message']) = $status['fail'][0]; trigger_error("Failed sending trigger email to {$return['email']}: {$return['message']}"); return $return; } $return['listid'] = $subscriberinfo['listid']; $return['email'] = $subscriberinfo['emailaddress']; /** * ----- */ // Set previous user $prevUserID = $user->userid; // Set previous queue $prevQueueID = $triggerrecord['queueid']; $return['result'] = true; return $return; }
/** * SetupEmail * Sets up the email object for use. * If the email has been set up before, it sets the Email_API object to the cache version and returns. * * If it has not been set up before, * the newsletter is loaded into the email class (subject, htmlbody, textbody, attachments) * * If it's a partial setup (the flag passed in), once the content has been loaded into the Email_API object * the method returns. * * This is used to get custom field id's being used in the email campaigns * so they can be loaded in bulk. * * If it's not a partial setup, then it * - sets the appropriate smtp details * - loads attachments * - sets extra header information * - sets the email header details (bounce, reply-to, from details etc) * - after all of that, set the object into the email_objects cache for later use. * * @param Boolean $partial_setup Whether this is a partial setup or not. A partial set up is used to work out which custom fields are being used so they can be loaded in bulk. * * @uses Email_API * @uses email_objects * @uses newsletters * @uses _sending_newsletter * @uses SS_Email_API */ private function SetupEmail($partial_setup = false) { if (isset($this->email_objects[$this->_sending_newsletter])) { $this->Email_API = $this->email_objects[$this->_sending_newsletter]; return; } /** * Set up the email class * so we can get the custom fields we need to. */ if (!isset($this->newsletters[$this->_sending_newsletter])) { return false; } $newsletter = $this->newsletters[$this->_sending_newsletter]; $email_api = new SS_Email_API(); if (SENDSTUDIO_SEND_TEST_MODE) { $email_api->TestMode = true; } $email_api->Set('Multipart', $this->jobdetails['Multipart']); $email_api->Set('Subject', $newsletter['Subject']); if ($this->jobdetails['Multipart']) { if ($newsletter['TextBody'] && $newsletter['HTMLBody'] && $newsletter['Format'] == 'b') { $sent_format = 'm'; } else { $email_api->Set('Multipart', false); } } if ($newsletter['TextBody'] && in_array($newsletter['Format'], array('t', 'b'))) { $email_api->AddBody('text', $newsletter['TextBody']); $email_api->AppendBody('text', $this->user->Get('textfooter')); $email_api->AppendBody('text', stripslashes(SENDSTUDIO_TEXTFOOTER)); } if ($newsletter['HTMLBody'] && in_array($newsletter['Format'], array('h', 'b'))) { $email_api->AddBody('html', $newsletter['HTMLBody']); $email_api->AppendBody('html', $this->user->Get('htmlfooter')); $email_api->AppendBody('html', stripslashes(SENDSTUDIO_HTMLFOOTER)); } if ($partial_setup) { $this->Email_API = $email_api; return; } $email_api->ClearRecipients(); $email_api->SetSmtp(SENDSTUDIO_SMTP_SERVER, SENDSTUDIO_SMTP_USERNAME, @base64_decode(SENDSTUDIO_SMTP_PASSWORD), SENDSTUDIO_SMTP_PORT); $email_api->Set('statid', $this->statids[$this->_sending_newsletter]); $email_api->Set('listids', $this->jobdetails['Lists']); if (SENDSTUDIO_FORCE_UNSUBLINK) { $email_api->ForceLinkChecks(true); } $email_api->TrackOpens(true); $email_api->TrackLinks(true); $email_api->Set('CharSet', $this->jobdetails['Charset']); if (!SENDSTUDIO_SAFE_MODE) { $email_api->Set('imagedir', TEMP_DIRECTORY . '/send.' . $this->_jobid . '.' . $this->_queueid); } else { $email_api->Set('imagedir', TEMP_DIRECTORY . '/send'); } // clear out the attachments just to be safe. $email_api->ClearAttachments(); if ($newsletter['Attachments']) { $path = $newsletter['Attachments']['path']; $files = $newsletter['Attachments']['filelist']; foreach ($files as $p => $file) { $email_api->AddAttachment($path . '/' . $file); } } $email_api->Set('FromName', $this->jobdetails['SendFromName']); $email_api->Set('FromAddress', $this->jobdetails['SendFromEmail']); $email_api->Set('ReplyTo', $this->jobdetails['ReplyToEmail']); $email_api->Set('BounceAddress', $this->jobdetails['BounceEmail']); $email_api->Set('EmbedImages', $this->jobdetails['EmbedImages']); $email_api->Set('SentBy', $this->user->Get('userid')); if ($this->user->smtpserver) { $email_api->SetSmtp($this->user->smtpserver, $this->user->smtpusername, $this->user->smtppassword, $this->user->smtpport); } $this->email_objects[$this->_sending_newsletter] = $email_api; $this->Email_API = $email_api; }