PHP通过phpmailer批量发送邮件功能

来源:互联网 发布:linux 自动启动svn 编辑:程序博客网 时间:2024/06/05 02:28

前端页面代码:

注意:目前发送人使用的qq邮箱支持的不是特别友好.建议使用网易 新浪 163等其他邮箱.

          需要用到phpmailer包 下载地址:https://sourceforge.net/projects/phpmailer/   http://download.csdn.net/detail/u014236259/9663181  

          发送到多个邮箱用“,”隔开。例:  889955@qq.com,6666666@qq.com,5454545@qq.com

          

          

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">    <head>         <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />         <title></title>        <link rel="stylesheet" type="text/css" href="http://www.sucaihuo.com/jquery/css/common.css" />        <style type="text/css">            .demo{ margin:40px auto 0 auto; min-height:250px;}            .demo p{line-height:30px; padding:4px}            #result{color:#f30; font-weight:bold}            hr{border:1px red dotted;}        </style>    </head>    <body>        <div class="head">            <div class="head_inner clearfix">                <ul id="nav">                    <hr style="height:20px;width:500px;border:none;border-top:1px dashed red;" />                </ul>            </div>        </div>        <div class="container">            <div class="demo">                <p>账号:<input type="text" style="border-top-color:red;" class="input" name="account" id="account" value="************@163.com"/></p>                <p>密码:<input type="password" class="input" name="pwd" id="pwd" value="1838888888888"/></p>                <p>smpt:<input type="text" class="input" name="smpt" id="smpt" value="smtp.163.com"/></p>                <p>标题:<input type="text" class="input" name="title" id="title" value="测试标题"/></p>                <p>内容:<textarea name="body" id="body">测试内容</textarea></p>                <p>邮箱:<textarea name="email" id="email" autocomplete="off"></textarea> 多个邮箱用英文,号隔开</p>                <p><input type="button" class="btn" id="send" value="发送邮件" onclick="send_email()"/></p>                <div id="result"></div>            </div>        </div>        <div class="foot">        </div><!--        <script type="text/javascript" src="http://libs.useso.com/js/jquery/1.7.2/jquery.min.js"></script>-->        <script type="text/javascript" src="jquery-3.1.0.min.js"></script>        <script type="text/javascript">                        function send_email() {                            var account = $('#account').val();                            if (account == "") {                                alert("账号不能为空!");                                return false;                            }                            var pwd = $('#pwd').val();                            if (pwd == "") {                                alert("密码不能为空!");                                return false;                            }                            var smpt = $('#smpt').val();                            if (smpt == "") {                                alert("smpt不能为空!");                                return false;                            }                            var email = $('#email').val();                            if (email == "") {                                alert("邮箱不能为空!");                                return false;                            }                            var title = $('#title').val();                            if (title == "") {                                alert("标题不能为空!");                                return false;                            }                            var body = $('#body').val();                            if (body == "") {                                alert("内容不能为空!");                                return false;                            }                            $("#send").addClass("loading").val("loading...").attr("disabled", "disabled");                            $("#result").html('');                            $.post("ajax.php", {account: account, pwd: pwd, email: email, title: title, body: body, smpt: smpt}, function(data) {                                $("#send").removeAttr("disabled").removeClass("loading").val("发送");                                $("#result").html(data);                            })//                        var preg = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/; //匹配Email//                        if (!preg.test(email.value)) {//                            alert("Email格式错误!");//                            return false;//                        }                        }        </script>    </body></html>

 

后端处理代码:

<?phpfunction sendMail($to_email, $paras) {    $pattern = "/^([0-9A-Za-z\\-_\\.]+)@([0-9a-z]+\\.[a-z]{2,3}(\\.[a-z]{2})?)$/i";    if (!preg_match($pattern, $to_email)) {        return "".$to_email."邮箱格式有误";    }    $from = $paras['from'];    $title = $paras['title'];    $body = $paras['body'];    $smpt = $paras['smpt'];    $account = $paras['account'];    $pwd = $paras['pwd'];    include_once 'phpmailer/class.phpmailer.php';    $mail = new PHPMailer(); //PHPMailer对象    $mail->CharSet = 'UTF-8'; //设定邮件编码,默认ISO-8859-1,如果发中文此项必须设置,否则乱码    $mail->IsSMTP();  // 设定使用SMTP服务    $mail->SMTPDebug = 0;                     // 关闭SMTP调试功能    $mail->SMTPAuth = true;                  // 启用 SMTP 验证功能    $mail->SMTPSecure = '';                 // 使用安全协议    $mail->Host = $smpt;  // SMTP 服务器    $mail->Port = "25";  // SMTP服务器的端口号    $mail->Username = $account;  // SMTP服务器用户名    $mail->Password = $pwd;  // SMTP服务器密码    $mail->Subject = $title; //邮件标题    $mail->SetFrom($account, $from);    $mail->MsgHTML($body);    $mail->AddAddress($to_email, $from);    $result = $mail->Send() ? '200' : $mail->ErrorInfo;    return $result;}    $smpt = $_POST['smpt'];    $account = $_POST['account'];    $pwd = $_POST['pwd'];    $paras = array(        "from" => "我是测试454545454",//发送者        "title" => $_POST['title'],//邮件标题        "body" => $_POST['body'],//邮件内容        "smpt" => $smpt,//smpt服务器        "account" => $account,//账号        "pwd" => $pwd,//密码    );//    $file = fopen('vvvvvvvvasdf.csv','r');//    while ($data = fgetcsv($file)) {        //每次读取CSV里面的一行内容        //print_r($data[0]);        //此为一个数组,要获得每一个数据,访问数组下标即可        // $goods_list[] = $data;       //print_r($goods_list);       /* foreach ($goods_list as $arr){          if ($arr[0]!=""){              echo $arr[0]."<br>";          }        } */    //echo $goods_list[2][0];    //$to_email = $data[0].'@qq.com';    $to_emails = $_POST['email'];    $to_emails = explode(",", $to_email);    $success = 0;    $error = "";    foreach ($to_emails as $v) {        $rs = sendMail($v, $paras);        if ($rs == '200') {            $success ++;        }else{            $error .=$rs;        }    }   // }    echo "共发送<strong style='color:red;font-size:16px'>" . $success . "</strong>邮件<p>".$error."</p>";    ?>

需要的类文件去网上下载一个即可,大致目录结构如下: