PHP的一个完整SMTP类(解决邮件服务器需要验证时的问题)

2016-01-29 15:01 30 1 收藏

PHP的一个完整SMTP类(解决邮件服务器需要验证时的问题),PHP的一个完整SMTP类(解决邮件服务器需要验证时的问题)

【 tulaoshi.com - PHP 】

 

smtp.php

<?php
class smtp

{

/* Public Variables */

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/php/)

var $smtp_port;

var $time_out;

var $host_name;

var $log_file;

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/php/)

var $relay_host;

var $debug;

var $auth;

var $user;

var $pass;

/* Private Variables */
var $sock;

/* Constractor */

function smtp($relay_host = "", $smtp_port = 25,$auth = false,$user,$pass)

{

$this-debug = FALSE;

$this-smtp_port = $smtp_port;

$this-relay_host = $relay_host;

$this-time_out = 30; //is used in fsockopen()
#

$this-auth = $auth;//auth

$this-user = $user;

$this-pass = $pass;

#

$this-host_name = "localhost"; //is used in HELO command
$this-log_file = "";

 

$this-sock = FALSE;

}

/* Main Function */

function sendmail($to, $from, $subject = "", $body = "", $mailtype, $cc = "", $bcc = "", $additional_headers = "")

{

$mail_from = $this-get_address($this-strip_comment($from));

$body = ereg_replace("(^|(rn))(.)", "1.3", $body);

$header .= "MIME-Version:1.0rn";

if($mailtype=="HTML"){

$header .= "Content-Type:text/htmlrn";

}

$header .= "To: ".$to."rn";

if ($cc != "") {

$header .= "Cc: ".$cc."rn";

}

$header .= "From: $from<".$from."rn";

$header .= "Subject: ".$subject."rn";

$header .= $additional_headers;

$header .= "Date: ".date("r")."rn";

$header .= "X-Mailer:By Redhat (PHP/".phpversion().")rn";

list($msec, $sec) = explode(" ", microtime());

$header .= "Message-ID: <".date("YmdHis", $sec).".".($msec*1000000).".".$mail_from."rn";

$TO = explode(",", $this-strip_comment($to));

if ($cc != "") {

$TO = array_merge($TO, explode(",", $this-strip_comment($cc)));

}

if ($bcc != "") {

$TO = array_merge($TO, explode(",", $this-strip_comment($bcc)));

}

$sent = TRUE;

foreach ($TO as $rcpt_to) {

$rcpt_to = $this-get_address($rcpt_to);

if (!$this-smtp_sockopen($rcpt_to)) {

$this-log_write("Error: Cannot send email to ".$rcpt_to."n");

$sent = FALSE;

continue;

}

if ($this-smtp_send($this-host_name, $mail_from, $rcpt_to, $header, $body)) {

$this-log_write("E-mail has been sent to <".$rcpt_to."n");

} else {

$this-log_write("Error: Cannot send email to <".$rcpt_to."n");

$sent = FALSE;

}

fclose($this-sock);

$this-log_write("Disconnected from remote hostn");

}

return $sent;

}

 

/* Private Functions */

 

function smtp_send($helo, $from, $to, $header, $body = "")

{

if (!$this-smtp_putcmd("HELO", $helo)) {

return $this-smtp_error("sending HELO command");

}

#auth

if($this-auth){

if (!$this-smtp_putcmd("AUTH LOGIN", base64_encode($this-user))) {

return $this-smtp_error("sending HELO command");

}

if (!$this-smtp_putcmd("", base64_encode($this-pass))) {

return $this-smtp_error("sending HELO command");

}

}

#

if (!$this-smtp_putcmd("MAIL", "FROM:<".$from."")) {

return $this-smtp_error("sending MAIL FROM command");

}

if (!$this-smtp_putcmd("RCPT", "TO:<".$to."")) {

return $this-smtp_error("sending RCPT TO command");

}

if (!$this-smtp_putcmd("DATA")) {

return $this-smtp_error("sending DATA command");

}

if (!$this-smtp_message($header, $body)) {

return $this-smtp_error("sending message");

}

if (!$this-smtp_eom()) {

return $this-smtp_error("sending <CR<LF.<CR<LF [EOM]");

}

if (!$this-smtp_putcmd("QUIT")) {

return $this-smtp_error("sending QUIT command");

}

return TRUE;

}

来源:https://www.tulaoshi.com/n/20160129/1495049.html

延伸阅读
本文的对象是COM编程初学者,其目的旨在描述如何用ATL创建COM服务器,以及如何在VC或VB编写的客户端应用程序中调用COM服务器。为了不给初学者增加负担,本文不打算深入讨论COM和IDL的细节,而是展示用ATL创建简单的COM对象所需要的步骤。希望通过这篇文章能刺激你学习COM编程的欲望。 第一步:运行ATL COM向导(参见图一) 你要...
  文/赵杰 近日,由于市教育城域网网络结构调整,对我校电脑IP地址进行了重新划分。完成后,出现一个问题:教师机和学生机能正常上网,网关服务器只能够登录市教委网站和城域网其他学校网站,不能上因特网。询问市网络中心,得知各学校都暂时存在这个问题,原因是各校网关服务器的外网卡IP地址未能被正确路由到上一级网络中心(...
标签: 服务器
Linux服务器磁盘占满问题解决   今天下班某电商技术部leader发现个问题,说他们服务器硬盘满了。把日志文件都删掉了,可硬盘空间依旧满。于是df -h查看了下各个挂载点的状况(如下图)。 /dev/sda3占用了100%,那么我们du -s -h ./*看下目录的占用情况(如下图)。 linux磁盘挂载点目录占用情况(图) 挂载点下的目录之...
标签: PHP
转自:http://www.webdn.com/web_file/program/php/0602080309/ 【摘 要】 本文将演示如何使用PHP连接一个LDAP服务器。具体的例子是连接到一个公共的LDAP服务器并且进行搜索。这个例子模拟的是Netscape Communicator 4.*,通过自己的地址本连接到LDAP资源。   本文将演示如何使用PHP连接一个LDAP服务器。具体的例子是...
标签: PHP
对于需要身份验证的页面,使用apache服务器验证是最好不过的了。 不过,apache服务器验证的界面不够友好。而且,并不是所有的情况 都可以使用apache服务器验证,比如cgi模式的php,iis下的php。 用session可以在不同页面间保存用户身份,比如 login.php if ($name=="" && $pass=="") { ?> } else { if($name!="uuu" || ...

经验教程

226

收藏

12
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部