Reply to Sending mass email compaingns in Ajaxel CMS
Sending mass email compaingns in Ajaxel CMS Posted by Alexander Shatalov Working, I like that.. Hard work for years, got many versions. Great script:PHP public static function sendMass($data, $subject, $body, $attachments=false, $fromaddress=MAIL_EMAIL, $fromname=MAIL_MAME, $S=false,$update=NULL,$plain=false) { if ($S) { $smarty =& Index()->Smarty; } if (!CMS::method('allow','email_mysign')) { $body .= Conf()->g('allow2'); } self::$mail_email = $fromaddress; self::$mail_name = $fromname; $from = ((defined('SMTP_USERNAME') && USE_SMTP && SMTP_USERNAME) ? SMTP_USERNAME : $fromaddress); $msg2 = $msg3 = $msg4 = ''; $eol = "\r\n"; $mime_boundary = md5(time()); $htmlalt_mime_boundary = $mime_boundary.'_htmlalt'; $old_from = ini_get('sendmail_from'); ini_set('sendmail_from',$from); $_emails = array(); $_up = 0; $_fromname = self::mime_header_encode($fromname); if ($plain) { $headers = 'From: "'.$_fromname.'" <'.$from.'>'.$eol; $headers .= 'Reply-To: "'.$_fromname.'" <'.$fromaddress.'>'.$eol; $headers .= 'Return-Path: "'.$_fromname.'" <'.$fromaddress.'>'.$eol; $headers .= 'Message-ID: <'.time().'-'.$fromaddress.'>'.$eol; $headers .= 'X-Mailer: '.CMS::NAME.' v'.CMS::VERSION.' on PHP v'.phpversion().$eol; if (Conf()->is('email_headers')) { foreach (Conf()->g('email_headers') as $h) { $headers .= $h.$eol; } } $headers .= 'MIME-Version: 1.0'.$eol; $headers .= 'Content-Type: multipart/related; boundary="'.$mime_boundary.'"'.$eol.$eol; $message = '--'.$mime_boundary.$eol; $message .= 'Content-Type: text/plain; charset='.self::$smtp_charset.$eol; $message .= 'Content-Transfer-Encoding: 8bit'.$eol.$eol; if (!$S) $message .= self::plain($body).$eol.$eol; $i = 0; foreach ($data as $rs) { if ($S) { $smarty->assign('name',$rs['name']); $smarty->assign('group',$rs['group']); $smarty->assign('email',$rs['email']); $subject = $smarty->fetch($S['subject']); $html = $smarty->fetch($S['message']); $message = $message.$html.$eol.$eol; } $mail = self::mail($rs['email'], $subject, $message, $headers, $_subject, $html); if (Conf()->g('MYSQL_wait_timeout') && Conf()->g('MYSQL_second_start') && Conf()->g('MYSQL_wait_timeout') <= (time() - Conf()->g('MYSQL_second_start'))) { DB::close(); sleep(1); Conf()->s('MYSQL_second_start', time()); DB::reconnect(); } if ($mail) { $_emails[] = $rs['email']; if ($update) { $ex = DB::run($s = str_replace(array('{$email}','{$type}'),array($rs['email'],$rs['group']),$update)); if (!$ex || !DB::affected()) die(Message::sql($s)); else $_up++; } $i++; } usleep(4000); } } else { $headers = 'From: '.$_fromname.'<'.$fromaddress.'>'.$eol; $headers .= 'Reply-To: '.$_fromname.'<'.$fromaddress.'>'.$eol; $headers .= 'Return-Path: '.$_fromname.'<'.$fromaddress.'>'.$eol; $headers .= 'Message-ID: <'.time().'-'.$fromaddress.'>'.$eol; $headers .= 'X-Mailer: '.CMS::NAME.' v'.CMS::VERSION.' on PHP v'.phpversion().$eol; if (Conf()->is('email_headers')) { foreach (Conf()->g('email_headers') as $h) { $headers .= $h.$eol; } } $headers .= 'MIME-Version: 1.0'.$eol; $headers .= 'Content-Type: multipart/mixed; boundary="'.$mime_boundary.'"'.$eol.$eol; $msg1 = '--'.$mime_boundary.$eol; $msg1 .= 'Content-Type: multipart/alternative; boundary="'.$htmlalt_mime_boundary.'"'.$eol.$eol; $msg1 .= '--'.$htmlalt_mime_boundary.$eol; $msg1 .= 'Content-Type: text/plain; charset='.self::$smtp_charset.$eol; $msg1 .= 'Content-Transfer-Encoding: 8bit'.$eol.$eol; $msg2 .= $eol.$eol.'--'.$htmlalt_mime_boundary.$eol; $msg2 .= 'Content-Type: text/html; charset='.self::$smtp_charset.$eol; $msg2 .= 'Content-Transfer-Encoding: 8bit'.$eol.$eol; $msg3 .= $eol.$eol.'--'.$htmlalt_mime_boundary.'--'.$eol.$eol; $att = ''; if (!$S) { self::add_mail_att(); $_body = array(); foreach ($body as $l => $b) { $_body[$l] = self::cleanUpMess($b); $_body[$l] = self::urls($_body[$l],$mime_boundary); $_body[$l] = self::classes($_body[$l],$mime_boundary); $_body[$l] = self::body($_body[$l]); } $body = $_body; unset($_body); } if ($attachments && is_array($attachments)) { foreach ($attachments as $a) { if (!($file = File::Exists($a['file']))) continue; $file_name = substr($file, (strrpos($file, '/')+1)); $att .= '--'.$mime_boundary.$eol; $att .= 'Content-Type: '.(isset($a['content_type'])?$a['content_type']:filetype($file)).'; name="'.$file_name.'"'.$eol; $att .= 'Content-Transfer-Encoding: base64'.$eol; $att .= 'Content-Description: '.$file_name.$eol; $att .= 'Content-Disposition: attachment; filename="'.$file_name.'"'.$eol.$eol; $handle = fopen($file, 'rb'); $att .= chunk_split(base64_encode(fread($handle, filesize($file)))); @fclose($handle); $att .= $eol.$eol; } } $att .= self::$add_mail_attchments; self::add_mail_att(); $msg4 .= '--'.$mime_boundary.'--'.$eol.$eol; $__subject = $subject; $_subject = array(); foreach ($subject as $l => $s) { $_subject[$l] = self::mime_header_encode($s); } $subject = $_subject; unset($_subject); if (!$S) { $message = array(); foreach ($body as $l => $b) { $message[$l] = $msg1.self::plain($b).$msg2.$b.$msg3.$att.$msg4; } } $i = 0; foreach ($data as $rs) { if (!$rs['lang']) $rs['lang'] = DEFAULT_LANGUAGE; if ($S) { $smarty->assign('name',$rs['name']); $smarty->assign('group',$rs['group']); $smarty->assign('lang',$rs['lang']); $smarty->assign('email',$rs['email']); $smarty->assign('row',$rs); $subject = self::parse($smarty->fetch($S['subject']),$rs); $html = self::body(self::parse($smarty->fetch($S['message']),$rs,true)); $message = $msg1.self::plain($html).$msg2.$html.$msg3.$att.$msg4; $mail = self::mail($rs['email'], $subject, $message, $headers, $subject, $html); } else { $mail = self::mail($rs['email'], self::parse($subject[$rs['lang']], $rs), self::parse($message[$rs['lang']], $rs, true), $headers, $subject[$rs['lang']], $message[$rs['lang']]); } /* if (Conf()->g('MYSQL_wait_timeout') && Conf()->g('MYSQL_second_start') && Conf()->g('MYSQL_wait_timeout') <= (time() - Conf()->g('MYSQL_second_start'))) { DB::close(); sleep(1); Conf()->s('MYSQL_second_start', time()); DB::reconnect(); } */ if ($mail) { $_emails[] = $rs['email']; if ($update) { $s = str_replace(array('{$email}','{$group}'),array($rs['email'],$rs['group']),$update); $ex = DB::run($s); if (!$ex || !DB::affected()) die(Message::sql($s)); else $_up++; } $i++; } } } Conf()->s('MASS_UP', $_up); ini_set('sendmail_from', $old_from); return $i; } |