PHP中發郵件____PHP

來源:互聯網
上載者:User

測試代碼

<?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();?>

這裡引用了一個 外部的類 PHPMailer 。 這個類 官網是http://phpmailer.worxware.com/ ,好像官網讓去 github上 下載。 

我用這個發郵件時候,一開始 怎麼也發不出去 ,然後把debug開關開啟 $mail->SMTPDebug = 2;

發現是 伺服器名稱 不對 ,然後去這個類檔案裡看了看 ,class.phpmailer.php

在serverHostname 這個函數中,把_SERVER 全域變數列印出來 ,發現 我的hostname 並不是放在 _SERVER['SERVERNAME']  這個變數中,而是放在 _SERVER['HOSTNAME'] , 解決辦法是 ,要麼把SERVERNAME改成HOSTNAME ,要麼 在程式裡 對$this->Hostname 這個變數賦值 。看情況自己改了。 我是直接在類裡改的。


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.