Home > Backend Development > PHP Tutorial > PHP中发邮件

PHP中发邮件

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-23 13:44:14
Original
842 people have browsed it

测试代码

<?phpheader ('Content-Type: text/xml; charset=utf-8');require_once('./PHPMailer-master/class.phpmailer.php');function sendMail() {    $mail = new PHPMailer();    //采用SMTP发送邮件    $mail->IsSMTP();    $mail->SMTPDebug = 0;    $mail->SMTPAuth = true;     $mail->SMTPSecure = "tls";      $mail->CharSet = "utf-8";    $mail->Encoding = "base64";     $mail->MessageID = time();     $mail->Host = 'email.163.com';    $mail->Port = 587;    $mail->Username = 'yourname';    $mail->Password = 'password';    $mail->From = 'yourname@163.com';    //发件人                       $mail->FromName = 'some name';    //收件人邮件组    $mail->AddAddress('dest@163.com', 'his name');    $body = "Hi all:<br>                this is a test mail<br>            <br>";    $mail->Subject = 'a subject ';    $mail->MsgHTML($body);    $mail->Send();}sendMail();?>
Copy after login

这里引用了一个 外部的类 PHPMailer 。 这个类 官网是http://phpmailer.worxware.com/ ,好像官网让去 github上 下载。 

我用这个发邮件时候,一开始 怎么也发不出去 ,然后把debug开关打开 $mail->SMTPDebug = 2;

发现是 服务器名称 不对 ,然后去这个类文件里看了看 ,class.phpmailer.php

在serverHostname 这个函数中,把_SERVER 全局变量打印出来 ,发现 我的hostname 并不是放在 _SERVER['SERVERNAME']  这个变量中,而是放在 _SERVER['HOSTNAME'] , 解决办法是 ,要么把SERVERNAME改成HOSTNAME ,要么 在程序里 对$this->Hostname 这个变量赋值 。看情况自己改了。 我是直接在类里改的。


Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template