利用PHPMailer 來完成PHP的郵件發送


1.首先是下載PHPMailer

http://code.google.com/a/apache-extras.org/p/phpmailer/

 

2.解壓

從中取出class.phpmailer.php 和 class.smtp.php 放到你的項目的文件夾,因為我們等下會引用到它們.

 

3.創建發送郵件的函數,其中你需要配置smtp服務器

function postmail($to,$subject = '',$body = ''){
    //Author:Jiucool WebSite: http://www.jiucool.com
    //$to 表示收件人地址 $subject 表示郵件標題 $body表示郵件正文
    //error_reporting(E_ALL);
    error_reporting(E_STRICT);
    date_default_timezone_set('Asia/Shanghai');//設定時區東八區
    require_once('class.phpmailer.php');
    include('class.smtp.php');
    $mail             = new PHPMailer(); //new一個PHPMailer對象出來
    $body            = eregi_replace("[\]",'',$body); //對郵件內容進行必要的過濾
    $mail->CharSet ="GBK";//設定郵件編碼,默認ISO-8859-1,如果發中文此項必須設置,否則亂碼
    $mail->IsSMTP(); // 設定使用SMTP服務
    $mail->SMTPDebug  = 1;                     // 啟用SMTP調試功能
    // 1 = errors and messages
    // 2 = messages only
    $mail->SMTPAuth   = true;                  // 啟用 SMTP 驗證功能
    $mail->SMTPSecure = "ssl";                 // 安全協議,可以注釋掉
    $mail->Host       = 'stmp.163.com';      // SMTP 服務器
    $mail->Port       = 25;                   // SMTP服務器的端口號
    $mail->Username   = 'wangliang_198x';  // SMTP服務器用戶名,PS:我亂打的
    $mail->Password   = 'password';            // SMTP服務器密碼
    $mail->SetFrom('xxx@xxx.xxx', 'who');
    $mail->AddReplyTo('xxx@xxx.xxx','who');
    $mail->Subject    = $subject;
    $mail->AltBody    = 'To view the message, please use an HTML compatible email viewer!'; // optional, comment out and test
    $mail->MsgHTML($body);
    $address = $to;
    $mail->AddAddress($address, '');
    //$mail->AddAttachment("images/phpmailer.gif");      // attachment
    //$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment
    if(!$mail->Send()) {
        echo 'Mailer Error: ' . $mail->ErrorInfo;
    } else {
//        echo "Message sent!恭喜,郵件發送成功!";
    }
}

 

4. 使用函數

postmail('wangliang_198x@163.com','My subject','嘩啦啦');

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM