但我的程序是下面这种是发送邮件的源码,可以发送邮件,
但不能自定义发件人名称, 请各位老师给指点下。
<?
class smail {
//您的SMTP 服务器供应商,可以是域名或IP地址
var $smtp = "smtp.qq.com";//我用的是qq的smtp的服务器,你可以换成自己的
//SMTP需要要身份验证设值为 1 不需要身份验证值为 0,现在大多数的SMTP服务商都要验证,如不清楚请与你的smtp 服务商联系。
var $check = 1;
//您的email帐号名称
var $username = "";
//您的email密码
var $password = "";
//此email 必需是发信服务器上的email
var $s_from = "";
function smail ( $from, $password, $smtp, $check = 1 ) {
if( preg_match("/^[^\d\-_][\w\-]*[^\-_]@[^\-][a-zA-Z\d\-]+[^\-](\.[^\-][a-zA-Z\d\-]*[^\-])*\.[a-zA-Z]{2,3}/", $from ) ) {
$this->username = substr( $from, 0, strpos( $from , "@" ) );
$this->password = $password;
$this->smtp = $smtp ? $smtp : $this->smtp;
$this->check = $check;
$this->s_from = $from;
}
}
function send ( $to, $from, $subject, $message ) {
//连接服务器
$fp = fsockopen ( $this->smtp, 25, $errno, $errstr, 60);
if (!$fp ) return "联接服务器失败".__LINE__;
set_socket_blocking($fp, true );
$lastmessage=fgets($fp,512);
if ( substr($lastmessage,0,3) != 220 ) return "错误信息1:$lastmessage".__LINE__;
//HELO
$yourname = "YOURNAME";
if($this->check == "1") $lastact="EHLO ".$yourname."
";
else $lastact="HELO ".$yourname."
";
fputs($fp, $lastact);
$lastmessage == fgets($fp,512);
if (substr($lastmessage,0,3) != 220 ) return "错误信息2:$lastmessage".__LINE__;
while (true) {
$lastmessage = fgets($fp,512);
if ( (substr($lastmessage,3,1) != "-") or (empty($lastmessage)) )
break;
}
//身份验证
if ($this->check=="1") {
//验证开始
$lastact="AUTH LOGIN"."
";
fputs( $fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != 334) return "错误信息3:$lastmessage".__LINE__;
//用户姓名
$lastact=base64_encode($this->username)."
";
fputs( $fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != 334) return "错误信息4:$lastmessage".__LINE__;
//用户密码
$lastact=base64_encode($this->password)."
";
fputs( $fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != "235") return "错误信息5:$lastmessage".__LINE__;
}
//FROM:
$lastact="MAIL FROM: <". $this->s_from . ">
";
fputs( $fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != 250) return "错误信息6:$lastmessage".__LINE__;
//TO:
$lastact="RCPT TO: <". $to .">
";
fputs( $fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != 250) return "错误信息7:$lastmessage".__LINE__;
//DATA
$lastact="DATA
";
fputs($fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != 354) return "错误信息8:$lastmessage".__LINE__;
//处理Subject头
$head="Subject: $subject
";
$message = $head."
".$message;
//处理From头
$head="From: $from
";
$message = $head.$message;
你说可以发送邮件,这段代码我没有具体测试,你说需要可以设置发件人名称,代码$this->username = substr( $from, 0, strpos( $from , "@" ) );这个获取的是你的发件人的邮箱截取的名称,你可以单独命名一个参数传过去,直接页面上复制一下发件人的HTML代码修改一下为发件人名称就可以实现了。
我将 $this->username = substr( $from, 0, strpos( $from , "@" ) );
改成 $this->username = "新订单";
测试,出错:
Deprecated: Function set_socket_blocking() is deprecated in E:\www\email\sendEmail.class.php on line 27
错误信息5:535 Error: authentication failed 63
@往事又如烟: 这个错误提示是这个函数已经被废弃了,参考地址http://php.net/manual/zh/function.set-socket-blocking.php
@unofficial: 感谢能收到您的回复
现在这个可以发送邮件,但就是不能自定议发送人名称,具体怎么传递这个参数,
//处理From头
$head="From: $from
";
我把这个 $from更改后, 收到邮件 提示未设置发件人
@ unofficial this->username = substr( $from, 0, strpos( $from , "@" ) ); 这个地方不能改,改后就不能验证了,
可以给:
多加一个参数,比如:
然后把上面哪行改成:
$header
.=
"From: $zdy<"
.
$from
.
">\r\n"
;
了解了。因为昨晚没有具体看后面的代码,当时把username当成了发件人名称,于是就出现了上面的一幕,不好意思,是我的疏忽,你的做法挺不错的。