php 发送邮件
压缩包在文件里
<?php
/**
* 注:本邮件类都是经过我测试成功了的,如果大家发送邮件的时候遇到了失败的问题,请从以下几点排查:
* 1. 用户名和密码是否正确;
* 2. 检查邮箱设置是否启用了smtp服务;
* 3. 是否是php环境的问题导致;
* 4. 将26行的$smtp->debug = false改为true,可以显示错误信息,然后可以复制报错信息到网上搜一下错误的原因;
* 5. 如果还是不能解决,可以访问:http://www.daixiaorui.com/read/16.html#viewpl
* 下面的评论中,可能有你要找的答案。
*/
require_once "email.class.php";
//******************** 配置信息 ********************************
$smtpserver = "smtp.exmail.qq.com";//SMTP服务器
$smtpserverport =25;//SMTP服务器端口
$smtpusermail = "lijiageng@zhongkeyun.com";//SMTP服务器的用户邮箱
$smtpemailto = $_POST['toemail'];//发送给谁
$smtpuser = "lijiageng@zhongkeyun.com";//SMTP服务器的用户帐号
$smtppass = "48658521Gl";//SMTP服务器的用户密码
$mailtitle = $_POST['title'];//邮件主题
$mailcontent = "<h1>".$_POST['content']."</h1>";//邮件内容
$mailtype = "HTML";//邮件格式(HTML/TXT),TXT为文本邮件
//************************ 配置信息 ****************************
$smtp = new smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//这里面的一个true是表示使用身份验证,否则不使用身份验证.
$smtp->debug = false;//是否显示发送的调试信息
$state = $smtp->sendmail($smtpemailto, $smtpusermail, $mailtitle, $mailcontent, $mailtype);
echo "<div style='width:300px; margin:36px auto;'>";
if($state==""){
echo "对不起,邮件发送失败!请检查邮箱填写是否有误。";
echo "<a href='index.html'>点此返回</a>";
exit();
}
echo "恭喜!邮件发送成功!!";
echo "<a href='index.html'>点此返回</a>";
echo "</div>";
?>
第二种是 不引用文件 在一个类里面
<?php
$bfconfig = Array (
'sitename' => '网站名称',
);
$mail = Array (
'state' => 1,
'server' => 'smtp.exmail.qq.com',
'port' => 25,
'auth' => 1,
'username' => 'lijiageng@zhongkeyun.com',
'password' => '48658521Gl',
'charset' => 'gbk',
'mailfrom' => 'lijiageng@zhongkeyun.com'
);
function sendmail($mail_to, $mail_subject, $mail_message) {
global $mail, $bfconfig;
date_default_timezone_set('PRC');
$mail_subject = '=?'.$mail['charset'].'?B?'.base64_encode($mail_subject).'?=';
$mail_message = chunk_split(base64_encode(preg_replace("/(^|(\r\n))(\.)/", "\1.\3", $mail_message)));
$headers = '';
$headers .= "";
$headers .= "MIME-Version:1.0\r\n";
$headers .= "Content-type:text/html\r\n";
$headers .= "Content-Transfer-Encoding: base64\r\n";
$headers .= "From: ".$bfconfig['sitename']."<".$mail['mailfrom'].">\r\n";
$headers .= "Date: ".date("r")."\r\n";
list($msec, $sec) = explode(" ", microtime());
$headers .= "Message-ID: <".date("YmdHis", $sec).".".($msec * 1000000).".".$mail['mailfrom'].">\r\n";
if(!$fp = fsockopen($mail['server'], $mail['port'], $errno, $errstr, 30)) {
exit("CONNECT - Unable to connect to the SMTP server");
}
stream_set_blocking($fp, true);
$lastmessage = fgets($fp, 512);
if(substr($lastmessage, 0, 3) != '220') {
exit("CONNECT - ".$lastmessage);
}
fputs($fp, ($mail['auth'] ? 'EHLO' : 'HELO')." befen\r\n");
$lastmessage = fgets($fp, 512);
if(substr($lastmessage, 0, 3) != 220 && substr($lastmessage, 0, 3) != 250) {
exit("HELO/EHLO - ".$lastmessage);
}
while(1) {
if(substr($lastmessage, 3, 1) != '-' || empty($lastmessage)) {
break;
}
$lastmessage = fgets($fp, 512);
}
if($mail['auth']) {
fputs($fp, "AUTH LOGIN\r\n");
$lastmessage = fgets($fp, 512);
if(substr($lastmessage, 0, 3) != 334) {
exit($lastmessage);
}
fputs($fp, base64_encode($mail['username'])."\r\n");
$lastmessage = fgets($fp, 512);
if(substr($lastmessage, 0, 3) != 334) {
exit("AUTH LOGIN - ".$lastmessage);
}
fputs($fp, base64_encode($mail['password'])."\r\n");
$lastmessage = fgets($fp, 512);
if(substr($lastmessage, 0, 3) != 235) {
exit("AUTH LOGIN - ".$lastmessage);
}
$email_from = $mail['mailfrom'];
}
fputs($fp, "MAIL FROM: <".preg_replace("/.*\<(.+?)\>.*/", "\\1", $email_from).">\r\n");
$lastmessage = fgets($fp, 512);
if(substr($lastmessage, 0, 3) != 250) {
fputs($fp, "MAIL FROM: <".preg_replace("/.*\<(.+?)\>.*/", "\\1", $email_from).">\r\n");
$lastmessage = fgets($fp, 512);
if(substr($lastmessage, 0, 3) != 250) {
exit("MAIL FROM - ".$lastmessage);
}
}
foreach(explode(',', $mail_to) as $touser) {
$touser = trim($touser);
if($touser) {
fputs($fp, "RCPT TO: <".preg_replace("/.*\<(.+?)\>.*/", "\\1", $touser).">\r\n");
$lastmessage = fgets($fp, 512);
if(substr($lastmessage, 0, 3) != 250) {
fputs($fp, "RCPT TO: <".preg_replace("/.*\<(.+?)\>.*/", "\\1", $touser).">\r\n");
$lastmessage = fgets($fp, 512);
exit("RCPT TO - ".$lastmessage);
}
}
}
fputs($fp, "DATA\r\n");
$lastmessage = fgets($fp, 512);
if(substr($lastmessage, 0, 3) != 354) {
exit("DATA - ".$lastmessage);
}
fputs($fp, $headers);
fputs($fp, "To: ".$mail_to."\r\n");
fputs($fp, "Subject: $mail_subject\r\n");
fputs($fp, "\r\n\r\n");
fputs($fp, "$mail_message\r\n.\r\n");
$lastmessage = fgets($fp, 512);
if(substr($lastmessage, 0, 3) != 250) {
exit("END - ".$lastmessage);
}
fputs($fp, "QUIT\r\n");
}
$mail_to = '1582363645@qq.com';
$mail_subject = 'nihao a ';
$mail_message = 'd9fhisodjidjj';
sendmail($mail_to, $mail_subject, $mail_message);