search
HomeBackend DevelopmentPHP Tutorial用phpmailer如何群发邮件

csdn的版主们不好意思又来麻烦你们了

请问我数据库里面比如有10条email的地址,我如何得到他,然后按钮提交群发出去
就根据昨天我问的帖子
http://bbs.csdn.net/topics/390967296?page=1#post-398769890
库名email,表名bl_email,字段email


回复讨论(解决方案)

简单说一下2个方法,可试试看有问题再讨论

1. 你上一个帖子提到的用2页处理
从 A.php 获取10条email资料 (select email from bl_email where ..... )
然後sumit表单POST到B.php处理寄送动作

2. 用ajax不换页处理
从 A.php 获取10条email资料 (select email from bl_email where ..... )
按下按钮後用ajax post 10条email到後端 B.php处理寄送动作

回复1楼
为何只读取到一条记录
 $conn=mysql_connect("localhost","root","");//连接数据库
 mysql_select_db("email",$conn);//连接哪个库
 mysql_query("set names utf-8");//编码方式 
 $sql="select * from bl_email";//查询那个表
 $result=mysql_query($sql,$conn);
 while($array=mysql_fetch_array($result)){
  $email=$array["address"];
 };
 print_r("$email");                 
?> 

回复1楼
为何只读取到一条记录
 $conn=mysql_connect("localhost","root","");//连接数据库
 mysql_select_db("email",$conn);//连接哪个库
 mysql_query("set names utf-8");//编码方式 
 $sql="select * from bl_email";//查询那个表
 $result=mysql_query($sql,$conn);
 while($array=mysql_fetch_array($result)){
  $email=$array["address"];
 };
 print_r("$email");                 
?> 



用这句看看印出什麽结果
print_r(mysql_fetch_array($result));

难道错误在这句吗
打印的结果
Array ( [0] => 2 [id] => 2 [1] => tom [name] => tom [2] => hzhhzw123@126.com [address] => hzhhzw123@126.com [3] => 2345 [tel] => 2345 ) 

回复3楼
前面没看,4楼的打印是没加while的
加了while的
打印出 mysql_fetch_array(Resource id #4) 

这样看来你提取的数据的确只有一行,你确定数据库内有多笔资料?

回复3楼
问题找到了,打印没有放在while里面

恭喜你..

那怎么送到表单去

我直接把上面取得变量$email加到了这个上面
$mail->AddAddress("$email","h"); //添加收件人
这样可以提交,就是邮件发不过去

我直接把上面取得变量$email加到了这个上面
$mail->AddAddress("$email","h"); //添加收件人
这样可以提交,就是邮件发不过去



$mail->AddAddress("$email","h"); 
改为
foreach($email as $em){
    $mail->AddAddress("$em","h"); 
}

我直接把上面取得变量$email加到了这个上面
$mail->AddAddress("$email","h"); //添加收件人
这样可以提交,就是邮件发不过去



  if(!$mail->Send()) {
      echo "发送失败: " . $mail->ErrorInfo;
  } 

回复11楼和12楼
提示:You must provide at least one recipient email address. 
为什么没有获得地址
程序
           $conn=mysql_connect("localhost","root","");//连接数据库
   mysql_select_db("email",$conn);//连接哪个库
   mysql_query("set names utf-8");//编码方式 
   $sql="select * from bl_email";//查询那个表
   $result=mysql_query($sql,$conn);
   while($array=mysql_fetch_array($result))
          {
      $email=$array["address"];
   }

if(is_array($email))
   {
   foreach($email as $em)
{
   $mail->AddAddress("$em","h");
}
   }

if(!$mail->Send()){
          echo "发送失败: " . $mail->ErrorInfo;  
?>
<script> <br /> alert('无法发送邮件,错误信息:'.$mail->ErrorInfo); <br /> window.location = 'send.php'; <br /> </script>
     } else {
         //echo "邮件已经发送";
?>
<script> <br /> alert('邮件发送成功'); <br /> window.location = 'send.php'; <br /> </script>
     }
?>

if(is_array($email))

如果$email不是array的情?,你?有?。

问题已经解决
在while上面给$email定义一个空的数组
就解决了
前面应该问题是在
打印在while里面可以出来多个值
在外部打印就只出来一个值
然后下面的$mail->AddAddress("$em","h");在按照版主的加个foreach循环
就可以群发了

请教下楼主,你所说的 在while上面给$email定义一个空的数组

是下面这样吗?
我也是新手。。。

 while($array=mysql_fetch_array($result))
          {
      $email[]=$array["address"];
   }

回复16楼
我是这样做的
$email=array();
while($row=mysql_fetch_array($result))
{
    $email[]=$row["address"];
 }

while之前的数组应该不用定义吧?

$email[]会自动创建一个数组。

回复18楼
恩,刚刚实验过了
不加也可以,学习了

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

How to make PHP applications fasterHow to make PHP applications fasterMay 12, 2025 am 12:12 AM

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

PHP Performance Optimization Checklist: Improve Speed NowPHP Performance Optimization Checklist: Improve Speed NowMay 12, 2025 am 12:07 AM

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

PHP Dependency Injection: Improve Code TestabilityPHP Dependency Injection: Improve Code TestabilityMay 12, 2025 am 12:03 AM

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

PHP Performance Optimization: Database Query OptimizationPHP Performance Optimization: Database Query OptimizationMay 12, 2025 am 12:02 AM

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor