前端頁面代碼:
注意:目前發送人使用的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>
后端處理代碼:
<?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>"; ?>
需要的類文件去網上下載一個即可,大致目錄結構如下: