今天给各位分享怎么做一个php邮箱发送的知识,其中也会对php搭建邮箱源码进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录:
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网页中发送邮件
先建一个邮件发送的类 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
怎么利用php发送邮件求详细教程
PHP虽然提供了mail()函数,但并不好用,而PHPMailer是一个不错的邮件发送工具,接下来将详细介绍,需要了解的朋友可以参考下:
本人使用wamp集成开发环境,Apache2.4.4, Mysql5.6.12 , php5.4.12.开始的时候使用mail()发送邮件,更改配置始终无法成功,了解到mail()函数使用需要sendmail程序。又下载了sendmail程序扩展包。按照网上的说法也改好了php.ini和sendmail.ini。使用foxmail 7.1创建了自己的qq邮箱账户,开启了POP3/SMTP服务,更改发件服务器为POP3,使用和收件服务器相同的身份验证,结果还是报错:Warning: mail(): SMTP server response: 503 Error: need EHLO and AUTH first ! in F:\PHP\wamp\www\mail.php on line 8。以下是使用mail()函数发送邮件的php代码:
[php] view plain copy
span style="font-size:14px"?php
$to = "757006080@qq.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "757006080@qq.com";
$headers = "From: $from";
$send=mail($to,$subject,$message,$headers);
if($send)
echo "Mail Sent";
else
echo "Sorry,mail sent failed!"
?/span
在CSDN论坛上发现phpmailer可以方便快捷的发送邮件,以下写出详细使用教程:
1.需要下载PHPMailer文件包,(点击打开链接)
2.确认你的服务器已经系统支持socket,通过phpinfo()查看是否支持socket;
3.把文件解压到你的WEB服务器目录下,就可以使用PHPMailer发送邮件了。
以下为前台表单php代码:
[php] view plain copy
span style="font-size:14px"html
body
h3phpmailer Unit Test/h3
请你输入font color="#FF6666"收信/font的邮箱地址:
form name="phpmailer" action="testemail.php" method="post"
input type="hidden" name="submitted" value="1"/
邮箱地址: input type="text" size="50" name="to" /
br/
input type="submit" value="发送"/
/form
/body
/html /span
以下为后台程序:
[php] view plain copy
?php
/**
* Simple example script using PHPMailer with exceptions enabled
* @package phpmailer
* @version $Id$
*/
header("content-type:text/html;charset=utf-8");
ini_set("magic_quotes_runtime",0);
require('class.phpmailer.php');
try {
$mail = new PHPMailer(true); //New instance, with exceptions enabled
//$body = file_get_contents('contents.html');
//$body = preg_replace('/\\\\/','', $body); //Strip backslashes
$to = $_POST['to'];
$mail-CharSet="GB2312";//设置邮件字符编码否则邮件会乱码
$mail-Encoding="base64";
$mail-IsSMTP(); // tell the class to use SMTP
$mail-SMTPAuth = true; // enable SMTP authentication
$mail-Port = 25; // set the SMTP server port
$mail-Host = "smtp.qq.com"; // SMTP server
$mail-Username = "757006080@qq.com"; // SMTP server username
$mail-Password = "000000000000"; // SMTP server password
//$mail-IsSendmail(); // tell the class to use Sendmail
$mail-AddReplyTo("757006080@qq.com","han qing");
$mail-From = "757006080@qq.com";
$mail-FromName = "han qing";
//$to = "hanqing757@gmail.com";
$mail-AddAddress($to);
$mail-Subject =$mail-Subject = "=?utf-8?B?" . base64_encode("First PHPMailer Message") . "?=";
$mail-Body = "h1phpmailer演示/h1 这是用PHPMAILER发的第一份邮件,从QQ邮箱发到Google邮箱.";
$mail-AddAttachment("F:/myloe.jpg");
$mail-AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail-WordWrap = 80; // set word wrap
//$mail-MsgHTML($body);
$mail-IsHTML(true); // send as HTML
$mail-Send();
echo 'Message has been sent.';
} catch (phpmailerException $e) {
echo $e-errorMessage();
}
?
关于怎么做一个php邮箱发送和php搭建邮箱源码的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。