php 邮件群发(phpmailer发送qq邮件)

 更新时间:2016年11月25日 17:02  点击:1505
邮件群发其实非常的简单只要利用遍历发布就可以了,下面本例子利用了phpmailer函数来登录163邮箱给QQ邮箱发邮件,希望本文章对各位同学有帮助。

*@author ray
*@since 2009-08-07

smtp邮件帐号一定要多,不然会被qq服务器当作垃圾邮件。
2.得适当的休眠

smtp.dat文件格式为
smtp.163.com|sss@163.com|密码
smtp.sina.com|www@sina.com|密码 www.111cn.net
程序随机抽取一个连接,发送邮件,如果发送不成功,将该邮件地址存入sleepmail.dat休眠30分后在发送(这个是为了连接smtp服务器多次后发送不成功而做的修改)。
*/

 代码如下 复制代码
define('__debug__', false);
define('__ps教程w_file__', dirname(__file__) . '/smtp.dat');
define('sleeping_email', dirname(__file__) . "/sleepmail.dat");//休眠的email
define('sleeping_time', 1800);//休眠多长时间,以秒为单位
define('file_append', 1);
if (!function_exists('file_put_contents')) {
    function file_put_contents($n, $d, $flag = false) {
        $mode = ($flag == file_append || strtoupper($flag) == 'file_append') ? 'a' : 'w';
        $f = @fopen($n, $mode);
        if ($f === false) {
            return 0;
        } else {
            if (is_array($d)) $d = implode($d);
            $byteswritten = fwrite($f, $d);
            fclose($f);
            return $byteswritten;
        }
    }
}
$errorno = 0;
$errormsg = '';
$currtime = time();
$unusemails = array();
//收件人和邮件标题和邮件内容
$to = isset($argv[1]) ? $argv[1] : "" ;
$subject = isset($argv[2]) ? $argv[2] : "";
$mailfile = isset($argv[3]) ? $argv[3] : "" ;
if (__debug__) {
    echo "
file:$mailfile to:$to subject:$subject ";
}
if (empty($mailfile) || empty($to) || empty($subject)) {
    $errorno = 1;
    $errormsg = "参数不全";
}
//加载不可用的email列表
if (!$errorno) {
    if (file_exists(sleeping_email)) {
        $sleepmails = file(sleeping_email);
        if (!empty($sleepmails)) {
       
            foreach($sleepmails as $sleepmail) {
                //解析
                if (false !== strpos($sleepmail, '|')) {
                    $tmp = explode('|', $sleepmail);
                    if (isset($tmp[0]) && isset($tmp[1])) {
                        $mail = trim($tmp[0]);
                        $time = trim($tmp[1]);
                       
                        //是否可用
                        if ( ($currtime - $time )< sleeping_time) {
                            $unusemails[] = $mail;
                        }
                    }
                }
            }
        }
    }
}
if (!$errorno) {
    //随机加载smtp服务器和smtp用户名和密码
    $info = file(__psw_file__);
    $len = count($info);
   
    do {
        $rnd = mt_rand(0, $len - 1);
        $line = isset($info[$rnd]) ? $info[$rnd] : "";
       
        if (false !== strpos($line, '|')) {
       
            $tmp = explode('|', $line);
            if (isset($tmp[0]) && isset($tmp[1]) && isset($tmp[2])) {
               
                $smtpserver = trim($tmp[0]);
                $frommail = trim($tmp[1]);
                $psw = trim($tmp[2]);
                $smtpusername = substr($frommail, 0, strrpos($frommail, '@'));
            }
        }
    }while (in_array($frommail, $unusemails));//如果在不可用的列表中,在次加载
   
    if (!isset($smtpserver) || !isset($frommail) || !isset($psw)) {
        $errorno = 2;
        $errormsg = "没找到发件人qq信箱和密码";
    }
}
if (!$errorno && __debug__) {
    echo "smtp:$smtpserver from:$frommail psw:$psw user:$smtpusername ";
}
if (!$errorno) {
    //通过phpmailer连接smtp服务器发信
    require(dirname(__file__) . "/phpmailer/class.phpmailer.php");
    require(dirname(__file__) . "/phpmailer/class.smtp.php");
    $mail = new phpmailer();
   
    $body = $mail->getfile($mailfile);
    $body = eregi_replace("[]",'',$body);
   
    //charset
    $mail->charset = "gb2312";
   
    //$mail->smtpdebug = 2;//www.111cn.net用于显示具体的smtp错误
   
    $mail->issmtp();
    $mail->smtpauth = true;
    if ("smtp.qq.com" == trim($smtpserver)) {
        $mail->username = $frommail;
    } else {
        $mail->username = $smtpusername;
    }
    $mail->password = $psw;
    $mail->host = $smtpserver;
   
    $mail->from = $frommail;
    $mail->fromname = "晴天网络";
   
    $mail->ishtml(true);
   
    $mail->addaddress($to);
    $mail->subject = $subject;
    $mail->body = $body;
   
    if (!$mail->send()) {
   
       // echo "message could not be sent. ";
        $errorno = 3;
        $errormsg = $mail->errorinfo;
    } else {
        echo "
send to $to success use $frommail ";
        exit;
    }
}
if (3 == $errorno) {
    //记录信息,该信息地址休眠n分钟
    $content = "$frommail|" . time() . " ";//email|当前时间戳
    file_put_contents(sleeping_email, $content, file_append);
}
echo "
error no($errorno) " . $errormsg . " ";
exit;
本文章不光是是一款利用php发送邮件类代码与是一款实用的php邮件发送代码,如果你是初学者,可以拿当作学习同时也可以使用,如果是你开发者,这款邮件类完全可以使用哦。
 代码如下 复制代码

<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312" />
<title>使用php发送电子邮件</title>
<style type="text/css教程">
<!--
.style1 {font-size: 12px}
.style2 {
 font-size: 24px;
 font-weight: bold;
}
-->
</style>
</head>

<body>
<p align="center" class="style2">使用php发电子邮件</p>
<form name="form1" method="post" action="send_mail.php">
<table width="444" height="347" border="0" align="center">
    <tr>
        <td width="71" height="23" bgcolor="#d6b1e9">
        <div align="right" class="style1">
            <div align="left">&nbsp;收件人</div>
        </div>
        </td>
        <td width="363">
            <label>&nbsp;<input type="text" name="sendto"></label>
        </td>
    </tr>
    <tr>
        <td height="27" bgcolor="#d6b1e9">
        <div align="right" class="style1">
            <div align="left">&nbsp;邮件标题</div>
        </div></td>
        <td>
            <label>&nbsp;<input type="text" name="subject"></label>
        </td>
    </tr>
    <tr>
        <td height="23" colspan="2" bgcolor="#d6b1e9">
        <div align="right" class="style1">
            <div align="left">&nbsp;邮件正文</div>
        </div>
        </td>
    </tr>
    <tr>
        <td colspan="2" bgcolor="#d6b1e9">
        <div align="right">www.111cn.net
            <label>
            <div align="left">
                <textarea name="emailcontent" cols="60" rows="18"></textarea>
            </div>
            </label>
        </div>
          <div align="right"></div>
        <div align="right"></div>
        </td>
    </tr>
    <tr>
        <td colspan="2">
            <label><input type="submit" name="submit" value="提交"></label>
        </td>
    </tr>
</table>
</form>

</body>
</html>

 

-- php教程myadmin sql dump
-- version 2.11.6
-- http://www.phpmyadmin.net
--
-- host: localhost
-- generation time: jun 29, 2010 at 09:27 am
-- server version: 5.0.51
-- php version: 5.2.6

set sql_mode="no_auto_value_on_zero";


/*!40101 set @old_character_set_client=@@character_set_client */;
/*!40101 set @old_character_set_results=@@character_set_results */;
/*!40101 set @old_collation_connection=@@collation_connection */;
/*!40101 set names utf8 */;

--
-- database: `test`
--

-- --------------------------------------------------------

--
-- table structure for table `maillist`
--

create table if not exists `maillist` (
  `id` int(11) not null auto_increment,
  `title` text collate utf8_unicode_ci not null,
  `content` mediumtext collate utf8_unicode_ci not null,
  `senduser` varchar(50) collate utf8_unicode_ci not null,
  `sendmail` varchar(200) collate utf8_unicode_ci not null,
  `sendtime` int(11) not null,
  `accept_email` varchar(200) collate utf8_unicode_ci not null,
  `cc_email` varchar(200) collate utf8_unicode_ci not null,
  `bcc_email` varchar(200) collate utf8_unicode_ci not null,
  `msessage_id` varchar(200) collate utf8_unicode_ci not null,
  `msgno` int(11) not null comment '邮件的序号,为0表示已经下载结束',
  `is_download` tinyint(4) not null default '0',
  primary key  (`id`)
) engine=myisam default charset=utf8 collate=utf8_unicode_ci auto_increment=1 ;

