周梦康 发表于 2014-06-04 2482 次浏览

这么简单的代码也还是厚着脸皮上传到了github上:https://github.com/zhoumengkang/sendemail 谁要是无聊可以看看。

文件目录结构

├── attachment                //附件存放目录
│   └── attachment.txt        //附件demo
├── config.php                //发送邮箱的账号密码配置,以及邮件群发的内容
├── index.html                //请求该文件开始循环发送
├── mailmodel.php             //邮件处理类
├── phpmailer                 //phpmailer扩展
│   ├── class.phpmailer.php
│   ├── class.pop3.php
│   └── class.smtp.php
└── send.php                  //index.html ajax 请求的文件

用户信息存放在contact的数据库里面的contact表里面。

SET NAMES utf8;
SET foreign_key_checks = 0;
SET time_zone = '+08:00';
SET sql_mode = 'NO_AUTO_VALUE_ON_ZERO';

DROP TABLE IF EXISTS `contact`;
CREATE TABLE `test` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(100) NOT NULL,
  `email` varchar(300) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

通过访问index.html循环ajax请求send.php来发送邮件,这样防止页面卡死。

index.html

<!doctype html>
<html>
<head>
  	<meta charset="utf-8">
	<title></title>
	<script type="text/javascript" src="http://libs.baidu.com/jquery/1.7.2/jquery.min.js"></script>
	<script type="text/javascript">
		$(function(){
			$("#progress").append('<div>页面打开时间'+new Date().toLocaleString()+'</div>');
			var page = 0;
			var send = setInterval(function(){
				$.ajax({
				  type:"POST",
				  url: "send.php",
				  data: {page:page},
				  async: false,
				  dataType: 'json',
				  success:function(data){
				  		//console.log(data);  		
				  		page++;
				  		$("#progress").append('<div><b>'+ new Date().toLocaleString()+'</b></div><div>发送到第'+page+'封:'+data.info+'</div>');
				  		if(data.flag){
				  			clearInterval(send);
				  			$("#progress").append('<p style="color: red;font-weight: 800;">发送完毕</p>');
				  		}
				  }
				});	
			},12000);
		})
	</script>
</head>
<body>
	<div id="progress"></div>
</body>
</html>

send.php

<?php
set_time_limit(0);
require_once(dirname(__FILE__).'/mailmodel.php');

//从数据库取出所有联系人信息
$con = mysql_connect("localhost","root","zmkzmk");
if (!$con){
	die('Could not connect: ' . mysql_error());
}
mysql_select_db('contact'); 
$result = mysql_query("set names 'utf8'");
$totalNumRes =  mysql_query("select count(*) as num from `test`");
while($row = mysql_fetch_assoc($totalNumRes)){
    $totalNum = $row['num'];
}
$page = intval($_POST['page']) ? intval($_POST['page']) : 0;

//这就是最后一封了
if(intval($totalNum) - intval($page) <2){
    $data['flag'] = 1;
}

$num = 1;
$start = 0 + ($num*$page);
$sql = "select `id`,`email`,`name` from `contact` order by id asc limit {$start} , {$num}";
$result = mysql_query($sql); 

//导入配置
$configArr = include dirname(__FILE__).'/config.php';

$mailmodel = new MailModel($configArr['email'],$configArr['password'],$configArr['name']);

while($row = mysql_fetch_assoc($result)){
    $email = trim($row['email']);
    $data['info'] =  '<div> '.$row['name'].' : '.$email.' </div>';
    if($configArr['attachment']){
        $mailmodel->send($email,$row['name'],$configArr['title'],$configArr['body'],$configArr['attachment']);
    }else{
        $mailmodel->send($email,$row['name'],$configArr['title'],$configArr['body']);
    }
    
    echo json_encode($data);
    //数据库里做标记
    //$sql = "INSERT INTO `mark` (`uid`) VALUES (".$row['id'].")";
    //mysql_query($sql);
}

mysql_close($con);


评论列表