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

來源:互聯網
上載者:User
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','嘩啦啦');
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.