If the port is not specified use the default SMTP_PORT.
If tval is specified then a connection will try and be
established with the server for that number of seconds.
If tval is not specified the default is 30 seconds to
try on the connection.
SMTP CODE SUCCESS: 220
SMTP CODE FAILURE: 421
/** * Sends mail via SMTP using PhpSMTP (Author: * Chris Ryan). Returns bool. Returns false if there is a * bad MAIL FROM, RCPT, or DATA input. * @private * @returns bool */ function smtp_send($header, $body) { // Include SMTP class code, but not twice include_once $this->PluginDir . "class.smtp.php"; $smtp = new SMTP(); $smtp->do_debug = $this->SMTPDebug; // Try to connect to all SMTP servers $hosts = explode(";", $this->Host); $index = 0; $connection = false; $smtp_from = ""; $bad_rcpt = array(); $e = ""; // Retry while there is no connection while ($index < count($hosts) && $connection == false) { if (strstr($hosts[$index], ":")) { list($host, $port) = explode(":", $hosts[$index]); } else { $host = $hosts[$index]; $port = $this->Port; } if ($smtp->Connect($host, $port, $this->Timeout)) { $connection = true; } //printf("%s host could not connect<br>", $hosts[$index]); //debug only $index++; } if (!$connection) { $this->error_handler("SMTP Error: could not connect to SMTP host server(s)"); return false; } // Must perform HELO before authentication $smtp->Hello($this->Helo); // If user requests SMTP authentication if ($this->SMTPAuth) { if (!$smtp->Authenticate($this->Username, $this->Password)) { $this->error_handler("SMTP Error: Could not authenticate"); return false; } } if ($this->Sender == "") { $smtp_from = $this->From; } else { $smtp_from = $this->Sender; } if (!$smtp->Mail(sprintf("<%s>", $smtp_from))) { $e = sprintf("SMTP Error: From address [%s] failed", $smtp_from); $this->error_handler($e); return false; } // Attempt to send attach all recipients for ($i = 0; $i < count($this->to); $i++) { if (!$smtp->Recipient(sprintf("<%s>", $this->to[$i][0]))) { $bad_rcpt[] = $this->to[$i][0]; } } for ($i = 0; $i < count($this->cc); $i++) { if (!$smtp->Recipient(sprintf("<%s>", $this->cc[$i][0]))) { $bad_rcpt[] = $this->cc[$i][0]; } } for ($i = 0; $i < count($this->bcc); $i++) { if (!$smtp->Recipient(sprintf("<%s>", $this->bcc[$i][0]))) { $bad_rcpt[] = $this->bcc[$i][0]; } } // Create error message if (count($bad_rcpt) > 0) { for ($i = 0; $i < count($bad_rcpt); $i++) { if ($i != 0) { $e .= ", "; } $e .= $bad_rcpt[$i]; } $e = sprintf("SMTP Error: The following recipients failed [%s]", $e); $this->error_handler($e); return false; } if (!$smtp->Data(sprintf("%s%s", $header, $body))) { $this->error_handler("SMTP Error: Data not accepted"); return false; } $smtp->Quit(); return true; }
echo "OK.\n"; } } if ($mass) { while (!$plproxy->val("SELECT COUNT(*) FROM messages(?) WHERE id = ?", $sender['uid'], $message_id)) { echo "Wait PGQ (10 seconds)...\n"; sleep(10); } $res = $plproxy->query("SELECT * FROM messages_zeros_userdata(?, ?)", $sender['uid'], $message_id); } else { $res = $master->query($sql); } echo "Send email messages\n"; $count = 0; $smtp = new SMTP(); if (!$smtp->Connect()) { die("Don't connect to SMTP\n"); } while ($user = pg_fetch_assoc($res)) { if (empty($user['email']) || !is_null($eSubscr) && substr($user['subscr'], $eSubscr, 1) == '0') { continue; } $smtp->recipient = $user['uname'] . " " . $user['usurname'] . " [" . $user['login'] . "] <" . $user['email'] . ">"; $smtp->subject = preg_replace("/\\{\\{([-_A-Za-z0-9]+)\\}\\}/e", "\$user['\\1']", $eSubject); $smtp->message = preg_replace("/\\{\\{([-_A-Za-z0-9]+)\\}\\}/e", "\$user['\\1']", $eMessage); if ($count > 0 && $count % $printStatus == 0) { echo "Working... {$count} emails sended\n"; } $smtp->SmtpMail('text/html'); $count++; }
function smtp_send($header, $body) { include "smtp.inc.php"; // Load code only if asked $smtp = new SMTP(); $smtp->do_debug = $this->SMTPDebug; // Try to connect to all SMTP servers $hosts = explode(";", $this->Host); $x = 0; $connection = false; while ($x < count($hosts)) { if ($smtp->Connect($hosts[$x], $this->Port, $this->Timeout)) { $connection = true; break; } // printf("%s host could not connect<br>", $hosts[$x]); //debug only $x++; } if (!$connection) { $this->error_handler("SMTP Error: could not connect to SMTP host server(s)"); } $smtp->Hello($this->Helo); $smtp->Mail(sprintf("<%s>", $this->From)); for ($x = 0; $x < count($this->to); $x++) { $smtp->Recipient(sprintf("<%s>", $this->to[$x][0])); } for ($x = 0; $x < count($this->cc); $x++) { $smtp->Recipient(sprintf("<%s>", $this->cc[$x][0])); } for ($x = 0; $x < count($this->bcc); $x++) { $smtp->Recipient(sprintf("<%s>", $this->bcc[$x][0])); } $smtp->Data(sprintf("%s%s", $header, $body)); $smtp->Quit(); }
function smtp_send($header, $body) { global $enable_debug; $smtp = new SMTP(); $smtp->do_debug = $enable_debug; $hosts = explode(";", $this->Host); $index = 0; $connection = false; while ($index < count($hosts) && $connection == false) { if ($smtp->Connect($hosts[$index], $this->Port, $this->Timeout)) { $connection = true; } $index++; } if (!$connection) { $this->error_handler("SMTP Error: could not connect to SMTP host server(s)"); return false; } if ($this->blUseAuthLogin) { if (!$smtp->AuthHello($this->Helo, $this->AuthUser, $this->AuthPass)) { $this->error_handler("SMTP Error: Invalid username/password"); return false; } } else { $smtp->Hello($this->Helo); } $smtp->MailFrom(sprintf("<%s>", $this->From)); for ($i = 0; $i < count($this->to); $i++) { if (!$smtp->Recipient(sprintf("<%s>", $this->to[$i][0]))) { $this->error_handler("SMTP Error: Recipient not accepted. Verify your relay rules"); return false; } } for ($i = 0; $i < count($this->cc); $i++) { if (!$smtp->Recipient(sprintf("<%s>", $this->cc[$i][0]))) { $this->error_handler("SMTP Error: Recipient not accepted. Verify your relay rules"); return false; } } for ($i = 0; $i < count($this->bcc); $i++) { if (!$smtp->Recipient(sprintf("<%s>", $this->bcc[$i][0]))) { $this->error_handler("SMTP Error: Recipient not accepted. Verify your relay rules"); return false; } } if (!$smtp->Data(sprintf("%s%s", $header, $body))) { $this->error_handler("SMTP Error: Data not accepted"); return false; } $smtp->Quit(); }
/** * for Test email configuration * @autor Alvaro <*****@*****.**> */ public function testConnection($params) { G::LoadClass('net'); G::LoadThirdParty('phpmailer', 'class.smtp'); if ($_POST['typeTest'] == 'MAIL') { define("SUCCESSFUL", 'SUCCESSFUL'); define("FAILED", 'FAILED'); $mail_to = $_POST['mail_to']; $send_test_mail = $_POST['send_test_mail']; $_POST['FROM_NAME'] = $mail_to; $_POST['FROM_EMAIL'] = $mail_to; $_POST['MESS_ENGINE'] = 'MAIL'; $_POST['MESS_SERVER'] = 'localhost'; $_POST['MESS_PORT'] = 25; $_POST['MESS_ACCOUNT'] = $mail_to; $_POST['MESS_PASSWORD'] = ''; $_POST['TO'] = $mail_to; $_POST['SMTPAuth'] = true; try { $resp = $this->sendTestMail(); } catch (Exception $error) { $resp = new stdclass(); $resp->status = false; $resp->msg = $error->getMessage(); } $response = array('success' => $resp->status); if ($resp->status == false) { $response['msg'] = G::LoadTranslation('ID_SENDMAIL_NOT_INSTALLED'); } echo G::json_encode($response); die; } $step = $_POST['step']; $server = $_POST['server']; $user = $_POST['user']; $passwd = $_POST['passwd']; $passwdHide = $_POST['passwdHide']; if (trim($passwdHide) != '') { $passwd = $passwdHide; $passwdHide = ''; } $passwdDec = G::decrypt($passwd, 'EMAILENCRYPT'); $auxPass = explode('hash:', $passwdDec); if (count($auxPass) > 1) { if (count($auxPass) == 2) { $passwd = $auxPass[1]; } else { array_shift($auxPass); $passwd = implode('', $auxPass); } } $_POST['passwd'] = $passwd; $port = $_POST['port']; $auth_required = $_POST['req_auth']; $UseSecureCon = $_POST['UseSecureCon']; $SendaTestMail = $_POST['SendaTestMail']; $Mailto = $_POST['eMailto']; $SMTPSecure = $_POST['UseSecureCon']; $Server = new NET($server); $smtp = new SMTP(); $timeout = 10; $hostinfo = array(); $srv = $_POST['server']; switch ($step) { case 1: $this->success = $Server->getErrno() == 0; $this->msg = $this->result ? 'success' : $Server->error; break; case 2: $Server->scannPort($port); $this->success = $Server->getErrno() == 0; //'Successfull'.$smtp->status; $this->msg = $this->result ? '' : $Server->error; break; case 3: //try to connect to host if (preg_match('/^(.+):([0-9]+)$/', $srv, $hostinfo)) { $server = $hostinfo[1]; $port = $hostinfo[2]; } else { $host = $srv; } $tls = strtoupper($SMTPSecure) == 'tls'; $ssl = strtoupper($SMTPSecure) == 'ssl'; $this->success = $smtp->Connect(($ssl ? 'ssl://' : '') . $server, $port, $timeout); $this->msg = $this->result ? '' : $Server->error; break; case 4: //try login to host if ($auth_required == 'true') { try { if (preg_match('/^(.+):([0-9]+)$/', $srv, $hostinfo)) { $server = $hostinfo[1]; $port = $hostinfo[2]; } else { $server = $srv; } if (strtoupper($UseSecureCon) == 'TLS') { $tls = 'tls'; } if (strtoupper($UseSecureCon) == 'SSL') { $tls = 'ssl'; } $tls = strtoupper($UseSecureCon) == 'tls'; $ssl = strtoupper($UseSecureCon) == 'ssl'; $server = $_POST['server']; if (strtoupper($UseSecureCon) == 'SSL') { $resp = $smtp->Connect('ssl://' . $server, $port, $timeout); } else { $resp = $smtp->Connect($server, $port, $timeout); } if ($resp) { $hello = $_SERVER['SERVER_NAME']; $smtp->Hello($hello); if (strtoupper($UseSecureCon) == 'TLS') { $smtp->Hello($hello); } if ($smtp->Authenticate($user, $passwd)) { $this->success = true; } else { $this->success = false; $this->msg = $smtp->error['error']; } } else { $this->success = false; $this->msg = $smtp->error['error']; } } catch (Exception $e) { $this->success = false; $this->msg = $e->getMessage(); } } else { $this->success = true; $this->msg = 'No authentication required!'; } break; case 5: if ($SendaTestMail == 'true') { try { $_POST['FROM_NAME'] = 'Process Maker O.S. [Test mail]'; $_POST['FROM_EMAIL'] = $user; $_POST['MESS_ENGINE'] = 'PHPMAILER'; $_POST['MESS_SERVER'] = $server; $_POST['MESS_PORT'] = $port; $_POST['MESS_ACCOUNT'] = $user; $_POST['MESS_PASSWORD'] = $passwd; $_POST['TO'] = $Mailto; if ($auth_required == 'true') { $_POST['SMTPAuth'] = true; } else { $_POST['SMTPAuth'] = false; } if (strtolower($_POST["UseSecureCon"]) != "no") { $_POST["SMTPSecure"] = $_POST["UseSecureCon"]; } if ($_POST['UseSecureCon'] == 'ssl') { $_POST['MESS_SERVER'] = 'ssl://' . $_POST['MESS_SERVER']; } $resp = $this->sendTestMail(); if ($resp->status == '1') { $this->success = true; } else { $this->success = false; $this->msg = $smtp->error['error']; } } catch (Exception $e) { $this->success = false; $this->msg = $e->getMessage(); } } else { $this->success = true; $this->msg = 'jump this step'; } break; } }
function checkSMTP($smtp_server, $smtp_port = 25, $username, $password, $auth_enabled = false, $tls_enabled = true) { require_once "libs/phpmailer/class.smtp.php"; $smtp = new SMTP(); $smtp->Connect($smtp_server, $smtp_port); if (!$smtp->Connected()) { return array("ERROR" => "Failed to connect to server", "SMTP_ERROR" => $smtp->getError()); } if (!$smtp->Hello()) { return array("ERROR" => "Failed to send hello command", "SMTP_ERROR" => $smtp->getError()); } if ($tls_enabled) { if (!$smtp->StartTLS()) { return array("ERROR" => "Failed to start TLS", "SMTP_ERROR" => $smtp->getError()); } } if ($auth_enabled) { if (!$smtp->Authenticate($username, $password)) { $error = $smtp->getError(); if (preg_match("/STARTTLS/", $error['smtp_msg'])) { return array("ERROR" => "Authenticate Error, TLS must be activated", "SMTP_ERROR" => $smtp->getError()); } else { return array("ERROR" => "Authenticate not accepted from server", "SMTP_ERROR" => $smtp->getError()); } } } return true; }
print FAILED . ',' . $Server->error; } break; #try to connect to host #try to connect to host case 3: $hostinfo = array(); if (preg_match('/^(.+):([0-9]+)$/', $srv, $hostinfo)) { $host = $hostinfo[1]; $port = $hostinfo[2]; } else { $host = $srv; } $tls = $SMTPSecure == 'tls'; $ssl = $SMTPSecure == 'ssl'; $resp = $smtp->Connect(($ssl ? 'ssl://' : '') . $host, $port, $timeout); if ($resp) { print SUCCESSFUL . ',' . $smtp->status; } else { print FAILED . ',' . $smtp->error['error']; } break; #try login to host #try login to host case 4: if ($auth_required == 'yes') { try { $hostinfo = array(); if (preg_match('/^(.+):([0-9]+)$/', $srv, $hostinfo)) { $host = $hostinfo[1]; $port = $hostinfo[2];
<?php include "class.smtp.php"; $fp = fopen("domains", "r"); $domains = unserialize(fread($fp, filesize("domains"))); fclose($fp); $smtp = new SMTP(); $smtp->do_debug = 2; foreach ($domains as $key => $value) { $email = $domains[$key][0]; $mx = $domains[$key][1]; //echo "email:$email mx:$mx\n"; echo "--->> {$mx} <<---\n"; $smtp->Connect($mx, 25, 30); $smtp->Hello("planetxmail"); $smtp->Mail("*****@*****.**"); $smtp->Recipient($email); $smtp->Data("This is only a test."); $smtp->Close(); echo "--------------------\n"; }
public function Connect($host, $port = 0, $timeout = 30, $options = array()) { $result = parent::Connect($host, $port, $timeout, $options); $this->handleError(); return $result; }
error_reporting(E_ALL); // php errors define('DISPLAY_XPM4_ERRORS', true); // display XPM4 errors // path to 'POP3.php' and 'SMTP.php' files from XPM4 package require_once '../POP3.php'; require_once '../SMTP.php'; $f = '*****@*****.**'; // from mail address / account username $t = '*****@*****.**'; // to mail address $p = 'password'; // account password // standard mail message RFC2822 $m = 'From: ' . $f . "\r\n" . 'To: ' . $t . "\r\n" . 'Subject: test' . "\r\n" . 'Content-Type: text/plain' . "\r\n\r\n" . 'Text message.'; // connect to 'pop3.hostname.net' POP3 server address with authentication username '$f' and password '$p' $p = POP3::Connect('pop3.hostname.net', $f, $p) or die(print_r($_RESULT)); // connect to 'smtp.hostname.net' SMTP server address $c = SMTP::Connect('smtp.hostname.net') or die(print_r($_RESULT)); // send mail $s = SMTP::Send($c, array($t), $m, $f); // print result if ($s) { echo 'Sent !'; } else { print_r($_RESULT); } // disconnect from SMTP server SMTP::Disconnect($c); // disconnect from POP3 server POP3::Disconnect($p);
/** * Sends mail via SMTP using PhpSMTP (Author: * Chris Ryan). Returns bool. Returns false if there is a * bad MAIL FROM, RCPT, or DATA input. * @private * @returns bool */ function smtp_send($header, $body) { // Include SMTP class code, but not twice include_once "class.smtp.php"; // Load code only if asked $smtp = new SMTP(); $smtp->do_debug = $this->SMTPDebug; // Try to connect to all SMTP servers $hosts = explode(";", $this->Host); //$this -> Curent_SMTP_Server = $this -> Curent_SMTP_Server + $this -> nb_try; if ($this->Debug_Roll) { echo "<br>amount of HostName :" . count($hosts) . "<br><br>"; } if ($this->Debug_Roll) { echo "<br>Curent_SMTP_Server : " . $this->Curent_SMTP_Server; } $this->Curent_SMTP_Server = ($this->Curent_SMTP_Server + 1) % count($hosts); if ($this->Debug_Roll) { echo "<br><br>Curent_SMTP_Server : " . $this->Curent_SMTP_Server . "<br>"; } if ($this->Debug_Roll) { print_r($hosts); } if (count($hosts) > 1) { for ($i = 0; $i < $this->Curent_SMTP_Server; $i++) { $the_shift = array_shift($hosts); array_push($hosts, $the_shift); } } if ($this->Debug_Roll) { echo "<br>"; } if ($this->Debug_Roll) { print_r($hosts); } $index = 0; $connection = false; $smtp_from = ""; $bad_rcpt = array(); $e = ""; // Retry while there is no connection while ($this->nb_try < count($hosts) && $connection == false) { $this->nb_try++; if (strstr($hosts[$index], ":")) { list($host, $port) = explode(":", $hosts[$index]); } else { $host = $hosts[$index]; $port = $this->Port; } if ($this->Debug_Roll) { echo "<br> --> Host_USED: {$host} Port_USED: {$port} --->"; } if ($smtp->Connect($host, $port, $this->Timeout)) { $connection = true; if ($this->Debug_Roll) { echo "<br>OK<br>"; } } else { //printf("<br><b> %s </b> :: host could not connect<br>", $hosts[$index]); //debug only if ($this->Debug_Roll) { echo "<br>NO<br>"; } $this->Curent_SMTP_Server = $this->Curent_SMTP_Server + 1; } $index++; } if (!$connection) { //echo "SMTP Error: could not connect to SMTP host server(s) ::: $host"; $this->error_handler("SMTP Error: could not connect to SMTP host server(s)"); return false; } // Must perform HELO before authentication $smtp->Hello($this->Helo); // If user requests SMTP authentication if ($this->SMTPAuth) { if (!$smtp->Authenticate($this->Username, $this->Password)) { if ($this->Debug_Roll) { echo "<br> SMTP Error: Could not authenticate ({$this->Username}, {$this->Password})"; } $this->error_handler("SMTP Error: Could not authenticate ({$this->Username}, {$this->Password})"); //echo "<br>SMTP Error: Could not authenticate"; return false; } } if ($this->Sender == "") { $smtp_from = $this->From; } else { $smtp_from = $this->Sender; } if (!$smtp->Mail(sprintf("<%s>", $smtp_from))) { $e = sprintf("SMTP Error: From address [%s] failed", $smtp_from); if ($this->Debug_Roll) { echo "<br> {$e}"; } $this->error_handler($e); return false; } // Attempt to send attach all recipients for ($i = 0; $i < count($this->to); $i++) { if (!$smtp->Recipient(sprintf("<%s>", $this->to[$i][0]))) { if ($smtp->code_error != 550) { //Relaying denied $bad_rcpt[] = $this->to[$i][0]; } else { if ($this->Debug_Roll) { echo "<br><br> Relaying denied !!!"; } if ($this->Debug_Roll) { echo "<br> --> Host_USED: {$host} Port_USED: {$port} --->"; } if ($this->Debug_Roll) { echo "<br>Curent_SMTP_Server : " . $this->Curent_SMTP_Server; } if ($this->Debug_Roll) { echo "<br> NB TRY : " . $this->nb_try . "<br>"; } if ($this->Debug_Roll) { print_r($hosts); } $this->Recup_code_error = $smtp->code_error; return false; } } } for ($i = 0; $i < count($this->cc); $i++) { if (!$smtp->Recipient(sprintf("<%s>", $this->cc[$i][0]))) { $bad_rcpt[] = $this->cc[$i][0]; } } for ($i = 0; $i < count($this->bcc); $i++) { if (!$smtp->Recipient(sprintf("<%s>", $this->bcc[$i][0]))) { $bad_rcpt[] = $this->bcc[$i][0]; } } // Create error message if (count($bad_rcpt) > 0) { for ($i = 0; $i < count($bad_rcpt); $i++) { if ($i != 0) { $e .= ", "; } $e .= $bad_rcpt[$i]; } $e = sprintf("SMTP Error: The following recipients failed [%s]", $e); $this->error_handler($e); if ($this->Debug_Roll) { echo "<br>{$e}"; } return false; } if (!$smtp->Data(sprintf("%s%s", $header, $body))) { $this->error_handler("SMTP Error: Data not accepted"); if ($this->Debug_Roll) { echo "<br>SMTP Error: Data not accepted"; } return false; } $smtp->Quit(); if ($this->Debug_Roll) { echo "<br> SUCCESS !!!"; } return true; }
public function send($para = array(), $single = false, $priority = 3, $extHeader = "") { if ($this->from == '') { $this->from = ini_get('sendmail_from'); } $this->addHeader("Return-Path", $this->from); $mail_list = array_merge($this->to, $this->cc, $this->bcc); if ($single == false) { if (count($this->to) > 0) { $this->addHeader("To", implode(', ', $this->formatEmail($this->to))); } if (count($this->cc) > 0) { $this->addHeader("Cc", implode(', ', $this->formatEmail($this->cc))); } if (count($this->bcc) > 0) { $this->addHeader("Bcc", implode(', ', $this->formatEmail($this->bcc))); } } $this->addHeader("From", $this->from); if (count($this->reply) > 0) { $this->addHeader("Reply-To", implode(', ', $this->formatEmail($this->reply))); } $this->addHeader("Subject", $this->subject); $this->addHeader("Message-ID", sprintf("<%s@%s>", md5(uniqid(time())), $_SERVER["HTTP_HOST"])); if (!preg_match("/[1-5]/", $priority)) { $priority = 3; } $this->addHeader("X-Priority", $priority); $this->addHeader("X-Mailer", "MyStep_CMS"); $this->addHeader("MIME-Version", "1.0"); $mail_content = implode("\r\n", $this->headers) . "\r\n"; if (!empty($extHeader)) { $mail_content .= $extHeader . "\r\n"; } $mail_content .= $this->buildMail(); $info = ""; if (!empty($para['mode'])) { require "class.smtp.php"; $smtp = new SMTP(); if (!$smtp->Connect(($para['mode'] == "ssl" || $para['mode'] == "ssl/tls" ? "ssl://" : "") . $para['host'], $para['port'], 10)) { $this->Error("Cannot connect to the mail server!"); return false; } if (!$smtp->Hello($_SERVER["HTTP_HOST"])) { $this->Error("Cannot send messege to the mail server!"); return false; } if ($para['mode'] == "tls" || $para['mode'] == "ssl/tls") { if (!$smtp->StartTLS()) { $this->Error("TLS error!"); return false; } $smtp->Hello($_SERVER["HTTP_HOST"]); } if (isset($para['user'])) { if (!$smtp->Authenticate($para['user'], $para['password'])) { $this->Error("Authenticate Failed!"); return false; } } if (!$smtp->Mail(ini_get('sendmail_from'))) { $this->Error("Bad sender email"); return false; } for ($i = 0, $m = count($mail_list); $i < $m; $i++) { if ($smtp->Recipient($mail_list[$i][0])) { $info = " sended!"; } else { $info = " error!"; } if ($this->log_fp) { fwrite($this->log_fp, $mail_list[$i][0] . $info . "\n"); } } if (!$smtp->Data($mail_content)) { $this->Error("Mail send Failed!"); return false; } $smtp->Reset(); if ($smtp->Connected()) { $smtp->Quit(); $smtp->Close(); } } else { for ($i = 0, $m = count($mail_list); $i < $m; $i++) { if (!@mail(formatEmail($mail_list[$i]), $this->subject, "", $mail_content)) { $info = " sended!"; } else { $info = " error!"; } if ($this->log_fp) { fwrite($this->log_fp, $mail_list[$i][0] . $info . "\n"); } } } if ($this->log_fp) { fclose($this->log_fp); } return true; }
/** * Test connection by step * * @param array $arrayData Data * @param int $step Step * * return array Return array with result of test connection by step */ public function testConnectionByStep(array $arrayData, $step = 0) { try { \G::LoadClass("net"); \G::LoadThirdParty("phpmailer", "class.smtp"); //MAIL if ($arrayData["MESS_ENGINE"] == "MAIL") { $arrayDataMail = array(); $eregMail = "/^[0-9a-zA-Z]+(?:[._][0-9a-zA-Z]+)*@[0-9a-zA-Z]+(?:[._-][0-9a-zA-Z]+)*\\.[0-9a-zA-Z]{2,3}\$/"; $arrayDataMail["FROM_EMAIL"] = $arrayData["MESS_FROM_MAIL"] != "" && preg_match($eregMail, $arrayData["MESS_FROM_MAIL"]) ? $arrayData["MESS_FROM_MAIL"] : ""; $arrayDataMail["FROM_NAME"] = $arrayData["MESS_FROM_NAME"] != "" ? $arrayData["MESS_FROM_NAME"] : \G::LoadTranslation("ID_MESS_TEST_BODY"); $arrayDataMail["MESS_ENGINE"] = "MAIL"; $arrayDataMail["MESS_SERVER"] = "localhost"; $arrayDataMail["MESS_PORT"] = 25; $arrayDataMail["MESS_ACCOUNT"] = $arrayData["MAIL_TO"]; $arrayDataMail["MESS_PASSWORD"] = ""; $arrayDataMail["TO"] = $arrayData["MAIL_TO"]; $arrayDataMail["MESS_RAUTH"] = true; $arrayTestMailResult = array(); try { $arrayTestMailResult = $this->sendTestMail($arrayDataMail); } catch (Exception $e) { $arrayTestMailResult["status"] = false; $arrayTestMailResult["message"] = $e->getMessage(); } $arrayResult = array("result" => $arrayTestMailResult["status"], "message" => ""); if ($arrayTestMailResult["status"] == false) { $arrayResult["message"] = \G::LoadTranslation("ID_SENDMAIL_NOT_INSTALLED"); } //Return return $arrayResult; } //PHPMAILER $server = $arrayData["MESS_SERVER"]; $user = $arrayData["MESS_ACCOUNT"]; $passwd = $arrayData["MESS_PASSWORD"]; $fromMail = $arrayData["MESS_FROM_MAIL"]; $passwdHide = $arrayData["MESS_PASSWORD"]; if (trim($passwdHide) != "") { $passwd = $passwdHide; $passwdHide = ""; } $passwdDec = \G::decrypt($passwd, "EMAILENCRYPT"); $auxPass = explode("hash:", $passwdDec); if (count($auxPass) > 1) { if (count($auxPass) == 2) { $passwd = $auxPass[1]; } else { array_shift($auxPass); $passwd = implode("", $auxPass); } } $arrayData["MESS_PASSWORD"] = $passwd; $port = (int) $arrayData["MESS_PORT"]; $auth_required = (int) $arrayData["MESS_RAUTH"]; $useSecureCon = $arrayData["SMTPSECURE"]; $sendTestMail = (int) $arrayData["MESS_TRY_SEND_INMEDIATLY"]; $mailTo = $arrayData["MAIL_TO"]; $smtpSecure = $arrayData["SMTPSECURE"]; $serverNet = new \NET($server); $smtp = new \SMTP(); $timeout = 10; $hostinfo = array(); $srv = $arrayData["MESS_SERVER"]; $arrayResult = array(); switch ($step) { case 1: $arrayResult["result"] = $serverNet->getErrno() == 0; $arrayResult["message"] = $serverNet->error; break; case 2: $serverNet->scannPort($port); $arrayResult["result"] = $serverNet->getErrno() == 0; $arrayResult["message"] = $serverNet->error; break; case 3: //Try to connect to host if (preg_match("/^(.+):([0-9]+)\$/", $srv, $hostinfo)) { $server = $hostinfo[1]; $port = $hostinfo[2]; } else { $host = $srv; } $tls = strtoupper($smtpSecure) == "tls"; $ssl = strtoupper($smtpSecure) == "ssl"; $arrayResult["result"] = $smtp->Connect(($ssl ? "ssl://" : "") . $server, $port, $timeout); $arrayResult["message"] = $serverNet->error; break; case 4: //Try login to host if ($auth_required == 1) { try { if (preg_match("/^(.+):([0-9]+)\$/", $srv, $hostinfo)) { $server = $hostinfo[1]; $port = $hostinfo[2]; } else { $server = $srv; } if (strtoupper($useSecureCon) == "TLS") { $tls = "tls"; } if (strtoupper($useSecureCon) == "SSL") { $tls = "ssl"; } $tls = strtoupper($useSecureCon) == "tls"; $ssl = strtoupper($useSecureCon) == "ssl"; $server = $arrayData["MESS_SERVER"]; if (strtoupper($useSecureCon) == "SSL") { $resp = $smtp->Connect("ssl://" . $server, $port, $timeout); } else { $resp = $smtp->Connect($server, $port, $timeout); } if ($resp) { $hello = $_SERVER["SERVER_NAME"]; $smtp->Hello($hello); if (strtoupper($useSecureCon) == "TLS") { $smtp->Hello($hello); } if ($smtp->Authenticate($user, $passwd)) { $arrayResult["result"] = true; } else { if (strtoupper($useSecureCon) == "TLS") { $arrayResult["result"] = true; } else { $arrayResult["result"] = false; $smtpError = $smtp->getError(); $arrayResult["message"] = $smtpError["error"]; } } } else { $arrayResult["result"] = false; $smtpError = $smtp->getError(); $arrayResult["message"] = $smtpError["error"]; } } catch (Exception $e) { $arrayResult["result"] = false; $arrayResult["message"] = $e->getMessage(); } } else { $arrayResult["result"] = true; $arrayResult["message"] = "No authentication required!"; } break; case 5: if ($sendTestMail == 1) { try { $arrayDataPhpMailer = array(); $eregMail = "/^[0-9a-zA-Z]+(?:[._][0-9a-zA-Z]+)*@[0-9a-zA-Z]+(?:[._-][0-9a-zA-Z]+)*\\.[0-9a-zA-Z]{2,3}\$/"; $arrayDataPhpMailer["FROM_EMAIL"] = $fromMail != "" && preg_match($eregMail, $fromMail) ? $fromMail : ""; $arrayDataPhpMailer["FROM_NAME"] = $arrayData["MESS_FROM_NAME"] != "" ? $arrayData["MESS_FROM_NAME"] : \G::LoadTranslation("ID_MESS_TEST_BODY"); $arrayDataPhpMailer["MESS_ENGINE"] = "PHPMAILER"; $arrayDataPhpMailer["MESS_SERVER"] = $server; $arrayDataPhpMailer["MESS_PORT"] = $port; $arrayDataPhpMailer["MESS_ACCOUNT"] = $user; $arrayDataPhpMailer["MESS_PASSWORD"] = $passwd; $arrayDataPhpMailer["TO"] = $mailTo; if ($auth_required == 1) { $arrayDataPhpMailer["MESS_RAUTH"] = true; } else { $arrayDataPhpMailer["MESS_RAUTH"] = false; } if (strtolower($arrayData["SMTPSECURE"]) != "no") { $arrayDataPhpMailer["SMTPSecure"] = $arrayData["SMTPSECURE"]; } $arrayTestMailResult = $this->sendTestMail($arrayDataPhpMailer); if ($arrayTestMailResult["status"] . "" == "1") { $arrayResult["result"] = true; } else { $arrayResult["result"] = false; $smtpError = $smtp->getError(); $arrayResult["message"] = $smtpError["error"]; } } catch (Exception $e) { $arrayResult["result"] = false; $arrayResult["message"] = $e->getMessage(); } } else { $arrayResult["result"] = true; $arrayResult["message"] = "Jump this step"; } break; } if (!isset($arrayResult["message"])) { $arrayResult["message"] = ""; } //Return return $arrayResult; } catch (\Exception $e) { $arrayResult = array(); $arrayResult["result"] = false; $arrayResult["message"] = $e->getMessage(); //Return return $arrayResult; } }
function _send_mail($msgdata) { $smtp = new SMTP(); $smtp->debug = $this->debug; if (!$smtp->Connect($this->host, $this->port, $this->timeout)) { $this->errormsg = "SMTP Error: could not connect to SMTP host server"; $this->errormsg .= "[" . $this->host . ":" . $this->port . "]"; return false; } if ($this->authlogin) { if (!$smtp->AuthHello($this->helo, $this->authuser, $this->authpass)) { $this->errormsg = "SMTP Error: Invalid username/password"; if ($smtp->errormsg) { $this->errormsg .= "<br/>" . $smtp->errormsg; } return false; } } else { $smtp->Hello($this->helo); } if (!$smtp->MailFrom(sprintf("<%s>", $this->from))) { $this->errormsg = "SMTP Error: Mail from [" . $this->from . "] not accepted."; if ($smtp->errormsg) { $this->errormsg .= "<br/>" . $smtp->errormsg; } return false; } $iToCount = count($this->to); for ($i = 0; $i < $iToCount; $i++) { if (!$smtp->Recipient(sprintf("<%s>", $this->to[$i][0]))) { $this->errormsg = "SMTP Error: Recipient [" . $this->to[$i][0] . "] not accepted."; if ($smtp->errormsg) { $this->errormsg .= "<br/>" . $smtp->errormsg; } return false; } } $iCcCount = count($this->cc); for ($i = 0; $i < $iCcCount; $i++) { if (!$smtp->Recipient(sprintf("<%s>", $this->cc[$i][0]))) { $this->errormsg = "SMTP Error: Recipient [" . $this->cc[$i][0] . "] not accepted."; if ($smtp->errormsg) { $this->errormsg .= "<br/>" . $smtp->errormsg; } return false; } } $iBccCount = count($this->bcc); for ($i = 0; $i < $iBccCount; $i++) { if (!$smtp->Recipient(sprintf("<%s>", $this->bcc[$i][0]))) { $this->errormsg = "SMTP Error: Recipient [" . $this->bcc[$i][0] . "] not accepted."; if ($smtp->errormsg) { $this->errormsg .= "<br/>" . $smtp->errormsg; } return false; } } if (!$smtp->Data($msgdata)) { $this->errormsg = "SMTP Error: Data not accepted"; if ($smtp->errormsg) { $this->errormsg .= "<br/>" . $smtp->errormsg; } return false; } $smtp->Quit(); }
function smtp_send($header, $body) { $smtp = new SMTP(); $smtp->do_debug = $this->SMTPDebug; $hosts = explode(";", $this->Host); $index = 0; $connection = false; $smtp_from = ""; $bad_rcpt = array(); $e = ""; while ($index < count($hosts) && $connection == false) { if (strstr($hosts[$index], ":")) { list($host, $port) = explode(":", $hosts[$index]); } else { $host = $hosts[$index]; $port = $this->Port; } if ($smtp->Connect($host, $port, $this->Timeout)) { $connection = true; } $index++; } if (!$connection) { $this->error_handler("SMTP Error: could not connect to SMTP host server(s)"); return false; } $smtp->Hello($this->Helo); if ($this->SMTPAuth) { if (!$smtp->Authenticate($this->Username, $this->Password)) { $this->error_handler("SMTP Error: Could not authenticate"); return false; } } if ($this->Sender == "") { $smtp_from = $this->From; } else { $smtp_from = $this->Sender; } if (!$smtp->Mail(sprintf("<%s>", $smtp_from))) { $e = sprintf("SMTP Error: From address [%s] failed", $smtp_from); $this->error_handler($e); return false; } for ($i = 0; $i < count($this->to); $i++) { if (!$smtp->Recipient(sprintf("<%s>", $this->to[$i][0]))) { $bad_rcpt[] = $this->to[$i][0]; } } for ($i = 0; $i < count($this->cc); $i++) { if (!$smtp->Recipient(sprintf("<%s>", $this->cc[$i][0]))) { $bad_rcpt[] = $this->cc[$i][0]; } } for ($i = 0; $i < count($this->bcc); $i++) { if (!$smtp->Recipient(sprintf("<%s>", $this->bcc[$i][0]))) { $bad_rcpt[] = $this->bcc[$i][0]; } } if (count($bad_rcpt) > 0) { for ($i = 0; $i < count($bad_rcpt); $i++) { if ($i != 0) { $e .= ", "; } $e .= $bad_rcpt[$i]; } $e = sprintf("SMTP Error: The following recipients failed [%s]", $e); $this->error_handler($e); return false; } if (!$smtp->Data(sprintf("%s%s", $header, $body))) { $this->error_handler("SMTP Error: Data not accepted"); return false; } $smtp->Quit(); return true; }