phpmailer{群發並且發送附件}


PHPMailer是一個用於發送電子郵件的PHP函數包。

    第一,需要下載PHPMailer文件包phpmailer. http://phpmailer.sourceforge.net/
    第二,確認你的服務器系統已經支持socket ,通過phpinfo();查看是否支持sockets(socket 是屬於PHP擴展部分),如果顯現為    “enabled”,那就是支持了。
     第三,把文件解壓到你的web服務器目錄下,調用類就可以了.
     首先包含class.phpmailer.php,然后創建對象,設置參數,調用成員函數。具體請見下面的示例代碼:

    require("phpmailer/class.phpmailer.php");   
    function smtp_mail ($send,$sendto,$sendto_email, $subject, $body) {
        $mail = new PHPMailer();
        $mail->IsSMTP();                                                         // send via SMTP
        $mail->Host = $send['Host'];                                        // SMTP servers
        $mail->SMTPAuth = true;                                             // turn on SMTP authentication
        $mail->Username = $send['Username'];                      // SMTP username  注意:普通郵件認證不需要加 @域名
        $mail->Password = $send['Password'];                        // SMTP password

        $mail->From = $send['From'];                                      // 發件人郵箱
        $mail->FromName = $send['FromName'];                    // 發件人
 
        if(!empty($sendto['AddCC'][0])){                                  // 添加抄送
            for($i=0;$i<count($sendto['AddCC']);$i++)
            {
                $mail->AddCC($sendto['AddCC'][$i]);
            }
        }

        if(!empty($sendto['AddBCC'][0])){                                // 添加密送
            for($i=0;$i<count($sendto['AddBCC']);$i++)
            {
            $mail->AddBCC($sendto['AddBCC'][$i]); 
           }
        }

        $mail->CharSet = "UTF-8";                                            // 這里指定字符集
        $mail->Encoding = "base64";
        $mail->MessageID = time();
   
        for($i=0;$i<count($sendto['sendto_email']);$i++)
        {
             $mail->AddAddress($sendto['sendto_email'][$i]);                      // 收件人郵箱地址
        }
        $mail->IsHTML(true);                                                                       // send as HTML
        $mail->Subject = $subject;                                                               // 郵件主題
                                                                                                                // 郵件內容
        $mail->Body = '<html><head>
        <meta http-equiv="Content-Language" content="zh-cn">
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></head>
        <body> '.
                $sendto['body']
        .'</body>
         </html> ';                                                                      
   
         if(!empty($sendto['AddAttachment'][0])){
             foreach($sendto['AddAttachment'] as $value){
                 $mail->AddAttachment('D:/wwwSer/www/nwebs/uploads/'.$value);             // 附件的路徑和附件名稱
             }
         }
   
         $mail->AltBody ="text/html";
   
         if(!$mail->Send())
         {
            echo "發送有誤 <p>";
            echo "錯誤信息: " . $mail->ErrorInfo;
            exit;
        }
         else{
             echo "發送成功!<br/>";
         }
      }
   
    $send = array(                                                                                  // 接收所需要的數據
        'Host'     =>$Data2['info'][0]->SMTP,
        'Username' =>$Data2['info'][0]->username,
        'Password' =>$Auth->Auth_EmailPwDecrypt($Data2['info'][0]->password),
        'From'     =>$COMMON->Post("senduser"),
        'FromName' =>$userinfo->username
     );
   
    $sendto = array(
        'sendto_email' => $SendTo3,
        'AddCC'              => $AddCC3,
        'AddBCC'         => $AddBCC3,
        'subject'      => $COMMON->Post("subject"),
        'body'         => $COMMON->Post("content"),
        'AddAttachment'=> $FilesNames2
     );   

     //(發送到, 郵件主題, 郵件內容,用戶名)
     smtp_mail($send, $sendto, $sendto['sendto_email'], $sendto['subject'], $sendto['body']);
 }


要注意的內容:
    如果你想用它來群發郵件的話,記得修改包含文件函數,如: require("phpmailer/class.phpmailer.php"); 改為 require_once("phpmailer/class.phpmailer.php"); 否則的話會產生類的重定義。


免責聲明!

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



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