--
-- dumping data for table `maillist`
--


-- --------------------------------------------------------

--
-- table structure for table `mail_account`
--

create table if not exists `mail_account` (
  `id` int(11) not null auto_increment,
  `username` varchar(100) not null,
  `password` varchar(50) not null,
  `emailaddress` varchar(100) not null,
  `mailserver` varchar(50) not null,
  `servertype` varchar(10) not null,
  `port` varchar(10) not null,
  primary key  (`id`)
) engine=myisam default charset=latin1 auto_increment=1 ;

--
-- dumping data for table `mail_account`
--


-- --------------------------------------------------------

--
-- table structure for table `mail_attach`
--

create table if not exists `mail_attach` (
  `id` int(11) not null auto_increment,
  `mail_id` int(11) not null,
  `filename` varchar(200) collate utf8_unicode_ci not null,
  `filename_tmp` varchar(200) collate utf8_unicode_ci not null,
  `download_time` datetime not null,
  primary key  (`id`)
) engine=myisam default charset=utf8 collate=utf8_unicode_ci auto_increment=1 ;

--
-- dumping data for table `mail_attach`
--

这是一款免费的php发送邮件代码-用mail哦,是基础php自身的上传函数

 代码如下 复制代码

<html>
<head>
<title>发信给网管</title>
</head>
<body>
<h2 align="center">网管收信</h2><br>
<hr><br>
<center>
<form action="mail.php">
发件人:<input type="text" name="from" size=25><br>
主题:<input type="text" name="subject" size=20><br>
内容:
<textarea name="content" cols=80 rows=15>你好,站长:</textarea><br>
<input type="submit" value="寄出"> <input type="reset" value="重写"><br>
</form>
</body></html>

php代码

 代码如下 复制代码

<?php
if (empty($from) or empty($subject) or empty($content)) {
 echo "没有完成填写,请<a href='mail.html'>返回</a>";
}
$body="[主题] $subjectn";
$body.="[发件人] $fromn";
$body.=$content;
$deal=mail("guoanyuan@eyou.com",$subject,$body,"From:$from");
if ($deal) {echo "寄件成功!";}else{echo "寄件失败www.111cn.net!!!";}
?>

下面这些发送电子邮件代码,用了一个imap与php自带的mail函数来实例,非常方法的,还有一种就是第三方插件哦,phpmailer来发送邮件。
 代码如下 复制代码

<?php
//连接 IMAP 服务器链接,IMAP 的端口为 143。
$mbox = imap_open("{localhost:143}INBOX","user_id","password");
//连接POP3 服务器链接,POP3 的端口为 110。
$mbox = imap_open("{localhost/pop3:110}INBOX","user_id","password");
//连接NNTP 服务器链接,NNTP 的端口为 119。
$nntp = imap_open("{localhost/nntp:119}comp.test","","");
?>

<?php
//连接IMAP服务器
$mbox = imap_open("{imap.example.org}", "username", "password", OP_HALFOPEN)
      or die("连接失败: " . imap_last_error());
$list = imap_getmailboxes($mbox, "{imap.example.org}", "*");
if (is_array($list)) {
    foreach ($list as $key => $val) {
        echo "($key) ";
        echo imap_utf7_decode($val->name) . ",";
        echo "'" . $val->delimiter . "',";
        echo $val->attributes . "<br /> ";
    }
} else {
    echo "imap_getmailboxes 失败: " . imap_last_error() . " ";
}
//关闭imap连接
imap_close($mbox);
?>

 

[!--infotagslink--]

