Добрый день.
Есть скрипт отправки почты с сайта. Через стандартную функцию php mail письма уходят.
Как выбрать по умолчанию отправку через SMTP? В скрипте есть и php mail и smtp. На каком этапе идет определение, как отправлять?
Вставлю только часть кода - ограничение.
const MAIL_TRANSPORT_SMTP = 1;
const MAIL_TRANSPORT_DAEMON = 2;
const MAIL_TRANSPORT_FAKE_SUCCESS = 3;
const MAIL_TRANSPORT_FAKE_ERROR = 4;
private static $connect = array('host' => 'smtp.mail.ru', 'port' => 465, 'login' => 'ЛОГИН', 'pass' => 'ПАРОЛЬ', 'fromru' => 'Mail');
private $data = array(
'to_email' => '', 'to_name' => '', 'from_name' => '', 'from_email' => '', 'as_plain' => false,
'reply_email' => '', 'reply_name' => '', 'text' => '', 'subject' => '',
'transport' => self::MAIL_TRANSPORT_DAEMON
);
/**
*
* @return bool
*/
public function send() {
$result = false;
if (!$this->data['as_plain'] && is_file($_SERVER['DOCUMENT_ROOT'] . '/theme/default/mail.tpl')) {
ob_start();
if (!class_exists('ModCity')) {
include_once $_SERVER['DOCUMENT_ROOT'] . '/ext/city/modcity.class.php';
}
$toEmail = $this->data['to_email'];
$httpHost = 'http://' . $_SERVER['HTTP_HOST'];
$mailContent = $this->data['text'];
include $_SERVER['DOCUMENT_ROOT'] . '/theme/default/mail.tpl';
$this->data['text'] = ob_get_clean();
$siteParams = SFCore::siteParam();
foreach ($siteParams as $paramAlias => $paramValue) {
$this->data['text'] = str_replace('{' . $paramAlias . '}', $paramValue, $this->data['text']);
}
$this->data['text'] = str_replace('{year}', date('Y'), $this->data['text']);
}
$this->data['text_utf8'] = $this->data['text'];
if (md5(@iconv('utf-8', 'utf-8', $this->data['subject'])) == md5($this->data['subject'])) {
$this->data['subject'] = @iconv('utf-8', 'windows-1251', $this->data['subject']);
}
if (md5(@iconv('utf-8', 'utf-8', $this->data['text'])) == md5($this->data['text'])) {
$this->data['text'] = @iconv('utf-8', 'windows-1251', $this->data['text']);
}
$toName = $this->data['to_name'];
if ($toName && md5(@iconv('utf-8', 'utf-8', $toName)) == md5($toName)) {
$toName = @iconv('utf-8', 'windows-1251', $toName);
}
$toEnc = '=?windows-1251?B?' . base64_encode($toName) . '?=';
$this->data['to_full'] = "$toEnc <{$this->data['to_email']}>";
$fromName = $this->data['from_name'];
if ($fromName && md5(@iconv('utf-8', 'utf-8', $fromName)) == md5($fromName)) {
$fromName = @iconv('utf-8', 'windows-1251', $fromName);
}
$fromEnc = '=?windows-1251?B?' . base64_encode($fromName) . '?=';
$this->data['from_full'] = "$fromEnc <{$this->data['from_email']}>";
if ($this->data['reply_email']) {
$replyName = $this->data['reply_name'];
if ($replyName && md5(@iconv('utf-8', 'utf-8', $replyName)) == md5($replyName)) {
$replyName = @iconv('utf-8', 'windows-1251', $replyName);
}
$replyEnc = '=?windows-1251?B?' . base64_encode($replyName) . '?=';
$this->data['reply_full'] = "$replyEnc <{$this->data['reply_email']}>";
}
$this->data['subject_ready'] = '=?windows-1251?B?' . base64_encode($this->data['subject']) . '?=';
switch ($this->data['transport']) {
case self::MAIL_TRANSPORT_SMTP: $result = $this->sendSMTP();
break;
case self::MAIL_TRANSPORT_DAEMON: $result = $this->sendDaemon();
break;
case self::MAIL_TRANSPORT_FAKE_SUCCESS: $result = true;
break;
case self::MAIL_TRANSPORT_FAKE_ERROR: default: $result = false;
}
$this->toLog($result);
return $result;
}
private function toLog($result) {
$q = "DELETE FROM mail_log WHERE ADDDATE(time_create,INTERVAL 1 MONTH) < NOW()";
SFDB::query($q);
$set = array("mail_from = '" . SFDB::escape($this->data['from_full']) . "', email = '" . SFDB::escape($this->data['to_full']) . "'");
$set[] = "text = '" . SFDB::escape($this->data['text_utf8']) . "', subject = '" . SFDB::escape($this->data['subject_ready']) . "'";
$set[] = "success = " . (int) $result . ", transport = {$this->data['transport']}";
if (!empty($this->data['reply_full'])) {
$set[] = "reply_to = '" . SFDB::escape($this->data['reply_full']) . "'";
}
$q = "INSERT INTO mail_log SET " . implode(', ', $set);
$success = SFDB::query($q);
}
/**
* Отправка через стандартную функцию php mail
* @return type
*/
private function sendDaemon() {
$contentType = $this->data['as_plain'] ? 'plain' : 'html';
$header = "Content-type: text/$contentType; charset=\"windows-1251\"\n";
$header .= "MIME-Version: 1.0" . "\n";
$header .= "From: {$this->data['from_full']}\n";
if ($this->data['reply_email']) {
$header .= "Reply-To: {$this->data['reply_full']}\n";
}
$header .= "Subject: {$this->data['subject_ready']}";
//$header .= "Content-type: text/$contentType; charset=\"windows-1251\"\n";
return mail($this->data['to_full'], $this->data['subject_ready'], $this->data['text'], $header);
}
/**
* Отправка через SMTP
* @return bool
*/
private function sendSMTP() {
$canSend = true;
if (empty(self::$connect['login'])) {
$sender = explode(':', SFCore::siteParam('sender'));
self::$connect['login'] = $sender[0];
if (isset($sender[1])) {
self::$connect['pass'] = $sender[1];
} else {
$canSend = false;
}
}
if (!$canSend) {
return false;
}
include_once dirname(__FILE__) . '/smtp_sasl/login_sasl_client.php';
include_once dirname(__FILE__) . '/smtp_sasl/sasl.php';
include_once dirname(__FILE__) . '/smtp.class.php';
$smtp = new smtp();
$sender_line = __LINE__;
$recipient_line = __LINE__;
$smtp->host_name = self::$connect['host'];
$smtp->host_port = self::$connect['port'];
$smtp->ssl = 1;
$smtp->start_tls = 0;
$smtp->localhost = "localhost";
$smtp->timeout = 10;
$smtp->data_timeout = 0;
$smtp->debug = (int) @$_ENV['debug'];
$smtp->html_debug = (int) @$_ENV['debug'];
$smtp->pop3_auth_host = '';
$smtp->user = self::$connect['login'];
$smtp->realm = "";
$smtp->password = self::$connect['pass'];
$smtp->workstation = "";
$smtp->authentication_mechanism = '';
setlocale(LC_TIME, null);
$date = strftime("%a, %d %b %Y %H:%M:%S %z");
$contentType = $this->data['as_plain'] ? 'plain' : 'html';
$headers = array(
"From: {$this->data['from_full']}",
"To: {$this->data['to_full']}",
"Subject: {$this->data['subject_ready']}",
"Date: " . $date,
"Content-type: text/$contentType; charset=windows-1251"
);
if ($this->data['reply_email']) {
$headers[] = "Reply-To: {$this->data['reply_full']}";
}
$res = $smtp->SendMessage(self::$connect['login'], array($this->data['to_email']), $headers, $this->data['text']);
if(@$_ENV['debug']){
echo $smtp->error;
}
return $res;
}