下载sendmail
首先下载sendmail压缩包;并将其解压到D:盘中(一般最好不要解压到C:盘,且目录不要太长)。
设置PHP.ini文件:
[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = smtp.163.com
; http://php.net/smtp-port
smtp_port = 25
; For Win32 only.
; http://php.net/sendmail-from
sendmail_from = yourmail@163.com
; For Unix only. You may supply arguments as well (default: "sendmail -t -i").
; http://php.net/sendmail-path
sendmail_path ="D:\wamp\sendmail\sendmail.exe -t -i"
mail.add_x_header = On
分号‘;’表示注释的意思,smtp.163.com是说明采用的是163邮箱的smtp(simple mail transfer protocol 简单邮件传输协议的缩写)代理服务器(此服务默认未启用,需要自己手动开启),这是为邮件设置的专门通道发送数据报,就像快递一样层层递交,最后交到你的163账号发送出去。
yourmail@163.com是你站点的服务器调用来发邮件的From(简称发件人),先登录进去,在设置中的账户菜单下面启动IMAP/SMTP服务,发送完短信后,你将要设置第三方登录密码(它不是你平时登录邮箱的密码,为防止密码泄露,在公共状态下需要此第三方密码,其实就是授权码,它会在登录时被授予登录许可自动转换为普通密码),也就是sendmail.ini文件里的userpassword,username就是这个邮箱账号,其smtp一样是填smtp.163.com(如果是qq邮箱,两边一样填smtp.qq.com,不过qq的第三方密码不能自己设置)。
mail.add_x_header = On 中 On 的意思是 开启 mail()函数中的一个参数$header,里面包括了相关重要信息,像说明此邮件从哪里发送到哪去、字符格式是什么、包含什么格式的文本等诸如此类信息。
开启IMAP/SMTP服务(这是qq的,163的也差不多):
端口号:25是默认的,一般不用改。
设置sendmai.ini文件:
[sendmail]
smtp_server=smtp.163.com
; smtp port (normally 25)
smtp_port=25
smtp_ssl=auto
error_logfile=error.log
debug_logfile=debug.log
auth_username=yourmail@163.com
auth_password=zdfdsfsdfjdiffidj
error.log跟debug.log在同级目录下,若出现错误它就会自动建立txt文件告知最近一次error_message。
好,配置好后,接下来就可以使用mail()函数发邮件了。
用mail()发送邮件:
$to = "sendwho@example.com";
$subject = "网站反馈";
$msg = "<html><head><meta charset='utf-8'></head><body style='background-color:gray;'><h4>来自 $name 的一封邮件:</h4><div style='padding-left:20px;padding-top:5px;font-size:0.7em;'><strong>性别:</strong>$sex<br><strong>年龄范围:</strong>$age<br><strong>爱好:</strong>$hobby<br><strong>邮箱地址:</strong>.$dzyj<br><strong>手机号码:</strong>$telephone<br><strong>所属公司:</strong>.$company<br><strong>内容:</strong>$contents<br></div></body></html>";
$headers = "X-Mailer:PHP";
$headers .= "MIME-Version: 1.0";
$headers .= "PHP-Version:phpversion()\r\n";
$headers .= "Content-type:text/html;charset=utf-8\r\n";
$headers .= "From:yourmail@163.com\r\n";
$headers .= "Reply-To:yourmail@163.com\r\n";
if(!@mail($to,$subject,$msg,$headers)){
echo '<img class="warning" src="images/error.png">';
echo '<div id="sorry" style="font-size:1.5em;margin:30px 30px;"><p>遇到未知错误!无法提交相关信息</p><br/>'.'<a href="contact_form.php" style="font-size:1.0em">返回前一页</a></div>';
}
else{
echo '<img class="warning" src="images/success.png">';
echo '<div id="sorry" style="font-size:1.9em;margin:30px 30px;"><p>提交成功!</p><br/>'.'<a href="ddstore.php" style="font-size:1.0em">返回主页</a></div>';
$to发给谁;$subject邮件标题(其中必须为连续的字符且不包含特殊字符,不然会报错);$msg主体信息,我写成了html格式,效果比较好;而$header就是为了向服务器说明前面所有信息的详细细节。
发送成功后(手机客户端可以看到我的一些html的修饰,这是网页版的,没有什么效果,单纯的txt文本):
评论区