/** * Add an error message to the error container. * @access protected * @param string $msg * @return void */ protected function setError($msg) { $this->error_count++; if ($this->Mailer == 'smtp' and !is_null($this->smtp)) { $lasterror = $this->smtp->getError(); if (!empty($lasterror['error'])) { $msg .= $this->lang('smtp_error') . $lasterror['error']; if (!empty($lasterror['detail'])) { $msg .= ' Detail: ' . $lasterror['detail']; } if (!empty($lasterror['smtp_code'])) { $msg .= ' SMTP code: ' . $lasterror['smtp_code']; } if (!empty($lasterror['smtp_code_ex'])) { $msg .= ' Additional SMTP info: ' . $lasterror['smtp_code_ex']; } } } $this->ErrorInfo = $msg; }
/** * 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 { if (strtoupper($UseSecureCon) == 'TLS') { $this->success = true; } else { $this->success = false; $smtpError = $smtp->getError(); $this->msg = $smtpError['error']; // $this->msg = $smtp->error['error']; } } } else { $this->success = false; $smtpError = $smtp->getError(); $this->msg = $smtpError['error']; // $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'] = G::LoadTranslation('ID_MESS_TEST_BODY'); $_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; $smtpError = $smtp->getError(); $this->msg = $smtpError['error']; // $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; }
/** * Send mail via SMTP. * Returns false if there is a bad MAIL FROM, RCPT, or DATA input. * Uses the PHPMailerSMTP class by default. * @see PHPMailer::getSMTPInstance() to use a different class. * @param string $header The message headers * @param string $body The message body * @throws phpmailerException * @uses SMTP * @access protected * @return boolean */ protected function smtpSend($header, $body) { $bad_rcpt = array(); if (!$this->smtpConnect($this->SMTPOptions)) { throw new phpmailerException($this->lang('smtp_connect_failed'), self::STOP_CRITICAL); } if ('' == $this->Sender) { $smtp_from = $this->From; } else { $smtp_from = $this->Sender; } if (!$this->smtp->mail($smtp_from)) { $this->setError($this->lang('from_failed') . $smtp_from . ' : ' . implode(',', $this->smtp->getError())); throw new phpmailerException($this->ErrorInfo, self::STOP_CRITICAL); } // Attempt to send to all recipients foreach (array($this->to, $this->cc, $this->bcc) as $togroup) { foreach ($togroup as $to) { if (!$this->smtp->recipient($to[0])) { $error = $this->smtp->getError(); $bad_rcpt[] = array('to' => $to[0], 'error' => $error['detail']); $isSent = false; } else { $isSent = true; } $this->doCallback($isSent, array($to[0]), array(), array(), $this->Subject, $body, $this->From); } } // Only send the DATA command if we have viable recipients if (count($this->all_recipients) > count($bad_rcpt) and !$this->smtp->data($header . $body)) { throw new phpmailerException($this->lang('data_not_accepted'), self::STOP_CRITICAL); } if ($this->SMTPKeepAlive) { $this->smtp->reset(); } else { $this->smtp->quit(); $this->smtp->close(); } //Create error message for any bad addresses if (count($bad_rcpt) > 0) { $errstr = ''; foreach ($bad_rcpt as $bad) { $errstr .= $bad['to'] . ': ' . $bad['error']; } throw new phpmailerException($this->lang('recipients_failed') . $errstr, self::STOP_CONTINUE); } return true; }
$ssl = $SMTPSecure == 'ssl'; $resp = $smtp->Connect(($ssl ? 'ssl://' : '') . $host, $port, $timeout); if ($resp) { $hello = $_SERVER['SERVER_NAME']; $smtp->Hello($hello); if ($tls) { if (!$smtp->StartTLS()) { // problem with tls } //We must resend HELO after tls negotiation $smtp->Hello($hello); } if ($smtp->Authenticate($user, $passwd)) { print SUCCESSFUL . ',' . $smtp->status; } else { $smtpError = $smtp->getError(); print FAILED . ',' . $smtpError['error']; // print (FAILED . ',' . $smtp->error['error']) ; } } else { $smtpError = $smtp->getError(); print FAILED . ',' . $smtpError['error']; // print (FAILED . ',' . $smtp->error['error']) ; } } catch (Exception $e) { print FAILED . ',' . $e->getMessage(); } } else { print SUCCESSFUL . ', No authentication required!'; } break;
/** * 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; } }
/** * Add an error message to the error container. * * @param string $msg * * @return void */ protected function setError($msg) { $this->error_count++; if ($this->Mailer == 'smtp' and !is_null($this->smtp)) { $lasterror = $this->smtp->getError(); if (!empty($lasterror) and array_key_exists('smtp_msg', $lasterror)) { $msg .= '<p>' . $this->lang('smtp_error') . $lasterror['smtp_msg'] . "</p>\n"; } } $this->ErrorInfo = $msg; }
/** * Send email using authenticated STMP with TLS */ function sendResetMail($to, $newpass) { $smtp = new SMTP(); //$smtp->setDebugLevel(4); //$smtp->setDebugOutput("error_log"); $host = $this->config['smtp']['host']; $port = $this->config['smtp']['port']; // Connect if (!$smtp->connect($host, $port)) { return $smtp->getError(); } // EHLO if (!$smtp->hello("me")) { return $smtp->getError(); } // STARTTLS if (!$smtp->startTLS()) { return $smtp->getError(); } // EHLO if (!$smtp->hello("me")) { return $smtp->getError(); } // AUTH LOGIN $username = $this->config['smtp']['username']; $password = $this->config['smtp']['password']; if (!$smtp->authenticate($username, $password)) { return $smtp->getError(); } // MAIL FROM $from = $this->config['smtp']['from']; if (!$smtp->mail($from)) { return $smtp->getError(); } // RCPT TO if (!$smtp->recipient($to)) { return $smtp->getError(); } // DATA $msg = str_replace("@@email@@", $to, str_replace("@@newpass@@", $newpass, $this->config['smtp']['msgtemplate'])); if (!$smtp->data($msg)) { return $smtp->getError(); } // QUIT if (!$smtp->quit()) { return $smtp->getError(); } // Disconnect and close $smtp->close(); return null; }