本人使用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 = "[email protected]"
$subject = "Test mail"
$message = "Hello! This is a simple email message."
$from = "[email protected]"
$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>
<h3>phpmailer 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/htmlcharset=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 = "[email protected]"// SMTP server username
$mail->Password = "000000000000" // SMTP server password
//$mail->IsSendmail() // tell the class to use Sendmail
$mail->AddReplyTo("[email protected]","han qing")
$mail->From = "[email protected]"
$mail->FromName = "han qing"
//$to = "[email protected]"
$mail->AddAddress($to)
$mail->Subject =$mail->Subject = "=?utf-8?B?" . base64_encode("First PHPMailer Message") . "?="
$mail->Body = "<h1>phpmailer演示</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()
}
?>
Thinkphp3.2 PHPMailer 发送邮件结合QQ企业邮箱发送邮件
下载附件PHPMailer解压到ThinkPHP\Library\Vendor
在Common文件夹新建function.php
/*** 邮件发送函数
*/
function sendMail($to, $title, $content) {
Vendor('PHPMailer.PHPMailerAutoload')
$mail = new PHPMailer() //实例化
$mail->IsSMTP() // 启用SMTP
$mail->Host=C('MAIL_HOST') //smtp服务器的名称(这里以QQ邮箱为例)
$mail->SMTPAuth = C('MAIL_SMTPAUTH') //启用smtp认证
$mail->Username = C('MAIL_USERNAME') //你的邮箱名
$mail->Password = C('MAIL_PASSWORD') //邮箱密码
$mail->From = C('MAIL_FROM') //发件人地址(也就是你的邮箱地址)
$mail->FromName = C('MAIL_FROMNAME') //发件人姓名
$mail->AddAddress($to,"尊敬的客户")
$mail->WordWrap = 50 //设置每行字符长度
$mail->IsHTML(C('MAIL_ISHTML')) // 是否HTML格式邮件
$mail->CharSet=C('MAIL_CHARSET') //设置邮件编码
$mail->Subject =$title //邮件主题
$mail->Body = $content //邮件内容
$mail->AltBody = "这是一个纯文本的身体在非营利的HTML电子邮件客户端" //邮件正文不支持HTML的备用显示
return($mail->Send())
}
添加配置文件config.php
// 配置邮件发送服务器'MAIL_HOST' =>'smtp.exmail.qq.com',//smtp服务器的名称
'MAIL_SMTPAUTH' =>TRUE, //启用smtp认证
'MAIL_USERNAME' =>'[email protected]',//你的邮箱名
'MAIL_FROM' =>'[email protected]',//发件人地址
'MAIL_FROMNAME'=>'聚丰集团',//发件人姓名
'MAIL_PASSWORD' =>'******',//邮箱密码
'MAIL_CHARSET' =>'utf-8',//设置邮件编码
'MAIL_ISHTML' =>TRUE, // 是否HTML格式邮件
最后就是使用PHPMailer发送邮件
<form action="__URL__/add" method="post" enctype="multipart/form-data">邮箱:<input type="text" id="mail" name="mail"/>
标题:<input type="text" id="title" name="title"/>
内容<input type="text" id="content" name="content"/>
<input class="button" type="submit" value="发送" style="margin: 0 autodisplay: block"/>
</form> public function add(){
if(SendMail($_POST['mail'],$_POST['title'],$_POST['content']))
$this->success('发送成功!')
else
$this->error('发送失败')
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)