Проблем с SMTP

C++ JAVA
Post Reply
Pro_Lamer4e
Нов
Нов
Posts: 188
Joined: Mon Oct 26, 2009 5:10 am

Проблем с SMTP

Post by Pro_Lamer4e » Mon Sep 17, 2018 8:29 am

Добър ден, имам проблем с изпращането на имейл-и, по принцип скрипта си работи, но като се получи имейл отива в спам папката на abv , някой знае ли каде може да е проблема.

smtp.lib.php

Code: Select all

<?php

/**
* Allows users to send email without e-mail server on the localmachine
* @package SMTP
* @author Fredrik Haugbergsmyr <smtp.lib@lagnut.net>
*/

require_once &#40;$rootpath . 'include/smtp/net.const.php'&#41;;

/**
* @version 0.0.2.2
* @access public
* @todo split messages, attachments
*/
class smtp
&#123;


	/**
	* lagnut-smtp version, send in the headers
	*
	* @var string
	* @access private
	*/
	var $_version = '0.0.2.2';


	/**
	* Turn debugon / off
	*
	* @var bool
	* @access private
	*/
	var $_debug = false;


	/**
	* Serverconnection resource
	*
	* @var resource
	* @access private
	*/
	var $_connection = null;


	/**
	* E-mailheaders
	*
	* @var array headers
	* @access private
	*/
	var $_hdrs = array&#40;&#41;;


	/**
	* E-mailbody
	*
	* @var string
	* @access private
	*/
	var $_body = '';


	/**
	* Default Content type
	*
	* @var string
	* @access private
	*/
	var $_mime = 'text/plain';


	/**
	* Default Charset
	*
	* @var string
	* @access private
	*/
	var $_charset = 'UTF-8';


	/**
	* Class contruction, sets client headers
	*
	* @access public
	*/
	function smtp&#40;&#41;
	&#123;
		$this->_add_hdr&#40;'X-Mailer', sprintf&#40;'LAGNUT-SMTP/%s', $this->_version&#41;&#41;;
		$this->_add_hdr&#40;'User-Agent', sprintf&#40;'LAGNUT-SMTP/%s', $this->_version&#41;&#41;;
		$this->_add_hdr&#40;'MIME-Version', '1.0'&#41;;
	&#125;


	/**
	* Turn debugging on/off
	*
	* @access public
	* @param bool $debug command
	*/
	function debug&#40;$debug&#41;
	&#123;
		$this->_debug = &#40;bool&#41;$debug;
	&#125;


	/**
	* Clean input to prevent injection
	*
	* @param string $input User data
	*/
	function _clean&#40;&$input&#41;
	&#123;
		if &#40;!is_string&#40;$input&#41;&#41; &#123;
			return false;
		&#125;
		$input = urldecode&#40;$input&#41;;
		$input = str_replace&#40;"\n", '', str_replace&#40;"\r", '', $input&#41;&#41;;
	&#125;


	/**
	* Send command to server
	*
	* @access private
	* @param string $cmdcommand
	* @param optional $data data
	*/
	function _cmd&#40;$cmd, $data = false&#41;
	&#123;
		$this->_clean&#40;$cmd&#41;;
		$this->_clean&#40;$data&#41;;

		if &#40;$this->_is_closed&#40;&#41;&#41; &#123;
			return false;
		&#125;

		if &#40;!$data&#41; &#123;
			$command = sprintf&#40;"%s\r\n", $cmd&#41;;
		&#125;else &#123;
			$command = sprintf&#40;"%s&#58; %s\r\n", $cmd,$data&#41;;
		&#125;

		fwrite&#40;$this->_connection, $command&#41;;
		$resp = $this->_read&#40;&#41;;
		if &#40;$this->_debug&#41;&#123;
			printf&#40;$command&#41;;
			printf&#40;$resp&#41;;
		&#125;
		if &#40;$this->_is_closed&#40;$resp&#41;&#41; &#123;
			return false;
		&#125;
		return $resp;
	&#125;


	/**
	* Collects header
	*
	* @access private
	* @param string$key
	* @param string $data
	*/
	function _add_hdr&#40;$key, $data&#41;
	&#123;
		$this->_clean&#40;$key&#41;;
		$this->_clean&#40;$data&#41;;
		$this->_hdrs&#91;$key&#93; = sprintf&#40;"%s&#58; %s\r\n", $key, $data&#41;;
	&#125;


	/**
	* Read server output
	*
	* @access private
	* @return string
	*/
	function _read&#40;&#41;
	&#123;
		if &#40;$this->_is_closed&#40;&#41;&#41; &#123;
			return false;
		&#125;
		$o = '';
		do &#123;
			$str = @fgets&#40;$this->_connection, 515&#41;;
			if &#40;!$str&#41; &#123;
				break;
			&#125;
			$o .= $str;
			if &#40;substr&#40;$str, 3, 1&#41; == ' '&#41; &#123;
				break;
			&#125;
		&#125; while &#40;true&#41;;
		return $o;
	&#125;


	/**
	* Checks if server denies more commands
	*
	* @access private
	* @param $int
	* @return bool true if connection is closed
	*/
	function _is_closed&#40;$response = false&#41;
	&#123;
		if &#40;!$this->_connection&#41; &#123;
			return true;
		&#125;
		if &#40;isset&#40;$response&#123;0&#125;&#41; && &#40;$response&#123;0&#125; == 4|| $response&#123;0&#125;== 5&#41;&#41; &#123;
			$this->close&#40;&#41;;
			return true;
		&#125;
		return false;
	&#125;


	/**
	* Open connection to server
	*
	* @access public
	* @param string $server SMTP server
	* @param int $port Server port
	*/
	function open&#40;$server, $port = 25&#41;
	&#123;
		$this->_connection = fsockopen&#40;$server, $port, $e, $er, 8&#41;;

		if &#40;$this->_is_closed&#40;&#41;&#41; &#123;
			return false;
		&#125;

		$init= $this->_read&#40;&#41;;
		if &#40;$this->_debug&#41;&#123;
			printf&#40;$init&#41;;
		&#125;

		if &#40;$this->_is_closed&#40;$init&#41;&#41; &#123;
			return false;
		&#125;
		
		$lhost = &#40;isset&#40;$_SERVER&#91;'SERVER_ADDR'&#93;&#41; ? $_SERVER&#91;'SERVER_ADDR'&#93; &#58; '127.0.0.1'&#41;;

		if &#40;strpos&#40;$init,'ESMTP'&#41; === false&#41;&#123;
			$this->_cmd&#40;'HELO '. gethostbyaddr&#40;$lhost&#41;&#41;;
		&#125; else &#123;
			$this->_cmd&#40;'EHLO '. gethostbyaddr&#40;$lhost&#41;&#41;;
		&#125;
	&#125;


	/**
	* Start TLS communication
	*
	* @access public
	*/
	function start_tls&#40;&#41;
	&#123;
		if &#40;!function_exists&#40;'stream_socket_enable_crypto'&#41;&#41; &#123;
			trigger_error&#40;'TLS is not supported', E_USER_ERROR&#41;;
			return false;
		&#125;
		$this->_cmd&#40;'STARTTLS'&#41;;
		stream_socket_enable_crypto&#40;$this->_connection, true, STREAM_CRYPTO_METHOD_TLS_CLIENT&#41;;
	&#125;


	/**
	* Performs SMTP authentication
	*
	* @access public
	* @param string $username username
	* @param string $password password
	* @param int authentication mecanism
	*/
	function auth&#40;$username, $password, $type = LOGIN&#41;
	&#123;
		global $rootpath;		
		include_once &#40;$rootpath . 'include/smtp/sasl.lib.php'&#41;;
		$sasl =& new sasl&#40;$sasl, $username, $password&#41;;
		switch &#40;$type&#41; &#123;
			case PLAIN&#58;
				$this->_cmd&#40;'AUTH PLAIN'&#41;;
				$this->_cmd&#40;$sasl->plain&#40;$username, $password&#41;&#41;;
				break;
			case LOGIN&#58;
				$this->_cmd&#40;'AUTH LOGIN'&#41;;
				$this->_cmd&#40;$sasl->login&#40;$username&#41;&#41;;
				$this->_cmd&#40;$sasl->login&#40;$password&#41;&#41;;
				break;
			case CRAM_MD5&#58;
				$resp = explode&#40;' ', $this->_cmd&#40;'AUTH CRAM-MD5'&#41;&#41;;
				$this->_cmd&#40;$sasl->cram_md5&#40;$username, $password, trim&#40;$resp&#91;1&#93;&#41;&#41;&#41;;
				break;
		&#125;
	&#125;


	/**
	* Closes connection to the server
	*
	* @access public
	*/
	function close&#40;&#41;
	&#123;
		if &#40;$this->_is_closed&#40;&#41;&#41; &#123;
			return false;
		&#125;

		$this->_cmd&#40;'RSET'&#41;;
		$this->_cmd&#40;'QUIT'&#41;;
		fclose&#40;$this->_connection&#41;;
		$this->_connection = null;
	&#125;


	/**
	* E-mail sender
	*
	* @access public
	* @param string $from Sender
	*/
	function from&#40;$email, $name = ''&#41;
	&#123;
		$from = !empty&#40;$name&#41; ? sprintf&#40;'%s <%s>', $name, $email&#41; &#58; $email;
		$this->_cmd&#40;'MAIL FROM', sprintf&#40;'<%s>', $email&#41;&#41;;
		$this->_add_hdr&#40;'FROM', $from&#41;;
		$this->_add_hdr&#40;'Return-path', $email&#41;;
	&#125;


	/**
	* Send reply-to header
	*
	* @param string $to
	*/
	function reply_to&#40;$email, $name = ''&#41;
	&#123;
		$to = !empty&#40;$name&#41; ? sprintf&#40;'%s <%s>', $name, $email&#41; &#58; $email;
		$this->_add_hdr&#40;'REPLY-TO', $to&#41;;
	&#125;


	/**
	* E-mail reciever
	*
	* @access public
	* @param string $to Reciever
	*/
	function to&#40;$email, $name = ''&#41;
	&#123;
		$to = !empty&#40;$name&#41; ? sprintf&#40;'%s <%s>', $name, $email&#41; &#58; $email;
		$this->_cmd&#40;'RCPT TO', sprintf&#40;'<%s>', $email&#41;&#41;;
		$this->_add_hdr&#40;'TO', $to&#41;;
	&#125;


	/**
	* MIME type
	*
	* @access public
	* @param string $mime MIME type
	*/
	function mime_charset&#40;$mime,$charset&#41;
	&#123;
		$this->_charset = $charset;
		$this->_mime = $mime;
		$this->_add_hdr&#40;'Content-type', sprintf&#40;'%s; charset=%s', $mime, $charset&#41;&#41;;
	&#125;


	/**
	* E-mail subject
	*
	* @access public
	* @param string $subject subject
	*/
	function subject&#40;$subject&#41;
	&#123;
		$this->_clean&#40;$subject&#41;;
		$this->_add_hdr&#40;'SUBJECT', $this->encode_hdrs&#40;$subject&#41;&#41;;
	&#125;


	/**
	* E-mail body
	*
	* @access public
	* @param string $body body
	*/
	function body&#40;$body&#41;
	&#123;
		$body = preg_replace&#40;"/&#40;&#91;\n|\r&#93;&#41;\.&#40;&#91;\n|\r&#93;&#41;/", "$1..$2", $body&#41;;
		$this->_body = sprintf&#40;"\r\n%s", $body&#41;;
	&#125;


	/**
	* Send the mail
	*
	* @access public
	*/
	function send&#40;&#41;
	&#123;
		$resp = $this->_cmd&#40;'DATA'&#41;;
		if &#40;$this->_is_closed&#40;$resp&#41;&#41; &#123;
			$this->close&#40;&#41;;
			return false;
		&#125;
		foreach &#40;$this->_hdrs as $header&#41; &#123;
			fwrite&#40;$this->_connection, $header&#41;;
			if &#40;$this->_debug&#41; &#123;
				printf&#40;$header&#41;;
			&#125;
		&#125;
		fwrite&#40;$this->_connection,$this->_body&#41;;
		fwrite&#40;$this->_connection, "\r\n.\r\n"&#41;;
		$resp = trim&#40;$this->_read&#40;&#41;&#41;;
		if &#40;$this->_debug&#41;&#123;
			printf&#40;"%s\r\n", $this->_body&#41;;
			printf&#40;"\r\n.\r\n"&#41;;
			printf&#40;'%s', $resp&#41;;
		&#125;
		if &#40;&#40;int&#41;$resp&#123;0&#125; != 2&#41; &#123;
			return false;
		&#125; else &#123;
			return true;
		&#125;
	&#125;


	/**
	* encode headers
	*
	* @access private
	* @param string $input
	* @return string
	*/
	function encode_hdrs&#40;$input&#41;
	&#123;
		$replacement = preg_replace&#40;'/&#40;&#91;\x80-\xFF&#93;&#41;/e', '"=" . strtoupper&#40;dechex&#40;ord&#40;"\1"&#41;&#41;&#41;', $input&#41;;
		$input = str_replace&#40;$input, sprintf&#40;'=?%s?Q?%s?=', $this->_charset, $replacement&#41;, $input&#41;;
		return $input;
	&#125;


