WYSIJA_help_mailer

The MailPoet Newsletters WYSIJA help mailer class.

Defined (1)

The class is defined in the following location(s).

/helpers/mailer.php  
  1. class WYSIJA_help_mailer extends PHPMailer { 
  2. var $report = true; 
  3. var $checkConfirmField = true; 
  4. var $checkEnabled = true; 
  5. var $checkAccept = true; 
  6. var $parameters = array(); 
  7. //var $dispatcher; 
  8. var $errorNumber = 0; 
  9. var $reportMessage = ''; 
  10. var $autoAddUser = false; 
  11.  
  12. /** 
  13. * change from 1, 6 to 1, 3, 5, 6 
  14. * 3: When email not published(it could happen when email is in pause) 
  15. * 4: When user not confirmed (it could happen when follow up after subscription or registration and not yet confirmed) 
  16. */ 
  17. var $errorNewTry = array( 1, 6 ); 
  18. var $forceTemplate = 0; 
  19. var $testemail = false; 
  20. var $isMailjet = false; 
  21. var $isElasticRest = false; 
  22. var $isSendGridRest = false; 
  23. var $DKIM_selector = 'wys'; 
  24. var $listids = false; 
  25. var $listnames = false; 
  26. var $is_wp_mail = false; 
  27.  
  28. /** 
  29. * Change parent properties scope for legacy compatibility 
  30. */ 
  31. public $to = array(); 
  32. public $cc = array(); 
  33. public $bcc = array(); 
  34. public $ReplyTo = array(); 
  35. public $all_recipients = array(); 
  36. public $attachment = array(); 
  37.  
  38. /** 
  39. * @param type $extension 
  40. * @param array $config 
  41. * @param boolean $multisiteTest 
  42. */ 
  43. function __construct($extension='', $config=false, $multisiteTest=false) { 
  44. $this->core = new WYSIJA_OBJECT(); 
  45.  
  46. $this->XMailer = 'MailPoet (https://www.mailpoet.com)'; 
  47. $this->subscriberClass = WYSIJA::get( 'user', 'model' ); 
  48. $this->subscriberClass->getFormat = OBJECT; 
  49.  
  50. $this->encodingHelper = WYSIJA::get( 'encoding', 'helper' ); 
  51. $this->config = WYSIJA::get( 'config', 'model' ); 
  52.  
  53. //override the config with the one passed as parameter above in the constructor 
  54. $optionsMsOverride = array(); 
  55. if ( ! empty( $config ) ) { 
  56. $optionsMsOverride = array( 'sendmail_path', 'smtp_rest', 'smtp_host', 'smtp_port', 'smtp_secure', 'smtp_auth', 'smtp_login', 'smtp_password' ); 
  57.  
  58. //unset($this->config->values); 
  59. foreach ( $config as $key => $val ) { 
  60. if ( $multisiteTest && in_array( $key, $optionsMsOverride ) && isset( $config[ 'ms_' . $key ] ) ) { 
  61. $this->config->values[ $key ] = $config[ 'ms_' . $key ]; 
  62. } else { 
  63. $this->config->values[ $key ] = $config[ $key ]; 
  64.  
  65. $is_multisite = is_multisite(); 
  66.  
  67. //$is_multisite=true;//PROD comment that line 
  68. //if we are in a multisite situation and there is one sending method set for all the sites then we just force all of the multisites settings 
  69. if($is_multisite && ( ( $this->config->getValue('sending_method') == 'network') || $multisiteTest )) { 
  70.  
  71. //if we use the network method or we send a test multisite email then we ovverride the from_email and the rest of the option 
  72. $optionsMsOverride = array('from_email' , 'sendmail_path' , 'smtp_rest' , 'smtp_host' , 'smtp_port' , 'smtp_secure' , 'smtp_auth' , 'smtp_login' , 'smtp_password'); 
  73. foreach($optionsMsOverride as $key) { 
  74. if(isset($this->config->values['ms_'.$key])) $this->config->values[$key]=$this->config->values['ms_'.$key]; 
  75.  
  76. // this distinction is important somehow the network sending method needs to be overriden after we pass that condfition above 
  77. if(is_multisite() && $this->config->getValue('sending_method')=='network') { 
  78. $sending_method = $this->config->getValue('ms_sending_method'); 
  79. }else{ 
  80. $sending_method = $this->config->getValue('sending_method'); 
  81.  
  82. $this->setFrom($this->config->getValue('from_email'), $this->config->getValue('from_name')); 
  83. $this->Sender = $this->cleanText($this->config->getValue('bounce_email')); 
  84. if(empty($this->Sender)) { 
  85. $this->Sender = ''; 
  86. switch ( $sending_method ) { 
  87. case 'gmail' : 
  88. case 'smtp' : 
  89. //if it is an elsatic email smtp then we simply use the restAPI 
  90. $this->Host = $this->config->getValue('smtp_host'); 
  91.  
  92. if(in_array(trim($this->Host), array('smtp.elasticemail.com', 'smtp25.elasticemail.com'))) { 
  93. //REST API! 
  94. include_once (WYSIJA_INC. 'mailer' . DS . 'class.elasticemail.php'); 
  95. $this->Mailer = 'elasticemail'; 
  96. $this->elasticEmail = new acymailingElasticemail(); 
  97. $this->elasticEmail->Username = trim($this->config->getValue('smtp_login')); 
  98. $this->elasticEmail->Password = trim($this->config->getValue('smtp_password')); 
  99. $this->isElasticRest=true; 
  100. }elseif(in_array(trim($this->Host), array('smtp.sendgrid.net')) && $this->config->getValue('smtp_rest')) { 
  101. //REST API! 
  102. include_once (WYSIJA_INC. 'mailer' . DS . 'class.sendgrid.php'); 
  103. $this->Mailer = 'sendgrid'; 
  104. $this->sendGrid = new acymailingSendgrid(); 
  105. $this->sendGrid->Username = trim($this->config->getValue('smtp_login')); 
  106. $this->sendGrid->Password = trim($this->config->getValue('smtp_password')); 
  107. $this->isSendGridRest=true; 
  108. }elseif(in_array(trim($this->Host), array('sparkpost.com'))) { 
  109. $this->Mailer = 'sparkpost'; 
  110. $this->sparkpost = new WJ_Sparkpost( $this->config->getValue('smtp_password') ); 
  111. }else{ 
  112. if(in_array(trim($this->Host), array('mailpoet.com'))) { 
  113. $this->Mailer = 'mailpoet'; 
  114. $this->mailpoet = new WJ_Bridge( $this->config->getValue('smtp_password') ); 
  115. }else{ 
  116. $this->IsSMTP(); 
  117. if(strpos($this->Host, 'mailjet.com')!==false) { 
  118. $this->isMailjet=true; 
  119. $port = $this->config->getValue('smtp_port'); 
  120. if(empty($port) && $this->config->getValue('smtp_secure') == 'ssl') $port = 465; 
  121. if(!empty($port)) $this->Host.= ':'.$port; 
  122. $this->SMTPAuth = (bool) $this->config->getValue('smtp_auth'); 
  123. $this->Username = trim($this->config->getValue('smtp_login')); 
  124. $this->Password = trim($this->config->getValue('smtp_password')); 
  125. $this->SMTPSecure = trim((string)$this->config->getValue('smtp_secure')); 
  126. if(empty($this->Sender)) $this->Sender = strpos($this->Username, '@') ? $this->Username : $this->config->getValue('from_email'); 
  127. break; 
  128. case 'site': 
  129. switch($this->config->getValue('sending_emails_site_method')) { 
  130. case 'phpmail': 
  131. $this->IsMail(); 
  132. break; 
  133. case 'sendmail': 
  134. $this->IsSendmail(); 
  135. $this->SendMail = trim($this->config->getValue('sendmail_path')); 
  136. if(empty($this->SendMail)) { 
  137. $this->SendMail = '/usr/sbin/sendmail'; 
  138. break; 
  139. case 'wpmail': 
  140.  
  141. $this->IsWPmail(); 
  142. break; 
  143.  
  144. break; 
  145. case 'qmail' : 
  146. $this->IsQmail(); 
  147. break; 
  148. default : 
  149. $this->IsMail(); 
  150. break; 
  151. }//endswitch 
  152.  
  153. $this->PluginDir = dirname(__FILE__).DS; 
  154. $this->CharSet = strtolower($this->config->getValue('advanced_charset')); 
  155. if(empty($this->CharSet)) { 
  156. $this->CharSet = 'utf-8'; 
  157. $this->clearAll(); 
  158. $this->Encoding = '8bit'; 
  159. //$this->Hostname = ''; 
  160. $this->WordWrap = 150; 
  161.  
  162. if($this->config->getValue('dkim_active') && $this->config->getValue('dkim_pubk') && !$this->isElasticRest && !$this->isSendGridRest && $this->Mailer !== 'mailpoet' && $this->Mailer !== 'sparkpost') { 
  163. // check that server can sign emails 
  164. if(!function_exists('openssl_sign')) { 
  165. $this->core->error(__('You cannot use the DKIM signature option...', WYSIJA).' '.__('The PHP Extension openssl is not enabled on your server. Ask your host to enable it if you want to use an SSL connection.', WYSIJA)); 
  166. }else{ 
  167. $this->DKIM_domain = $this->config->getValue('dkim_domain'); 
  168. $this->DKIM_private = tempnam(sys_get_temp_dir(), 'pk'); 
  169. file_put_contents($this->DKIM_private, trim($this->config->getValue('dkim_privk'))); 
  170.  
  171. $this->DKIM_selector = 'wys'; 
  172.  
  173. /** 
  174. * SMTP class overriding for Mailer versions < 5.2.7 
  175. */ 
  176. if ($this->Mailer == 'smtp' && !is_callable('parent::getSMTPInstance')) { 
  177. $this->getSMTPInstance(); 
  178.  
  179. function __destruct() { 
  180. if($this->config->getValue('dkim_active') 
  181. && file_exists($this->DKIM_private) 
  182. ) { 
  183. @unlink($this->DKIM_private); 
  184.  
  185. function IsWPmail() { 
  186. $this->is_wp_mail = true; 
  187. $this->Mailer = 'wpmail'; 
  188.  
  189. function send() { 
  190. // prevent user/script details being exposed in X-PHP-Script header 
  191. if ( isset( $_SERVER['REMOTE_ADDR'] ) && ! empty( $_SERVER['REMOTE_ADDR'] ) ) { 
  192. $_server_remote_addr = $_SERVER['REMOTE_ADDR']; 
  193. $_SERVER['REMOTE_ADDR'] = ( isset( $_SERVER['SERVER_ADDR'] ) ? $_SERVER['SERVER_ADDR'] : '127.0.0.1' ); 
  194.  
  195. if ( ! empty( $this->sendHTML ) ) { 
  196. $this->AltBody = $this->textVersion( $this->Body, true ); 
  197. $this->Body = $this->strip_unwanted_html( $this->Body ); 
  198. } else { 
  199. $this->Body = $this->textVersion( $this->Body, false ); 
  200.  
  201.  
  202. if(empty($this->ReplyTo)) { 
  203. $replyToName = $this->config->getValue('replyto_name'); 
  204. $this->AddReplyTo($this->config->getValue('replyto_email'), $replyToName); 
  205.  
  206. //Embed images if there is images to embed... 
  207. if((bool)$this->config->getValue('embed_images', 0) && !$this->isElasticRest && !$this->isSendGridRest) { 
  208. $this->embedImages(); 
  209.  
  210. if(empty($this->Subject) OR empty($this->Body)) { 
  211. $this->reportMessage = __('There is no subject or body in this email', WYSIJA); 
  212. $this->errorNumber = 8; 
  213. return false; 
  214.  
  215. if(function_exists('mb_convert_encoding') && !empty($this->sendHTML)) { 
  216. $this->Body = mb_convert_encoding($this->Body, 'HTML-ENTITIES', 'UTF-8'); 
  217. $this->Body = str_replace('&', '&', $this->Body); 
  218.  
  219. if($this->CharSet != 'utf-8') { 
  220. $this->Body = $this->encodingHelper->change($this->Body, 'UTF-8', $this->CharSet); 
  221. $this->Subject = $this->encodingHelper->change($this->Subject, 'UTF-8', $this->CharSet); 
  222. if(!empty($this->AltBody)) { 
  223. $this->AltBody = $this->encodingHelper->change($this->AltBody, 'UTF-8', $this->CharSet); 
  224.  
  225. if($this->isElasticRest) { 
  226. $this->addCustomHeader('referral:cfb09bc8-558d-496b-83e6-b05e901a945c'); 
  227.  
  228. $this->Subject = str_replace(array('**', '**', '**', '**'), array("'", '"', '"', '-'), $this->Subject); 
  229. $this->Body = str_replace(chr(194), chr(32), $this->Body); 
  230. ob_start(); 
  231.  
  232. $result = parent::Send(); 
  233.  
  234. $warnings = ob_get_clean(); 
  235. if(!empty($warnings) && strpos($warnings, 'bloque')) { 
  236. $result = false; 
  237. $receivers = array(); 
  238. foreach($this->to as $oneReceiver) { 
  239. $receivers[] = $oneReceiver[0]; 
  240. if(!$result) { 
  241. $this->reportMessage = sprintf(__('Error Sending Message <b><i>%s</i></b> to <b><i>%s</i></b>', WYSIJA), $this->Subject, esc_html(implode('", "', $receivers))); 
  242. if(!empty($this->ErrorInfo)) { 
  243. //echo $this->ErrorInfo; 
  244. foreach($this->ErrorInfo as $error) { 
  245. $this->reportMessage.=' | '.$error['error']; 
  246. $this->ErrorInfo=array(); 
  247. //$this->core->error($this->ErrorInfo); 
  248. //$this->reportMessage(); 
  249.  
  250. if(!empty($warnings)) $this->reportMessage .= ' | '.$warnings; 
  251. $this->errorNumber = 1; 
  252. if($this->report) { 
  253. $this->core->error($this->reportMessage); 
  254.  
  255. }else{ 
  256. $this->reportMessage = sprintf(__('Successfully sent to <b><i>%s</i></b>', WYSIJA), esc_html(implode('", "', $receivers))); 
  257.  
  258. if($this->report) { 
  259. if(!empty($warnings)) { 
  260. $this->reportMessage .= ' | '.$warnings; 
  261. $this->core->notice($this->reportMessage, false); 
  262.  
  263. if ( isset( $_SERVER['REMOTE_ADDR'] ) && ! empty( $_SERVER['REMOTE_ADDR'] ) ) { 
  264. // restore obfuscated server variables 
  265. $_SERVER['REMOTE_ADDR'] = $_server_remote_addr; 
  266.  
  267. return $result; 
  268.  
  269. function load( $email_id ) { 
  270. $mailClass = WYSIJA::get( 'email', 'model' ); 
  271. $mailClass->getFormat = OBJECT; 
  272. $this->defaultMail[ $email_id ] = $mailClass->getOne( $email_id ); 
  273.  
  274. if ( ! is_array( $this->defaultMail[ $email_id ]->params ) ) { 
  275. $this->defaultMail[ $email_id ]->params = unserialize( base64_decode( $this->defaultMail[ $email_id ]->params ) ); 
  276.  
  277. $this->defaultMail[ $email_id ]->attach = $this->defaultMail[ $email_id ]->attachments; 
  278. unset( $this->defaultMail[ $email_id ]->attachments ); 
  279.  
  280. if ( empty( $this->defaultMail[ $email_id ]->email_id ) ) { 
  281. return false; 
  282.  
  283. if ( empty( $this->defaultMail[ $email_id ]->altbody ) ) { 
  284. $this->defaultMail[ $email_id ]->altbody = $this->textVersion( $this->defaultMail[ $email_id ]->body ); 
  285.  
  286. if ( ! empty( $this->defaultMail[ $email_id ]->attach ) ) { 
  287. $this->defaultMail[ $email_id ]->attachments = array(); 
  288. $uploadFolder = str_replace( array( '/', '\\' ), DS, html_entity_decode( $this->config->getValue( 'uploadfolder' ) ) ); 
  289. $uploadFolder = trim( $uploadFolder, DS . ' ' ) . DS; 
  290. $uploadPath = str_replace( array( '/', '\\' ), DS, $uploadFolder ); 
  291. $uploadURL = $this->config->getValue( 'uploadurl' ); 
  292.  
  293. foreach ( $this->defaultMail[ $email_id ]->attach as $oneAttach ) { 
  294. $attach = new StdClass(); 
  295. $attach->name = $oneAttach->filename; 
  296. $attach->filename = $uploadPath.$oneAttach->filename; 
  297. $attach->url = $uploadURL.$oneAttach->filename; 
  298. $this->defaultMail[ $email_id ]->attachments[] = $attach; 
  299.  
  300. $this->recordEmail( $email_id ); 
  301.  
  302. return $this->defaultMail[ $email_id ]; 
  303.  
  304. function wpmail_init( $mailer ) { 
  305. $mailer->ClearCustomHeaders(); 
  306. $mailer->Body = $this->AltBody; 
  307. $mailer->AltBody = $this->AltBody; 
  308. $mailer->Subject = $this->Subject; 
  309. $mailer->From = $this->From; 
  310. $mailer->FromName = $this->FromName; 
  311. $mailer->Sender = $this->Sender; 
  312. $mailer->MessageID = $this->MessageID; 
  313.  
  314. $mailer->AddAddress( $this->to[0][0], $this->to[0][1] ); 
  315. $mailer->AddReplyTo( $this->ReplyTo[0][0], $this->ReplyTo[0][1] ); 
  316.  
  317. $mailer->CharSet = $this->CharSet; 
  318. $mailer->Encoding = $this->Encoding; 
  319. $mailer->WordWrap = $this->WordWrap; 
  320.  
  321. return $mailer; 
  322.  
  323. function recordEmail( $email_id, $email_object = false ) { 
  324. if ( $email_object && ! isset( $this->defaultMail[ $email_id ] ) ) { 
  325. $this->defaultMail[ $email_id ] = $email_object; 
  326. /**parsin is done once the email is recorded after load*/ 
  327.  
  328. if ( $this->isMailjet ) { 
  329. $this->defaultMail[ $email_id ]->mailjetid = get_option( 'siteurl' ) . '-' . $this->defaultMail[ $email_id ]->email_id; 
  330.  
  331. $this->parseUserTags( $this->defaultMail[ $email_id ] ); 
  332. $this->parseSubjectUserTags( $this->defaultMail[ $email_id ] ); 
  333. $this->parseRelativeURL( $this->defaultMail[ $email_id ] ); 
  334.  
  335. add_action( 'wysija_replacetags', array( $this, 'replacetags' ) ); 
  336. do_action( 'wysija_replacetags', $email_id ); 
  337.  
  338. public function strip_unwanted_html( $content ) { 
  339. // Remove Script Tags 
  340. $content = preg_replace( '#< *script(?:(?!< */ *script *>).)*< */ *script *>#isU', '', $content ); 
  341.  
  342. return $content; 
  343.  
  344. function parseUserTags(&$emailobj) { 
  345.  
  346. if(!isset($emailobj->tags) || !$emailobj->tags) { 
  347. preg_match_all("#\[([^\]]*):([^\]|]*)([^\]]*)\]#Uis", $emailobj->body, $values_user); 
  348. $tags=array(); 
  349. foreach($values_user[0] as $tag ) { 
  350.  
  351. $tags[$tag]=explode(' | ', str_replace(array('[', ']'), '', $tag)); 
  352. foreach($tags[$tag] as &$arg) { 
  353. $arg=explode(':', $arg); 
  354. $emailobj->tags=$tags; 
  355.  
  356. if(!isset($emailobj->tags) || !$emailobj->tags)$emailobj->tags=array(); 
  357.  
  358.  
  359. function parseSubjectUserTags(&$emailobj) { 
  360.  
  361. if(!isset($emailobj->subject_tags) || !$emailobj->subject_tags) { 
  362. preg_match_all("#\[([^\]]*):([^\]|]*)([^\]]*)\]#Uis", $emailobj->subject, $values_user); 
  363. $tags=array(); 
  364. foreach($values_user[0] as $tag ) { 
  365.  
  366. $tags[$tag]=explode(' | ', str_replace(array('[', ']'), '', $tag)); 
  367. foreach($tags[$tag] as &$arg) { 
  368. $arg=explode(':', $arg); 
  369. $emailobj->subject_tags = $tags; 
  370.  
  371. if(!isset($emailobj->subject_tags) || !$emailobj->subject_tags)$emailobj->subject_tags=array(); 
  372.  
  373.  
  374.  
  375.  
  376. function parseRelativeURL(&$emailobj) { 
  377. static $mainurl = ''; 
  378. $siteurl=get_option('siteurl'); 
  379. $lastchar=substr($siteurl, -1); 
  380. if($lastchar!='/')$siteurl.='/'; 
  381.  
  382. if(empty($mainurl)) { 
  383.  
  384. $urls = parse_url($siteurl); 
  385. if(!empty($urls['path'])) { 
  386. $mainurl = substr($siteurl, 0, strrpos($siteurl, $urls['path'])).'/'; 
  387. }else{ 
  388. $mainurl = $siteurl; 
  389.  
  390. //It will remove the undefined thing added by tinyMCE 
  391. //$text = str_replace(array('href="../undefined/', 'href="../../undefined/', 'href="../../../undefined//', 'href="undefined/'), array('href="'.$mainurl, 'href="'.$mainurl, 'href="'.$mainurl, 'href="'.$siteurl), $text); 
  392. //We remove errors with /administrator links and our tags 
  393. //$text = preg_replace('#href="/?administrator/({|%7B)#Uis', 'href="$1', $text); 
  394.  
  395. $replace = array(); 
  396. $replaceBy = array(); 
  397. //We don't consider { as absolute url otherwise it could break a tag 
  398. if($mainurl !== $siteurl) { 
  399. //url like ../ your site... 
  400. //We don't transform mailto: # http:// ... 
  401. $replace[] = '#(href|src|action|background)[ ]*=[ ]*\"(?!(\{|%7B|\#|\[|[a-z]{3, 7}:|/))(?:\.\./)#i'; 
  402. $replaceBy[] = '$1="'.substr($siteurl, 0, strrpos(rtrim($siteurl, '/'), '/')+1); 
  403. $replace[] = '#(href|src|action|background)[ ]*=[ ]*\"(?!(\{|%7B|\#|\[|[a-z]{3, 7}:|/))(?:\.\./|\./)?#i'; 
  404. $replaceBy[] = '$1="'.$siteurl; 
  405. $replace[] = '#(href|src|action|background)[ ]*=[ ]*\"(?!(\{|%7B|\#|\[|[a-z]{3, 7}:))/#i'; 
  406. $replaceBy[] = '$1="'.$mainurl; 
  407.  
  408. //background images for div 
  409. $replace[] = '#(background-image[ ]*:[ ]*url\(\'?"?(?!([a-z]{3, 7}:|/|\'|"))(?:\.\./|\./)?)#i'; 
  410. $replaceBy[] = '$1'.$siteurl; 
  411.  
  412. $emailobj->body=preg_replace($replace, $replaceBy, $emailobj->body); 
  413. return true; 
  414.  
  415.  
  416. function clearAll() { 
  417. $this->Subject = ''; 
  418. $this->Body = ''; 
  419. $this->AltBody = ''; 
  420. $this->ClearAllRecipients(); 
  421. $this->ClearAttachments(); 
  422. $this->ClearCustomHeaders(); 
  423. $this->ClearReplyTos(); 
  424. $this->errorNumber = 0; 
  425. $this->setFrom($this->config->getValue('from_email'), $this->config->getValue('from_name')); 
  426.  
  427. function sendOne($email_id, $receiverid, $confirmEmail=false) { 
  428.  
  429. $this->clearAll(); 
  430. if(is_object($email_id)) { 
  431. $emailObj=$email_id; 
  432. $email_id=$email_id->email_id; 
  433. $this->recordEmail($email_id, $emailObj); 
  434.  
  435. if(!isset($this->defaultMail[$email_id])) { 
  436. if(!$this->load($email_id)) { 
  437. $this->reportMessage = 'Can not load the email : '.$email_id; 
  438. if($this->report) { 
  439. $this->core->error($this->reportMessage); 
  440. $this->errorNumber = 2; 
  441. return false; 
  442.  
  443. if(has_filter('mpoet_mailer_hostname')) { 
  444. $this->Hostname = apply_filters('mpoet_mailer_hostname', $this->Hostname); 
  445. /**if(!empty($this->forceTemplate) AND empty($this->defaultMail[$email_id]->tempid)) { 
  446. $this->defaultMail[$email_id]->tempid = $this->forceTemplate; 
  447. }*/ 
  448. $this->addCustomHeader( 'X-email_id: ' . $this->defaultMail[$email_id]->email_id ); 
  449.  
  450. if(isset($this->defaultMail[$email_id]->mailjetid)) { 
  451. $this->addCustomHeader( 'X-Mailjet-Campaign: ' . $this->defaultMail[$email_id]->mailjetid); 
  452.  
  453. if(!isset($this->forceVersion) AND empty($this->defaultMail[$email_id]->status)) { 
  454.  
  455. $this->reportMessage = sprintf(__('The email ID %s is not published', WYSIJA), $email_id); 
  456. $this->errorNumber = 3; 
  457. if($this->report) { 
  458. $this->core->error($this->reportMessage); 
  459. return false; 
  460.  
  461. if(!is_object($receiverid)) { 
  462. $this->subscriberClass->getFormat = OBJECT; 
  463. $receiver = $this->subscriberClass->getOne($receiverid); 
  464.  
  465. if(!$receiver) { 
  466. $userHelper = WYSIJA::get('user', 'helper'); 
  467.  
  468. if($userHelper->validEmail($receiverid)) { 
  469. $this->subscriberClass->getFormat = OBJECT; 
  470. $receiver = $this->subscriberClass->getOne(false, array('email'=>$receiverid)); 
  471.  
  472.  
  473.  
  474. if((!$receiver || empty($receiver->user_id)) AND is_string($receiverid) AND $this->autoAddUser) { 
  475.  
  476. $userHelper = WYSIJA::get('user', 'helper'); 
  477. if($userHelper->validEmail($receiverid)) { 
  478. $newUser = array(); 
  479. $newUser['email'] = $receiverid; 
  480. $newUser['status'] = 1; 
  481. if(isset($this->wp_user))$newUser['wpuser_id'] = $this->wp_user->ID; 
  482. $this->subscriberClass->checkVisitor = false; 
  483. $this->subscriberClass->sendConf = false; 
  484.  
  485. $user_id = $this->subscriberClass->insert($newUser); 
  486. $this->subscriberClass->getFormat = OBJECT; 
  487. $receiver = $this->subscriberClass->getOne($user_id); 
  488.  
  489. }else{ 
  490. $receiver = $receiverid; 
  491. if(empty($receiver->email)) { 
  492. $this->reportMessage = sprintf(__('Subscriber not found : <b><i>%s</i></b>', WYSIJA), isset($receiver->user_id) ? (int)$receiver->user_id : (int)$receiverid); 
  493. if($this->report) { 
  494. $this->core->error($this->reportMessage); 
  495. $this->errorNumber = 4; 
  496. return false; 
  497.  
  498. // message id to recognise it when using the bounce 
  499. $message_id=base64_encode(rand(0, 9999999)).'WY'.(int)$receiver->user_id; 
  500. $message_id.='SI'.(int)$this->defaultMail[$email_id]->email_id; 
  501.  
  502. // if we are sending the email from a multisite the message id needs to include the site id too 
  503. if(is_multisite()) { 
  504. global $blog_id; 
  505. $message_id.='JA'.(int)$blog_id; 
  506. $message_id.='MS'.base64_encode(time().rand(0, 99999)); 
  507. }else{ 
  508. $message_id.='JA'.base64_encode(time().rand(0, 99999)); 
  509.  
  510. $this->MessageID = '<'.preg_replace("|[^a-z0-9+_]|i", '', $message_id).'@'. $this->ServerHostname().'>'; 
  511. //$this->addCustomHeader( 'X-Subid: ' . $receiver->user_id ); 
  512. if(!isset($this->forceVersion)) { 
  513. if(/**!isset($this->defaultMail[$email_id]->simple) &&*/ $this->checkConfirmField AND empty($receiver->status) AND $this->config->getValue('confirm_dbleoptin')==1 AND $email_id != $this->config->getValue('confirm_email_id')) { 
  514. $this->reportMessage = sprintf(__($this->config->getValue('confirm_dbleoptin').' The subscriber <b><i>%s</i></b> is not confirmed', WYSIJA), esc_html($receiver->email)); 
  515. if($this->report) { 
  516. $this->core->error($this->reportMessage); 
  517. $this->errorNumber = 5; 
  518. return false; 
  519.  
  520. $addedName = $this->cleanText($receiver->firstname.' '.$receiver->lastname); 
  521. $this->AddAddress($this->cleanText($receiver->email), $addedName); 
  522. if(!isset($this->forceVersion)) { 
  523. $this->IsHTML(true); 
  524. }else{ 
  525. $this->IsHTML(false); 
  526. $this->Subject = $this->defaultMail[$email_id]->subject; 
  527. if($this->sendHTML) { 
  528. $this->Body = $this->defaultMail[$email_id]->body; 
  529. if($confirmEmail) { 
  530. $this->Body = nl2br($this->Body ); 
  531.  
  532. if($this->config->getValue('multiple_part', false)) { 
  533. $this->AltBody = $this->defaultMail[$email_id]->altbody; 
  534. }else{ 
  535. $this->Body = $this->defaultMail[$email_id]->altbody; 
  536.  
  537.  
  538.  
  539. if(!empty($this->defaultMail[$email_id]->replyto_email)) { 
  540. $replyToName = $this->cleanText($this->defaultMail[$email_id]->replyto_name) ; 
  541. $this->AddReplyTo($this->cleanText($this->defaultMail[$email_id]->replyto_email), $replyToName); 
  542. if(!empty($this->defaultMail[$email_id]->attachments)) { 
  543. foreach($this->defaultMail[$email_id]->attachments as $attachment) { 
  544. $this->AddAttachment($attachment->filename); 
  545. if(!empty($this->parameters)) { 
  546. $keysparams = array_keys($this->parameters); 
  547. $this->Subject = str_replace($keysparams, $this->parameters, $this->Subject); 
  548. $this->Body = str_replace($keysparams, $this->parameters, $this->Body); 
  549. if(!empty($this->AltBody)) $this->AltBody = str_replace($keysparams, $this->parameters, $this->AltBody); 
  550.  
  551.  
  552. $is_multisite=is_multisite(); 
  553.  
  554. //$is_multisite=true;//PROD comment that line 
  555. //if we are in a multisite situation and there is one sending method set for all the sites then we just force the from email 
  556. if($is_multisite && $this->config->getValue('sending_method')=='network') { 
  557. $this->defaultMail[$email_id]->from_email=$this->config->getValue('ms_from_email'); 
  558.  
  559. $this->setFrom($this->defaultMail[$email_id]->from_email, $this->defaultMail[$email_id]->from_name); 
  560.  
  561. /**$this->Body=stripslashes($this->Body); 
  562. $this->Subject=stripslashes($this->Subject);*/ 
  563.  
  564.  
  565.  
  566. $mailforTrigger = new StdClass(); 
  567. $mailforTrigger->body = &$this->Body; 
  568. //$mailforTrigger->altbody = &$this->AltBody; 
  569. $mailforTrigger->subject = &$this->Subject; 
  570. $mailforTrigger->from = &$this->From; 
  571. $mailforTrigger->fromName = &$this->FromName; 
  572. $mailforTrigger->replyto = &$this->ReplyTo; 
  573. $mailforTrigger->replyname = &$this->defaultMail[$email_id]->replyname; 
  574. $mailforTrigger->replyemail = &$this->defaultMail[$email_id]->replyemail; 
  575. $mailforTrigger->email_id = $this->defaultMail[$email_id]->email_id; 
  576. $mailforTrigger->type = &$this->defaultMail[$email_id]->type; 
  577.  
  578. if(isset($this->defaultMail[$email_id]->params)) { 
  579. if(!is_array($this->defaultMail[$email_id]->params)) $this->defaultMail[$email_id]->params=unserialize(base64_decode($this->defaultMail[$email_id]->params)); 
  580. $mailforTrigger->params = $this->defaultMail[$email_id]->params; 
  581.  
  582. $mailforTrigger->tags = &$this->defaultMail[$email_id]->tags; 
  583. $mailforTrigger->subject_tags = &$this->defaultMail[$email_id]->subject_tags; 
  584.  
  585. add_action('wysija_replaceusertags', array($this, 'replaceusertags'), 10, 2); 
  586. add_action('wysija_replaceusertags', array($this, 'tracker_replaceusertags'), 11, 2); 
  587. add_action('wysija_replaceusertags', array($this, 'openrate_replaceusertags'), 12, 2); 
  588.  
  589. do_action( 'wysija_replaceusertags', $mailforTrigger, $receiver); 
  590.  
  591. if(!empty($mailforTrigger->customHeaders)) { 
  592. foreach($mailforTrigger->customHeaders as $oneHeader) { 
  593. $this->addCustomHeader( $oneHeader ); 
  594.  
  595. return $this->send(); 
  596.  
  597. function embedImages() { 
  598. preg_match_all('/(src|background)="([^"]*)"/Ui', $this->Body, $images); 
  599. $result = true; 
  600. if(!empty($images[2])) { 
  601. $mimetypes = array('bmp' => 'image/bmp',  
  602. 'gif' => 'image/gif',  
  603. 'jpeg' => 'image/jpeg',  
  604. 'jpg' => 'image/jpeg',  
  605. 'jpe' => 'image/jpeg',  
  606. 'png' => 'image/png',  
  607. 'tiff' => 'image/tiff',  
  608. 'tif' => 'image/tiff'); 
  609. $allimages = array(); 
  610. foreach($images[2] as $i => $url) { 
  611. if(isset($allimages[$url])) continue; 
  612. $allimages[$url] = 1; 
  613. $path = str_replace(array($this->config->getValue('uploadurl'), '/'), array($this->config->getValue('uploadfolder'), DS), urldecode($url)); 
  614. $filename = basename($url); 
  615. $md5 = md5($filename); 
  616. $cid = 'cid:' . $md5; 
  617. $fileParts = explode(".", $filename); 
  618. $ext = strtolower($fileParts[1]); 
  619. if(!isset($mimetypes[$ext])) continue; 
  620. $mimeType = $mimetypes[$ext]; 
  621. if($this->AddEmbeddedImage($path, $md5, $filename, 'base64', $mimeType)) { 
  622. $this->Body = preg_replace("/".$images[1][$i]."=\"".preg_quote($url, '/')."\"/Ui", $images[1][$i]."=\"".$cid."\"", $this->Body); 
  623. }else{ 
  624. $result = false; 
  625. return $result; 
  626. function textVersion($html, $fullConvert = true) { 
  627. @ini_set('pcre.backtrack_limit', 1000000); 
  628. //$html = acymailing_absoluteURL($html); 
  629. if($fullConvert) { 
  630. $html = preg_replace('# +#', ' ', $html); 
  631. $html = str_replace(array("\n", "\r", "\t"), '', $html); 
  632. $removepictureslinks = "#< *a[^>]*> *< *img[^>]*> *< *\/ *a *>#isU"; 
  633. $removeScript = "#< *script(?:(?!< */ *script *>).)*< */ *script *>#isU"; 
  634. $removeStyle = "/<style\\b[^>]*>(.*?)<\\/style>/s"; 
  635. $removeStrikeTags = '#< *strike(?:(?!< */ *strike *>).)*< */ *strike *>#iU'; 
  636. $replaceByTwoReturnChar = '#< *(h1|h2)[^>]*>#Ui'; 
  637. $replaceByStars = '#< *li[^>]*>#Ui'; 
  638. $replaceByReturnChar1 = '#< */ *(li|td|tr|div|p)[^>]*> *< *(li|td|tr|div|p)[^>]*>#Ui'; 
  639. $replaceByReturnChar = '#< */? *(br|p|h1|h2|legend|h3|li|ul|h4|h5|h6|tr|td|div)[^>]*>#Ui'; 
  640. $replaceLinks = '/< *a[^>]*href *= *"([^#][^"]*)"[^>]*>(.*)< *\/ *a *>/Uis'; 
  641. $text = preg_replace(array($removepictureslinks, $removeScript, $removeStyle, $removeStrikeTags, $replaceByTwoReturnChar, $replaceByStars, $replaceByReturnChar1, $replaceByReturnChar, $replaceLinks), array('', '', '', '', "\n\n", "\n* ", "\n", "\n", '${2} ( ${1} )'), $html); 
  642. $text = str_replace(array(" ", " "), ' ', strip_tags($text)); 
  643. $text = trim(@html_entity_decode($text, ENT_QUOTES, 'UTF-8')); 
  644. if($fullConvert) { 
  645. $text = preg_replace('# +#', ' ', $text); 
  646. $text = preg_replace('#\n *\n\s+#', "\n\n", $text); 
  647. return $text; 
  648. function cleanText($text) { 
  649. return trim( preg_replace( '/(%0A|%0D|\n+|\r+)/i', '', (string) $text ) ); 
  650. function setFrom($email, $name='', $auto=true) { 
  651. if(!empty($email)) { 
  652. $this->From = $this->cleanText($email); 
  653. if(!empty($name)) { 
  654. $this->FromName = $this->cleanText($name); 
  655. function addParamInfo() { 
  656. if(!empty($_SERVER)) { 
  657. $serverinfo = array(); 
  658. foreach($_SERVER as $oneKey => $oneInfo) { 
  659. $serverinfo[] = $oneKey.' => '.strip_tags(print_r($oneInfo, true)); 
  660. $this->addParam('serverinfo', implode('<br />', $serverinfo)); 
  661. if(!empty($_REQUEST)) { 
  662. $postinfo = array(); 
  663. foreach($_REQUEST as $oneKey => $oneInfo) { 
  664. $postinfo[] = $oneKey.' => '.strip_tags(print_r($oneInfo, true)); 
  665. $this->addParam('postinfo', implode('<br />', $postinfo)); 
  666. function addParam($name, $value) { 
  667. $tagName = '{'.$name.'}'; 
  668. $this->parameters[$tagName] = $value; 
  669.  
  670. function sendSimple($sendto, $subject, $body, $params=array(), $format='text') { 
  671. $modelConfig=WYSIJA::get('config', 'model'); 
  672. $emailObj=new StdClass(); 
  673.  
  674.  
  675. $emailObj->email_id=0; 
  676. if(isset($params['email_id'])) $emailObj->email_id=$params['email_id']; 
  677. while(isset($this->defaultMail[$emailObj->email_id])) { 
  678. $emailObj->email_id=$emailObj->email_id-1; 
  679. $emailObj->subject=$subject; 
  680. $emailObj->body=$body; 
  681.  
  682. $emailObj->status=1; 
  683. $emailObj->attachments=""; 
  684. //dbg($modelConfig); 
  685. if(isset($params['from_name'])) $emailObj->from_name=$params['from_name']; 
  686. else $emailObj->from_name=$modelConfig->getValue('from_name'); 
  687. if(isset($params['from_email'])) $emailObj->from_email=$params['from_email']; 
  688. else $emailObj->from_email=$modelConfig->getValue('from_email'); 
  689. if(isset($params['replyto_name'])) $emailObj->replyto_name=$params['replyto_name']; 
  690. else $emailObj->replyto_name=$modelConfig->getValue('replyto_name'); 
  691. if(isset($params['replyto_email'])) $emailObj->replyto_email=$params['replyto_email']; 
  692. else $emailObj->replyto_email=$modelConfig->getValue('replyto_email'); 
  693.  
  694. if(isset($params['params'])) $emailObj->params=$params['params']; 
  695. //dbg($emailObj); 
  696. //dbg($emailObj); 
  697. $emailObj->mail_format=$format; 
  698. $emailObj->simple=1; 
  699. //if(!is_string($sendto)) $this->autoAddUser=true; 
  700.  
  701. /** no need to check the doubleoptin */ 
  702. $this->checkConfirmField=false; 
  703.  
  704. if(!$this->testemail) { 
  705. /**if($emailObj->email_id!=$this->config->getValue('confirm_email_id')) { 
  706. $emailObj->body.="[subscriptions_links]"; 
  707. $emailObj->body.="\n[subscriptions_linksfooter_address]"; 
  708. }*/ 
  709.  
  710. add_action('wysija_replacetags', array($this, 'replacetags')); 
  711. do_action( 'wysija_replacetags', array(&$emailObj)); 
  712.  
  713. /**$receiverObj=null; 
  714. $receiverObj->email=$sendto; 
  715. $receiverObj->firstname=""; 
  716. $receiverObj->keyuser=""; 
  717. $receiverObj->user_id=0;*/ 
  718. //dbg($emailObj); 
  719. if(is_string($sendto)) { 
  720. $dummyreceiver=new stdClass(); 
  721. $dummyreceiver->user_id=0; 
  722. $dummyreceiver->email=trim($sendto); 
  723. $dummyreceiver->status=1; 
  724. $dummyreceiver->lastname=$dummyreceiver->firstname =''; 
  725. }else $dummyreceiver=$sendto; 
  726. return $this->sendOne($emailObj, $dummyreceiver); 
  727.  
  728. function replacetags($email_id) { 
  729.  
  730. $find=array(); 
  731. $replace=array(); 
  732.  
  733. $find[]='[unsubscribe_linklabel]'; 
  734. if(!isset($this->config->values['unsubscribe_linkname'])) $replace[]=__('Unsubscribe', WYSIJA); 
  735. else $replace[]=$this->config->getValue('unsubscribe_linkname'); 
  736.  
  737. $this->defaultMail[$email_id]->body=str_replace($find, $replace, $this->defaultMail[$email_id]->body); 
  738.  
  739.  
  740. function replaceusertags($email, $receiver) { 
  741.  
  742. $arrayfind = array(); 
  743. $arrayreplace = array(); 
  744.  
  745. // Load shortcodes helper and replace user shortcodes. 
  746. $shortcodesH = WYSIJA::get('shortcodes', 'helper'); 
  747. $email->subject = $shortcodesH->replace_subject($email, $receiver); 
  748. $email->body = $shortcodesH->replace_body($email, $receiver); 
  749.  
  750. // TODO I think we can remove that subscriptions_links tag 
  751. $arrayfind[]='[subscriptions_links]'; 
  752. if(!empty($receiver)) $subscriptions_links='<div>'.$this->subscriberClass->getUnsubLink($receiver).'</div>'; 
  753. else $subscriptions_links=''; 
  754.  
  755. $arrayreplace[]=$subscriptions_links; 
  756. // end TODO 
  757.  
  758. if($email->email_id == $this->config->getValue('confirm_email_id')) { 
  759. $this->subscriberClass->reset(); 
  760. $activation_link=$this->subscriberClass->getConfirmLink($receiver, 'subscribe', false, true); 
  761. $listids=''; 
  762. if($this->listids) { 
  763. $listids='&wysiconf='.base64_encode(json_encode($this->listids)); 
  764. $arrayfind[]='[lists_to_confirm]'; 
  765. if(!$this->listnames) $arrayreplace[]=''; 
  766. else $arrayreplace[]='<strong>'.implode(', ', $this->listnames).'</strong>'; 
  767. $activation_link.=''; 
  768. $arrayfind[]='[activation_link]'; 
  769. $arrayreplace[]='<a href="'.$activation_link.$listids.'" target="_blank">'; 
  770.  
  771. $arrayfind[]='[/activation_link]'; 
  772. $arrayreplace[]='</a>'; 
  773.  
  774. $email->body=str_replace($arrayfind, $arrayreplace, $email->body); 
  775. $email->subject=str_replace($arrayfind, $arrayreplace, $email->subject); 
  776. $email->subject = ($email->subject) ? $email->subject : " "; 
  777.  
  778. /** 
  779. * Embed GA tracking code into a link 
  780. * @param string $link 
  781. * @param string $tracking_code 
  782. * @param string $media (email, web) 
  783. * @return string 
  784. */ 
  785. protected function add_ga_tracking_code($link, $tracking_code, $media = 'email') { 
  786. if (!$this->is_wysija_link($link) && $this->is_internal_link($link)) { 
  787. $hash_part_url = ''; 
  788. $argsp = array(); 
  789.  
  790. if (strpos($link, '#') !== false) 
  791. $hash_part_url = substr($link, strpos($link, '#')); 
  792. $link = substr($link, 0, strpos($link, '#')); 
  793.  
  794. $argsp['utm_source'] = 'wysija'; 
  795. $argsp['utm_medium'] = !empty($media) ? trim($media) : 'email'; 
  796. $argsp['utm_campaign'] = trim($tracking_code); 
  797.  
  798. $link .= $this->get_started_character_of_param($link); 
  799. $link .= http_build_query($argsp); 
  800. $link .= $hash_part_url; 
  801. return $link; 
  802.  
  803. /** 
  804. * Detects if a link is on a same website 
  805. * @param string $link 
  806. * @access public 
  807. * @return boolean 
  808. */ 
  809. public function is_internal_link($link) { 
  810. $helper_toolbox = WYSIJA::get('toolbox', 'helper'); 
  811. return (strpos($link, $helper_toolbox->_make_domain_name(site_url())) !== false); 
  812.  
  813. /** 
  814. * is a wysija link 
  815. * @param string $link 
  816. * @access public 
  817. * @return boolean 
  818. */ 
  819. public function is_wysija_link($link) { 
  820. // @todo: we should use pattern to remove all links with [] 
  821. return ( strpos( $link, '[view_in_browser_link]' ) !== false || strpos( $link, '[unsubscribe_link]' ) !== false || strpos( $link, '[subscriptions_link]' ) !== false ); 
  822.  
  823. /** 
  824. * Get a character which is between the original url and the next param 
  825. * @param string $link 
  826. * @access public 
  827. * @return string 
  828. */ 
  829. public function get_started_character_of_param($link) { 
  830. return (strpos($link, '?') !== false) ? '&' : '?'; 
  831.  
  832. function tracker_replaceusertags($email, $user) { 
  833. $urls = array(); 
  834. $results = array();// collect all links in email 
  835. if(!preg_match_all('#href[ ]*=[ ]*"(?!mailto:|tel:|\#|ymsgr:|callto:|file:|ftp:|webcal:|skype:)([^"]+)"#Ui', $email->body, $results)) return; 
  836.  
  837. $modelConf=WYSIJA::get('config', 'model'); 
  838.  
  839. foreach($results[1] as $i => $url) { 
  840. if(isset($urls[$results[0][$i]])|| strpos($url, 'wysija-key')) continue; 
  841.  
  842. $email_url = $url; 
  843. // Embed Google Analytics tracking code 
  844. if (isset($email->params['googletrackingcode']) && trim($email->params['googletrackingcode'])) { 
  845. $email_url = $this->add_ga_tracking_code($email_url, trim($email->params['googletrackingcode'])); 
  846.  
  847. if(!empty($email->params)) $email_url = apply_filters('mpoet_email_url', $email_url, $email->params); 
  848.  
  849. $Wysijaurls=array(); 
  850. $Wysijaurls['action=unsubscribe']='[unsubscribe_link]'; 
  851. $Wysijaurls['action=subscriptions']='[subscriptions_link]'; 
  852. $Wysijaurls['action=viewinbrowser']='[view_in_browser_link]'; 
  853. $urlsportions=array_keys($Wysijaurls); 
  854.  
  855. if(preg_match('#'.implode('|', $urlsportions).'|\{|%7B#i', $email_url)) { 
  856. foreach($Wysijaurls as $k =>$v) { 
  857. if(strpos($email_url, $k)!==false) { 
  858. if($modelConf->getValue('urlstats_base64')) { 
  859. $cururl=base64_encode($v); 
  860. }else{ 
  861. $cururl=$v; 
  862. $urlencoded=urlencode($cururl); 
  863. break; 
  864. }else{ 
  865. //make sure that broken url don't fuck the reidrection up like a space before http:// 
  866. $email_url=trim($email_url); 
  867. if($modelConf->getValue('urlstats_base64')) { 
  868. $cururl=base64_encode($email_url); 
  869. }else{ 
  870. $cururl=$email_url; 
  871. $urlencoded=urlencode($cururl); 
  872.  
  873. $args = array(); 
  874. $args['email_id'] = $email->email_id; 
  875. $args['user_id'] = $user->user_id; 
  876. if(empty($user->user_id)) { 
  877. $args['demo']=1; 
  878. $args['urlpassed'] = $urlencoded; 
  879. $args['controller'] = 'stats'; 
  880.  
  881. $page_id = $modelConf->getValue('confirm_email_link'); 
  882. //if it's a system url that needs privacy we hash it 
  883. if(strpos($email_url, '[unsubscribe_link]')!==false) { 
  884. $args['hash']=md5(AUTH_KEY.'[unsubscribe_link]'.$args['user_id']); 
  885. $page_id = $modelConf->getValue('unsubscribe_page'); 
  886.  
  887. if(strpos($email_url, '[subscriptions_link]')!==false) { 
  888. $args['hash']=md5(AUTH_KEY.'[subscriptions_link]'.$args['user_id']); 
  889. $page_id = $modelConf->getValue('subscriptions_page'); 
  890.  
  891.  
  892. $forbiddenparams=$modelConf->getValue('params_forbidden'); 
  893. if(isset($forbiddenparams['controller']['stats'])) $args['controller'] = $forbiddenparams['controller']['stats']; 
  894.  
  895. $args['action'] = 'analyse'; 
  896. $args['wysija-page'] = 1; 
  897. if(!$modelConf->getValue('urlstats_base64')) { 
  898. $args['no64'] = 1; 
  899.  
  900. $mytracker=WYSIJA::get_permalink($modelConf->getValue($page_id), $args); 
  901.  
  902. // overwrite unsubscribe link when sending with Elastic Email 
  903. if(strpos($email_url, '[unsubscribe_link]')!==false) { 
  904. $mailer_host = $modelConf->getValue('smtp_host'); 
  905. $mytracker = (preg_match('/elastic/i', $mailer_host)) ? "{unsubscribe:{$mytracker}}" : $mytracker; 
  906.  
  907. $urls[$results[0][$i]] = str_replace($url, $mytracker, $results[0][$i]); 
  908. $email->body = str_replace(array_keys($urls), $urls, $email->body); 
  909.  
  910.  
  911.  
  912. function openrate_replaceusertags( $email, $user ) { 
  913. //$typemails=array('news', 'autonews', 'followup') 
  914.  
  915. $typemails = array( 1, 2, 3 ); 
  916. if ( empty( $email->type ) || ! in_array( $email->type, $typemails ) || strpos( $email->body, '[nostatpicture]' ) ) { 
  917. $email->body = str_replace( array( '[statpicture]', '[nostatpicture]' ), '', $email->body ); 
  918. return; 
  919. $widths = range( 1, 50 ); 
  920. shuffle( $widths ); 
  921. $heights = range( 1, 3 ); 
  922. shuffle( $heights ); 
  923. /**$altTxt=array(__('Footer image', WYSIJA), __('Footer', WYSIJA), __('My footer image', WYSIJA)); 
  924. shuffle($altTxt);*/ 
  925.  
  926. $widthsize = $widths[0]; 
  927. $heightsize = $heights[0]; 
  928. $width = empty($widthsize) ? '' : ' width="'.$widthsize.'" '; 
  929. $height = empty($heightsize) ? '' : ' height="'.$heightsize.'" '; 
  930.  
  931. $modelConf = WYSIJA::get( 'config', 'model' ); 
  932.  
  933. $args = array(); 
  934. $args['email_id'] = $email->email_id; 
  935. $args['user_id'] = $user->user_id; 
  936.  
  937. $args['controller'] = 'stats'; 
  938.  
  939. $forbiddenparams = $modelConf->getValue( 'params_forbidden' ); 
  940. if ( isset( $forbiddenparams['controller']['stats'] ) ) { 
  941. $args['controller'] = $forbiddenparams['controller']['stats']; 
  942.  
  943.  
  944. $args['action'] = 'analyse'; 
  945. $args['wysija-page'] = 1; 
  946. $args['render'] = 1; 
  947.  
  948. $mytracker = WYSIJA::get_permalink( $modelConf->getValue( 'confirm_email_link' ), $args ); 
  949. $statPicture = '<img alt="" src="'.$mytracker.'" border="0" '.$height.$width.'/>'; 
  950.  
  951. if ( strpos( $email->body, '[statpicture]' ) ) { 
  952. $email->body = str_replace( '[statpicture]', $statPicture, $email->body ); 
  953. } elseif ( strpos( $email->body, '</body>' ) ) { 
  954. $email->body = str_replace( '</body>', $statPicture . '</body>', $email->body ); 
  955. } else { 
  956. $email->body .= $statPicture; 
  957.  
  958. function SetError($key, $var="") { 
  959. if(count($this->language) < 1) { 
  960. $this->SetLanguage('en'); // set the default language 
  961. $this->error_count++; 
  962.  
  963. if(!$this->ErrorInfo) { 
  964. $this->ErrorInfo=array(); 
  965. //$this->ErrorInfo = $this->language[$key]; 
  966. $varerror=''; 
  967. if(!is_array($var)) { 
  968. $varerror=$var; 
  969. } elseif(isset($var['error'])) { 
  970. $varerror=$var['error']; 
  971.  
  972. if(!isset($this->language[$key])) { 
  973. $errormsg=$key; 
  974. }else{ 
  975. $errormsg=$this->language[$key]; 
  976.  
  977. if($varerror) { 
  978. $errormsg.='('.$varerror.')'; 
  979.  
  980. $this->ErrorInfo[] = array('error'=>$errormsg); 
  981.  
  982. //$this->ErrorInfoVar = $var; 
  983.  
  984. /** 
  985. * Sets message type to HTML. 
  986. * @param bool $bool 
  987. * @return void 
  988. */ 
  989. public function IsHTML($ishtml = true) { 
  990. parent::IsHTML($ishtml); 
  991. $this->sendHTML = $ishtml; 
  992.  
  993. /** 
  994. * Overrides parent method 
  995. */ 
  996. public function getSMTPInstance() { 
  997. if (!is_object($this->smtp)) { 
  998. require_once WYSIJA_DIR . 'inc' . DS . 'mailer' . DS . 'class.mysmtp.php'; 
  999. $this->smtp = new WYSIJA_MySMTP; 
  1000. return $this->smtp; 
  1001.  
  1002. /** 
  1003. * Overrides parent method 
  1004. */ 
  1005. public function postSend() { 
  1006. switch($this->Mailer) { 
  1007. case 'elasticemail' : 
  1008. $result = $this->elasticEmail->sendMail($this); 
  1009. if (!$result) $this->SetError($this->elasticEmail->error); 
  1010. break; 
  1011. case 'sendgrid' : 
  1012. $result = $this->sendGrid->sendMail($this); 
  1013. if (!$result) $this->SetError($this->sendGrid->error); 
  1014. break; 
  1015. case 'sparkpost' : 
  1016. $result = $this->sparkpost->send_mail($this); 
  1017. if ( $result !== true ) $this->core->error($this->sparkpost->error); 
  1018. break; 
  1019. case 'mailpoet' : 
  1020. $result = $this->mailpoet->send_mail($this); 
  1021. if ( $result !== true ) $this->core->error($this->mailpoet->error); 
  1022. break; 
  1023. case 'wpmail' : 
  1024. $to = array_filter($this->to[0]); 
  1025. add_filter('phpmailer_init', array($this, 'wpmail_init'), 90); 
  1026. $result = wp_mail($to[0], $this->Subject, $this->Body, $header); 
  1027. break; 
  1028. default: 
  1029. $result = parent::postSend(); 
  1030. break; 
  1031.  
  1032. return $result;