相关文章

  • NodeJS实现阿里大鱼短信通知发送

    本文给大家介绍的是nodejs实现使用阿里大鱼短信API发送消息的方法和代码,有需要的小伙伴可以参考下。...2016-01-20
  • PHP测试成功的邮件发送案例

    mail()函数的作用:连接到邮件服务器,利用smtp协议,与该服务器交互并投邮件。注意:1、mail函数不支持esmtp协议,---即,只能直投,不能登陆2、由上条,我们只能直投至最终的收件服务器地址.而该地址,又是在PHP.ini中指定的,所...2015-10-30
  • php邮件发送的两种方式

    这篇文章研究的主要内容就是使用PHP来发送电子邮件,总结为以下两种方法:一、使用PHP内置的mail()函数<&#63;php $to = "test@163.com"; //收件人 $subject = "Test"; //主题 $message = "This is a test mail!"; //正文...2015-10-30
  • c# 实现发送邮件的功能

    这篇文章主要介绍了c# 如何实现发送邮件的功能,文中示例代码非常详细,帮助大家更好的理解和学习,感兴趣的朋友可以了解下...2020-07-07
  • php邮件发送的两种方式

    这篇文章研究的主要内容就是使用PHP来发送电子邮件,总结为以下两种方法:一、使用PHP内置的mail()函数<&#63;php $to = "test@163.com"; //收件人 $subject = "Test"; //主题 $message = "This is a test mail!"; //正文...2015-10-30
  • python实现企业微信定时发送文本消息的实例代码

    这篇文章主要介绍了python实现企业微信定时发送文本消息的实例代码,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2020-11-25
  • c#使用netmail方式发送邮件示例

    这篇文章主要介绍了c#使用netmail方式发送邮件的示例,大家参考使用吧...2020-06-25
  • PHP测试成功的邮件发送案例

    mail()函数的作用:连接到邮件服务器,利用smtp协议,与该服务器交互并投邮件。注意:1、mail函数不支持esmtp协议,---即,只能直投,不能登陆2、由上条,我们只能直投至最终的收件服务器地址.而该地址,又是在PHP.ini中指定的,所...2015-10-30
  • PHPMailer在SAE上无法发送邮件的解决方法

    PHPMailer在SAE上无法发送邮件怎么回事呢,我们以前在php5.2.7版本中使用了PHPMailer是可以发,但移到sae中发现无法发邮件了,那么此问题如何解决 在SAE上直接用5.2.7...2016-11-25
  • 整理几个android后台发送邮件的方法

    本文我们整理了三个android后台发送邮件的方法及示例,第一个是不借助Intent在android后台发送Email,第二个是用在收集应用的异常信息,第三个是分享一个android后台发送邮...2016-09-20
  • Perl中使用MIME::Lite发送邮件实例

    这篇文章主要介绍了Perl中使用MIME::Lite发送邮件实例,本文介绍了使用sendmail方式发送、发送HTML格式邮件、smtp方式发送邮件等内容,需要的朋友可以参考下...2020-06-29
  • 网上找到的两个PHP发送邮件的例子,很不错,贴出来给初学者参考吧(不知道是否有兄弟曾贴过),呵呵(2

    Advanced Example Here we will show the full capabilities of the PHP mail function. PHP Code: <?php echo "<html><body>"; $recipient = "Kris Arndt <karn@nu...2016-11-25
  • Delphi7中群发Email邮件的方法

    这篇文章主要介绍了Delphi7中群发Email邮件的方法,涉及邮件服务器软件的使用,电子邮件的判断与发送功能的实现,是非常实用的技巧,需要的朋友可以参考下...2020-06-30
  • node.js 基于 STMP 协议和 EWS 协议发送邮件

    这篇文章主要介绍了node.js 基于 STMP 协议和 EWS 协议发送邮件的示例,帮助大家更好的理解和使用node.js,感兴趣的朋友可以了解下...2021-02-15
  • PHP利用Jmail组件实现发送邮件

    学过asp的朋友可能知道jmail组件是使用在asp中一个常用的邮箱发送功能,在php中如果想调用jmail功能我们需要使用com组件来操作。 我们先来介绍格式 代码如...2016-11-25
  • phpMailer 发送邮件

    //原创:www.111cn.net 注明:转载说明来处www.111cn.net // 昨天听一网友说用php 里面的mail发邮件发不出去,我想一般都是发不了的,现在大多数据邮件提供商都不准那样了...2016-11-25
  • C#编程实现发送邮件的方法(可添加附件)

    这篇文章主要介绍了C#编程实现发送邮件的方法,具备添加附件的功能,涉及C#文件传输及邮件发送的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25
  • php中利用curl smtp发送邮件实例

    本文章来介绍人一下关于与我们不同的发送邮件的方法我们来利用php curl stmp来实现邮件的发送程序。 $ telnet 邮箱SMTP服务地址 25 Trying 邮箱服务IP地址......2016-11-25
  • Python基于httpx模块实现发送请求

    这篇文章主要介绍了Python基于httpx模块实现发送请求,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-07-08
  • php定时发送邮件

    <?php // 请求 PHPmailer类 文件 require_once("class.phpmailer.php"); //发送Email函数 function smtp_mail ( $sendto_email, $subject, $body, $extra_hd...2016-11-25