&#125;

?>

Функцията с която изпращам емайл

Code: Select all

function sent_mail&#40;$to,$fromname,$fromemail,$subject,$body,$multiple=false,$multiplemail=''&#41; &#123;
	global $SITENAME,$SITEEMAIL,$smtptype,$smtp,$smtp_host,$smtp_port,$smtp_from,$smtpaddress,$accountname,$accountpassword,$rootpath;
	# Sent Mail Function v.05 by xam &#40;This function to help avoid spam-filters.&#41;
	$result = true;
	if &#40;$smtptype == 'default'&#41; &#123;
		@mail&#40;$to, $subject, $body, "From&#58; $SITEEMAIL"&#41; or $result = false;
	&#125; elseif &#40;$smtptype == 'advanced'&#41; &#123;
	# Is the OS Windows or Mac or Linux?
	if &#40;strtoupper&#40;substr&#40;PHP_OS,0,3&#41;=='WIN'&#41;&#41; &#123;
		$eol="\r\n";
		$windows = true;
	&#125;
	elseif &#40;strtoupper&#40;substr&#40;PHP_OS,0,3&#41;=='MAC'&#41;&#41;
		$eol="\r";
	else
		$eol="\n";
	$mid = md5&#40;getip&#40;&#41; . $fromname&#41;;
	$name = $_SERVER&#91;"SERVER_NAME"&#93;;
	$headers .= "From&#58; $fromname <$fromemail>".$eol;
	$headers .= "Reply-To&#58; $fromname <$fromemail>".$eol;
	$headers .= "Return-Path&#58; $fromname <$fromemail>".$eol;
	$headers .= "Message-ID&#58; <$mid.thesystem@$name>".$eol;
	$headers .= "X-Mailer&#58; PHP v".phpversion&#40;&#41;.$eol;
    $headers .= "MIME-Version&#58; 1.0".$eol;
    $headers .= "Content-type&#58; text/plain; charset=windows-1251".$eol;
    $headers .= "X-Sender&#58; PHP".$eol;
    if &#40;$multiple&#41;
    	$headers .= "Bcc&#58; $multiplemail.$eol";
	if &#40;$smtp == "yes"&#41; &#123;
		ini_set&#40;'SMTP', $smtp_host&#41;;
		ini_set&#40;'smtp_port', $smtp_port&#41;;
		if &#40;$windows&#41;
			ini_set&#40;'sendmail_from', $smtp_from&#41;;
		&#125;

    	@mail&#40;$to, $subject, $body, $headers&#41; or $result = false;

    	ini_restore&#40;SMTP&#41;;
		ini_restore&#40;smtp_port&#41;;
		if &#40;$windows&#41;
			ini_restore&#40;sendmail_from&#41;;
	&#125; elseif &#40;$smtptype == 'external'&#41; &#123;
		require_once&#40;$rootpath . 'include/smtp/smtp.lib.php'&#41;;
		$mail = new smtp;
		$mail->debug&#40;false&#41;;
		$mail->open&#40;$smtp_host, $smtp_port&#41;;
		if &#40;!empty&#40;$accountname&#41; && !empty&#40;$accountpassword&#41;&#41;
			$mail->auth&#40;$accountname, $accountpassword&#41;;
		$mail->from&#40;$SITEEMAIL&#41;;
		$mail->to&#40;$to&#41;;
		$mail->subject&#40;$subject&#41;;
		$mail->body&#40;$body&#41;;
		$result = $mail->send&#40;&#41;;
		$mail->close&#40;&#41;;
	&#125; else
		$result = false;

	return $result;

raiden
Гуру
Гуру
Posts: 2182
Joined: Fri Dec 08, 2006 8:13 am
Answers: 190
Location: Варна

Post by raiden » Mon Sep 17, 2018 4:50 pm

Вероятно нямаш Reverse DNS (PTR) Record на мейл сървъра или reverse lookup-а връща домейн различен от този, от който се опитваш да изпратиш имейл

Pro_Lamer4e
Нов
Нов
Posts: 188
Joined: Mon Oct 26, 2009 5:10 am

Post by Pro_Lamer4e » Tue Sep 18, 2018 6:35 am

Благодаря, но не е от хотинга. смених го с по нова версия и се орпави , с това нямам проблеми вече https://github.com/PHPMailer/PHPMailer

Post Reply