/** Function used to send email * $module -- current module * $to_email -- to email address * $from_name -- currently loggedin user name * $from_email -- currently loggedin ec_users's email id. you can give as '' if you are not in HelpDesk module * $subject -- subject of the email you want to send * $contents -- body of the email you want to send * $cc -- add email ids with comma seperated. - optional * $bcc -- add email ids with comma seperated. - optional. * $attachment -- whether we want to attach the currently selected file or all ec_files.[values = current,all] - optional * $emailid -- id of the email object which will be used to get the ec_attachments */ function send_webmail($module, $to_email, $from_name, $from_email, $subject, $contents, $cc = '', $bcc = '', $attachment = '', $emailid = '') { global $adb, $log; $log->debug("Entering send_webmail() method ..."); $smtphandle = new SMTPMailer(); if (!isset($_SESSION["MAILLIST_PLAINTEXT"])) { $smtphandle->UseHTML(1); // set email format to HTML $headertag = "<HEAD><META http-equiv=\"Content-Type\" content=\"text/html; charset=GBK\"></HEAD>"; $contents = from_html($contents); $contents = eregi_replace('<BODY', $headertag . '<BODY', $contents); } $smtphandle->charset = 'GBK'; //convert UTF-8 to GBK $subject = iconv_ec("UTF-8", "GBK", $subject); $contents = iconv_ec("UTF-8", "GBK", $contents); $emailid = iconv_ec("UTF-8", "GBK", $emailid); //$from_email = ""; $smtphandle->subject = $subject; $smtphandle->body = $contents; $res = $adb->query("select * from ec_systems where server_type='email'"); $rownum = $adb->num_rows($res); if ($rownum == 0) { return "No Smtp Server!"; } $server = $adb->query_result($res, 0, 'server'); $username = $adb->query_result($res, 0, 'server_username'); $password = $adb->query_result($res, 0, 'server_password'); $smtp_auth = $adb->query_result($res, 0, 'smtp_auth'); $server_port = $adb->query_result($res, 0, 'server_port'); if ($from_email == '') { $from_email = $adb->query_result($res, 0, 'from_email'); } if ($from_name == '') { $from_name = $adb->query_result($res, 0, 'from_name'); } $from_name = iconv_ec("UTF-8", "GBK", $from_name); $from_email = iconv_ec("UTF-8", "GBK", $from_email); $smtphandle->SetHost($server, $server_port); $smtphandle->UseAuthLogin($username, $password); $smtphandle->SetFrom($from_email, $from_name); $smtphandle->AddReplyTo($from_email, $from_name); if ($to_email != '') { if (is_array($to_email)) { for ($j = 0, $num = count($to_email); $j < $num; $j++) { $smtphandle->AddTo($to_email[$j]); } } else { $_tmp = explode(",", $to_email); for ($j = 0, $num = count($_tmp); $j < $num; $j++) { $smtphandle->AddTo($_tmp[$j]); } } } if ($cc != '') { if (is_array($cc)) { for ($j = 0, $num = count($cc); $j < $num; $j++) { $smtphandle->AddCc($cc[$j]); } } else { $_tmp = explode(",", $cc); for ($j = 0, $num = count($_tmp); $j < $num; $j++) { $smtphandle->AddCc($_tmp[$j]); } } } if ($bcc != '') { if (is_array($bcc)) { for ($j = 0, $num = count($bcc); $j < $num; $j++) { $smtphandle->AddBcc($bcc[$j]); } } else { $_tmp = explode(",", $bcc); for ($j = 0, $num = count($_tmp); $j < $num; $j++) { $smtphandle->AddBcc($_tmp[$j]); } } } if ($attachment != "") { $query = "select * from ec_attachments where attachmentsid='" . $attachment . "'"; $result = $adb->query($query); $rownum = $adb->num_rows($result); if ($rownum > 0) { $attachmentsid = $adb->query_result($result, 0, 'attachmentsid'); $filename = $adb->query_result($result, 0, 'name'); $filename = iconv_ec("UTF-8", "GBK", $filename); $encode_filename = base64_encode_filename($adb->query_result($result, 0, 'name')); $filepath = $adb->query_result($result, 0, 'path'); $filetype = $adb->query_result($result, 0, 'type'); global $root_directory; $fullpath = $root_directory . $filepath . $attachmentsid . "_" . $encode_filename; $log->info("send_webmail :: fullpath:" . $fullpath); if (file_exists($fullpath)) { $attachment_status = $smtphandle->AddAttachment($fullpath, $filename, $filetype); if (!$attachment_status) { $log->info("send_webmail :: errormsg:" . $smtphandle->errormsg); } } } } $errMsg = ""; $sentmsg = $smtphandle->Send(); if ($sentmsg === false) { $errMsg = $smtphandle->errormsg . '<br>'; $log->info("send_webmail :: errormsg:" . $smtphandle->errormsg); } $log->debug("Exit send_webmail() method ..."); return $errMsg; }
function testmailer($email_title, $email_body) { if ($email_title == "" || $email_body == "") { file_put_contents("result.txt", "email body or title is null\n", FILE_APPEND); exit("email body or title is null"); } require "SMTPMailer.php"; // file_put_contents("result.txt","log report:\n"); $emails = array("*****@*****.**", "*****@*****.**", "*****@*****.**"); $email_num = count($emails); $curr_num = 0; $sucess_num = 0; $fail_num = 0; for ($i = 0; $i < $email_num; $i++) { $mailer = new SMTPMailer(); $mailer->Host = "202.38.64.8"; $mailer->UserName = ""; $mailer->Password = ""; $mailer->From = ""; $mailer->ContentType = "text/html"; $mailer->Subject = $email_title; $mailer->Body = $email_body; $mailer->To = $emails[$i]; $curr_num++; if ($mailer->Send()) { $sucess_num++; //file_put_contents("result.txt","($curr_num)$emails[$i] sucessful_sended! [$sucess_num sucess, $fail_num failed]\n",FILE_APPEND); } else { $fail_num++; if (strstr($mailer->Error, "Recipient") !== false) { $connection = mysql_connect("localhost", "mydonor", "MYDONOR@))(") or die("Unable toconnect!"); mysql_select_db("mydonor") or die("Unable to select database!"); mysql_query("SET NAMES UTF8"); $query = "update if_donor_email set priority = -1 where email_addr = '" . $emails[$i] . "'"; $result = mysql_query($query); $error = mysql_error(); //file_put_contents("result.txt", "($curr_num) $emails[$i] ".$mailer->Error." [$sucess_num sucess, $fail_num failed][set_invalid_email_priority=-1: $result $error]\n",FILE_APPEND); } else { //file_put_contents("result.txt", "($curr_num) $emails[$i] ".$mailer->Error." [$sucess_num sucess, $fail_num failed]\n",FILE_APPEND); } } echo $mailer->Info; sleep(2); } //sumary of this task // file_put_contents("result.txt","sumary:\n",FILE_APPEND); // file_put_contents("result.txt","the number of emails to be send is: $email_num\n",FILE_APPEND); // file_put_contents("result.txt","the number of emails sended sucessfully is: $sucess_num\n",FILE_APPEND); // file_put_contents("result.txt","the number of emails failed to be sended is: $fail_num\n",FILE_APPEND); }
function send_webmail($to_email, $from_name, $from_email, $subject, $contents, $sjid = '') { ini_set('date.timezone', 'Asia/Shanghai'); global $adb, $log; global $current_user; $log->debug("Entering send_webmail() method ..."); $smtphandle = new SMTPMailer(); if (!isset($_SESSION["MAILLIST_PLAINTEXT"])) { $smtphandle->UseHTML(1); // set email format to HTML $headertag = "<HEAD><META http-equiv=\"Content-Type\" content=\"text/html; charset=GBK\"></HEAD>"; $contents = from_html($contents); $contents = eregi_replace('<BODY', $headertag . '<BODY', $contents); } $smtphandle->charset = 'GBK'; //convert UTF-8 to GBK $subject = iconv_ec("UTF-8", "GBK", $subject); $contents = iconv_ec("UTF-8", "GBK", $contents); //$from_name = iconv_ec("UTF-8","GBK",$from_name); //$from_email = ""; $smtphandle->subject = $subject; $smtphandle->body = $contents; $res = $adb->query("select * from ec_systems where server_type='email' and smownerid='" . $current_user->id . "'"); $rownum = $adb->num_rows($res); if ($rownum == 0) { return "No Smtp Server!"; } $server = $adb->query_result($res, 0, 'server'); $username = $adb->query_result($res, 0, 'server_username'); $password = $adb->query_result($res, 0, 'server_password'); $smtp_auth = $adb->query_result($res, 0, 'smtp_auth'); $server_port = $adb->query_result($res, 0, 'server_port'); $from_email = $adb->query_result($res, 0, 'from_email'); $from_name = $adb->query_result($res, 0, 'from_name'); $from_name = iconv_ec("UTF-8", "GBK", $from_name); $from_email = iconv_ec("UTF-8", "GBK", $from_email); $smtphandle->SetHost($server, $server_port); $smtphandle->UseAuthLogin($username, $password); $smtphandle->SetFrom($from_email, $from_name); $smtphandle->AddReplyTo($from_email, $from_name); if ($to_email != '') { $smtphandle->AddTo($to_email); } if ($sjid != "") { $query = "select ec_attachments.* from ec_attachments " . " inner join ec_attachmentsjrel on ec_attachmentsjrel.attachmentsid = ec_attachments.attachmentsid " . "where ec_attachmentsjrel.sjid={$sjid} and ec_attachments.deleted=0 order by ec_attachments.attachmentsid asc"; $result = $adb->query($query); $rownum = $adb->num_rows($result); if ($rownum > 0) { while ($row = $adb->fetch_array($result)) { $attachmentsid = $row['attachmentsid']; $filename = $row['name']; $filename = iconv_ec("UTF-8", "GBK", $filename); $encode_filename = base64_encode_filename($row['name']); $filepath = $row['path']; $filetype = $row['type']; global $root_directory; $fullpath = $root_directory . $filepath . $attachmentsid . "_" . $encode_filename; $log->info("send_webmail :: fullpath:" . $fullpath); if (file_exists($fullpath)) { $attachment_status = $smtphandle->AddAttachment($fullpath, $filename, $filetype); if (!$attachment_status) { $log->info("send_webmail :: errormsg:" . $smtphandle->errormsg); } } } } } $errMsg = ""; $sentmsg = $smtphandle->Send(); if ($sentmsg === false) { $errMsg = $smtphandle->errormsg . '<br>'; $log->info("send_webmail :: errormsg:" . $smtphandle->errormsg); } $log->debug("Exit send_webmail() method ..."); return $errMsg; }
/** * Sends notice-emails to the Receivers * @param array(MessageAdminReceivers) $receivers * @return array(MessageAdminReceivers) the receivers were the email * couldnt be send to */ private function sendEmails($receivers) { require_once PATH_INCLUDE . '/email/SMTPMailer.php'; $usersNotSend = array(); //the Email could not be send to these users $userdata = $this->sendEmailsFetchUserdata($receivers); $mailer = new SMTPMailer($this->_interface); $mailer->smtpDataInDatabaseLoad(); $mailer->emailFromXmlLoad(PATH_INCLUDE . '/email/Babesk_Nachricht_Info.xml'); foreach ($userdata as $user) { $mailer->AddAddress($user->email); if ($user->email != '' && $mailer->Send()) { //everything fine } else { $usersNotSend[] = $user; } $mailer->ClearAddresses(); } return $usersNotSend; }