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
Working with Flash Session Data in LaravelWorking with Flash Session Data in LaravelMar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

cURL in PHP: How to Use the PHP cURL Extension in REST APIscURL in PHP: How to Use the PHP cURL Extension in REST APIsMar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

PHP Logging: Best Practices for PHP Log AnalysisPHP Logging: Best Practices for PHP Log AnalysisMar 10, 2025 pm 02:32 PM

PHP logging is essential for monitoring and debugging web applications, as well as capturing critical events, errors, and runtime behavior. It provides valuable insights into system performance, helps identify issues, and supports faster troubleshoot

Simplified HTTP Response Mocking in Laravel TestsSimplified HTTP Response Mocking in Laravel TestsMar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

12 Best PHP Chat Scripts on CodeCanyon12 Best PHP Chat Scripts on CodeCanyonMar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Explain the concept of late static binding in PHP.Explain the concept of late static binding in PHP.Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Customizing/Extending Frameworks: How to add custom functionality.Customizing/Extending Frameworks: How to add custom functionality.Mar 28, 2025 pm 05:12 PM

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft