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"); 否则的话会产生类的重定义。

原文地址:https://www.cnblogs.com/lh460795/p/2874860.html