使用 require_once("../lib/testmail.php");$smtp = new smtp_mail("smtp服务器地址","25","帐号","密码",true,false); $smtp->send("发送人","收件人信箱","主题:HELLO","内容:THIS IS A TEST MAIL",array("../test/!00004.pdg"));
to feixuehenshui(飞雪恨水) 你的这个代码 我在公司设好后运行 不报错 可是mail没有发出。 我在家中运行 报错Warning: socket_connect() unable to connect [0]: 在一个非套接字上尝试了一个操作。 in E:\armiroot\smtp\my.php on line 63 创建SOCKET连接失败,请检查您的网络连接和参数
//使用phpMailer的例子 require("mail.inc.php");// Instantiate your new class $mail = new my_phpmailer;// Now you only need to add the necessary stuff $mail->AddAddress("[email protected]", "Josh Adams"); $mail->Subject = "Here is the subject"; $mail->Body = "This is the message body"; $mail->AddAttachment("c:/temp/11-10-00.zip", "new_name.zip"); // optional nameif(!$mail->Send()) { echo "There was an error sending the message"; exit; }echo "Message was sent successfully";
公司的机器一般的都是真实对外发的,如果不是那可就没有什么好办法,端口映射可能可以,我指的是代理上网!
那是不可能的。
用了一下还不错
支持附件
<?php
// 需要 php_sockets.dll 扩展模块
set_time_limit(120); class smtp_mail
{
var $host; //主机
var $port; //端口 一般为25
var $user; //SMTP认证的帐号
var $pass; //认证密码
var $debug = false; //是否显示和服务器会话信息
var $conn;
var $result_str; //结果
var $in; //客户机发送的命令
var $from; //源信箱
var $to; //目标信箱
var $subject; //主题
var $body; //内容
var $auth=false;
var $content_type = "text/plain"; // 调试函数
function debug_show($strMessage)
{
if($this->debug)
{
echo $strMessage."<p>\r\n";
}
} // 功能:实现SMTP命令的发送
function docommand()
{
socket_write ($this->socket, $this->in, strlen ($this->in));
$this->debug_show("客户机命令:".$this->in);
$this->result_str = "服务器应答:<font color=#cc0000>".socket_read ($this->socket, 1024)."</font>";
$this->debug_show($this->result_str);
} // 功能:初始化SMTP邮件发送
function smtp_mail($host,$port,$user,$pass,$auth=false,$debug=false)
{
$this->host = $host;
$this->port = $port;
$this->user = base64_encode($user);
$this->pass = base64_encode($pass);
$this->debug = $debug;
$this->auth = $auth; // 初始化SOCKET
$this->socket = socket_create (AF_INET, SOCK_STREAM, SOL_TCP);
if($this->socket)
{
$this->result_str = "创建SOCKET:".socket_strerror(socket_last_error());
$this->debug_show($this->result_str);
}
else
{
exit("创建SOCKET失败,请检查您的网络连接和参数");
} // 建立到SMTP服务器的TCP连接
$this->conn = socket_connect($this->socket,$this->host,$this->port);
if($this->conn)
{
$this->result_str = "创建SOCKET连接:".socket_strerror(socket_last_error());
$this->debug_show($this->result_str);
}
else
{
exit("创建SOCKET连接失败,请检查您的网络连接和参数");
}
$this->result_str = "服务器应答:<font color=#cc0000>".socket_read ($this->socket, 1024)."</font>";
$this->debug_show($this->result_str);
} // 功能:实现真正的邮件传送
function send($from,$to,$subject,$body,$attache="")
{
if($from == "" || $to == "")
{
exit("请输入信箱地址");
}
if($subject == "")
{
$sebject = "无标题";
}
if($body == "")
{
$body = "无内容";
} $this->from = $from;
$this->to = $to;
$this->subject = $subject;
$this->body = $body; $All = "From:".$this->from."\r\n";
$All.= "To:".$this->to."\r\n";
$All.= "Subject:".$this->subject."\r\n";
$All.= "Mime-Version: 1.0\r\n"; // 判断是否有附件
if(is_array($attache))
{
// 发送含有附件的内容
$boundary = "===" . md5(uniqid("")) . "==="; $All.= "Content-Type: multipart/mixed; boundary=\"" . $boundary . "\"\r\n\r\n";
$All.= "This is a multi-part message in MIME format.\r\n\r\n";
$All.= "--" . $boundary . "\r\n";
$All.= "Content-Type: ". $this->content_type ."\r\n";
$All.= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$All.= $this->body . "\r\n\r\n"; while(list($key, $val) = each($attache))
{
$fd = fopen($val, "r") or die("不能打开文件".$val."!\r\n");
$contents = chunk_split(base64_encode(fread($fd,filesize($val))));
fclose($fd);
$All.= "--" . $boundary . "\r\n";
$All.= "Content-Type: application/octet-stream; name=\"".basename($val)."\"\r\n";
$All.= "Content-Transfer-Encoding: BASE64\r\n";
$All.= "Content-Disposition: attachment; filename=\"".basename($val)."\"\r\n\r\n";
$All.= $contents . "\r\n";
}
$All.= "--" . $boundary . "--\r\n";
}
else
{
// 发送无附件的内容
$All.= "Content-Type: ".$this->content_type."\r\n";
$All.= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$All.= $this->body;
} //以下是和服务器会话
$this->in = "EHLO HELO\r\n";
$this->docommand(); // 是否需要验证
if($this->auth)
{
$this->in = "AUTH LOGIN\r\n";
$this->docommand(); $this->in = $this->user."\r\n";
$this->docommand(); $this->in = $this->pass."\r\n";
$this->docommand();
} $this->in = "MAIL FROM:".$this->from."\r\n";
$this->docommand(); $this->in = "RCPT TO:".$this->to."\r\n";
$this->docommand(); $this->in = "DATA\r\n";
$this->docommand(); $this->in = $All."\r\n.\r\n";
$this->docommand(); //结束,关闭连接
$this->in = "QUIT\r\n";
$this->docommand();
}
}
?>
require_once("../lib/testmail.php");$smtp = new smtp_mail("smtp服务器地址","25","帐号","密码",true,false);
$smtp->send("发送人","收件人信箱","主题:HELLO","内容:THIS IS A TEST MAIL",array("../test/!00004.pdg"));
创建SOCKET连接失败,请检查您的网络连接和参数
或许有用,试试吧!这个我的是可以的。
require("mail.inc.php");// Instantiate your new class
$mail = new my_phpmailer;// Now you only need to add the necessary stuff
$mail->AddAddress("[email protected]", "Josh Adams");
$mail->Subject = "Here is the subject";
$mail->Body = "This is the message body";
$mail->AddAttachment("c:/temp/11-10-00.zip", "new_name.zip"); // optional nameif(!$mail->Send())
{
echo "There was an error sending the message";
exit;
}echo "Message was sent successfully";