利用PHP+phpmailer开发包,实现批量发送邮件功能教程Php

印迹发布于:2019-4-2 1437

开发程序时时常会用到邮件提醒大家可以用以下方法结合phpmailer包,轻松开发邮件发送提醒功能,以下结合网络上网友提供的方案,供大家使用。

发送人可以使用网易 新浪 163等其他邮箱,本例需要用到phpmailer包:

下载地址:https://sourceforge.net/projects/phpmailer/ 
前端页面代码:

发送到多个邮箱用“,”隔开。例:  561721@qq.com,654321@qq.com,12345@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="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="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>
后端处理代码:
<?php
function 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>";
    ?>

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


http://www.virplus.com/thread-247.htm
转载请注明:2019-4-2 于 VirPlus 发表

推荐阅读
最新回复 (0)

    ( 登录 ) 后,可以发表评论!

    返回