本文目录一览:
PHP功能齐全的发送邮件类及用法,急,请好心人帮下忙。
是这样的。SMTP设置只能在windows服务器下使用。
如果你的服务器是Linux。直接就可以使用php的函数mail()发送邮件。
mail函数在windows下是不能够使用的。只能使用SMTP发送。
php如何发送邮件
你好,用这个邮件类,需要在调用时,填写一个smtp服务器和你的用户名密码。
?php
set_time_limit(600);
/*
* 邮件发送类
*/
class smail {
//您的SMTP 服务器供应商,可以是域名或IP地址
var $smtp = "";
//SMTP需要要身份验证设值为 1 不需要身份验证值为 0,现在大多数的SMTP服务商都要验证,如不清楚请与你的smtp 服务商联系。
var $check = 1;
//您的email帐号名称
var $username = "";
//您的email密码
var $password = "";
//此email 必需是发信服务器上的email
var $s_from = "";
/*
* 功能:发信初始化设置
* $from 你的发信服务器上的邮箱
* $password 你的邮箱密码
* $smtp 您的SMTP 服务器供应商,可以是域名或IP地址
* $check SMTP需要要身份验证设值为 1 不需要身份验证值为 0,现在大多数的SMTP服务商都要验证
*/
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;
}
}
/*
* 功能:发送邮件
* $to 目标邮箱
* $from 来源邮箱
* $subject 邮件标题
* $message 邮件内容
*/
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."\r\n";
else $lastact="HELO ".$yourname."\r\n";
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"."\r\n";
fputs( $fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != 334) return "错误信息3:$lastmessage".__LINE__;
//用户姓名
$lastact=base64_encode($this-username)."\r\n";
fputs( $fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != 334) return "错误信息4:$lastmessage".__LINE__;
//用户密码
$lastact=base64_encode($this-password)."\r\n";
fputs( $fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != "235") return "错误信息5:$lastmessage".__LINE__;
}
//FROM:
$lastact="MAIL FROM: ". $this-s_from . "\r\n";
fputs( $fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != 250) return "错误信息6:$lastmessage".__LINE__;
//TO:
$lastact="RCPT TO: ". $to ." \r\n";
fputs( $fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != 250) return "错误信息7:$lastmessage".__LINE__;
//DATA
$lastact="DATA\r\n";
fputs($fp, $lastact);
$lastmessage = fgets ($fp,512);
if (substr($lastmessage,0,3) != 354) return "错误信息8:$lastmessage".__LINE__;
//处理Subject头
$head="Subject: $subject\r\n";
$message = $head."\r\n".$message;
//处理From头
$head="From: $from\r\n";
$message = $head.$message;
//处理To头
$head="To: $to\r\n";
$message = $head.$message;
//加上结束串
$message .= "\r\n.\r\n";
//发送信息
fputs($fp, $message);
$lastact="QUIT\r\n";
fputs($fp,$lastace);
fclose($fp);
return 0;
}
}
// 发送示例
// 只需要把这部分改成你的信息就行
$sm = new smail( "用户名", "密码", "发件smtp服务器" );
$end = $sm-send( "收件人", "发件人(可以伪造哦)", "标题", "内容" );
if( $end ) echo $end;
else echo "发送成功!$x";
?
求一个能用的php发送邮件类,最好是个小例子
?php
require('PHPMailer/class.smtp.php');
$mail = new PHPMailer(true);
$mail-IsSMTP();
$mail-CharSet='UTF-8';
$mail-SMTPAuth = true;
$mail-Port = 25;
$mail-Host = "smtp.163.com";//邮箱smtp地址,此处以163为例
$mail-Username = "你的邮箱账号";//你的邮箱账号
$mail-Password = "你的邮箱密码";//你的邮箱密码
$mail-From = "你的邮箱账号";//你的邮箱账号
$mail-FromName = get_option('blogname');
$to = $to;//收件人
$mail-AddAddress($to);
$mail-Subject = $subject;//主题
$mail-Body = $message;//正文
$mail-WordWrap = 80;
//$mail-AddAttachment("f:/test.png"); //可以添加附件
$mail-IsHTML(true);
$mail-Send();
如何在PHP网页中发送邮件
先建一个邮件发送的类 sendEmail.class.php页面:
?
class smail {
//您的SMTP 服务器供应商,可以是域名或IP地址
var $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;
//处理To头
$head="To: $to
";
$message = $head.$message;
//加上结束串
$message .= "
.
";
//发送信息
fputs($fp, $message);
$lastact="QUIT
";
fputs($fp,$lastace);
fclose($fp);
return 0;
}
}
?
然后建立一个发送页面:sendEmail.php
?php
require_once('sendemail.class.php');
if((isset($_POST["send"]))($_POST["send"]=="form1"))
{
//获取收件人地址
$sendto=$_POST['txt1'];
$sendfrom="发件人邮箱";
$mailpass="发件人邮箱密码";
$mailserver="邮箱服务器";
$subject=$_POST['txt3'];
$message =$_POST['textarea'];
$sm = new smail( $sendfrom, $mailpass, $mailserver);
$end = $sm-send( $sendto, $sendfrom, $subject, $message );
if( $end ) echo $end;
else echo "scriptalert('发送成功');/script";
}
?
center
form style="width:524px; height:107px;" method="post" name="form1" id="form1" action=""
table width="524" height="107" border="0" align="center"
!--DWLayoutTable--
tr
td height="60" colspan="2" align="center" valign="middle" bgcolor="#999999"font color="#339933" size="+4" face="隶书"strong发送电子邮件/strong/font/td
/tr
tr
td width="109" height="44" align="left" valign="middle" bgcolor="#CCCC99"strongfont size="5"收件人:/font/strong/td
td width="405" align="left" valign="middle" bgcolor="#CCCC99"input type="text" name="txt1"//td
/tr
tr
td height="44" align="left" valign="middle" bgcolor="#99CCFF"strongfont size="5"主题:/font/strong/td
td valign="middle" bgcolor="#99CCFF"input type="text" name="txt3""//td
/tr
tr
td height="163" align="left" valign="middle" bgcolor="#66CCFF"strongfont size="5"内容:/font/strong/td
td valign="top" bgcolor="#66CCFF"textarea name="textarea" style="width:405px; height:163px"/textarea/td
/tr
tr
td height="37" colspan="2" align="center" valign="middle" bgcolor="#6699CC"input type="submit" value="发送"//td
/tr
/table
input type="hidden" name="send" value="form1"/
/form
/center