doudou1438 2016-04-26 09:21
浏览 132
已采纳

phpMailer没有添加多个地址

I got an issue with phpMailer and it is not sending to multiple addresses when using an if statement. If I hardcode the emails they work fine...

P.s. I have also checked the variables and the if statements and all are working correctly.

$mail = new PHPMailer;

$mail->setFrom('x@x.com', 'xxx');
$mail->addAddress($row['recipient']); 
if ( $row['recipient2'] != '' ) { $mail->addAddress($row['recipient2']); }
if ( $row['recipient3'] != '' ) { $mail->addAddress($row['recipient3']); }
$mail->addAddress('x@x.com'); 
$mail->addReplyTo('x@x.com', 'xxx');

EDIT - NEW examples - bounty set:

I tried the following script which should give me 7 addresses however I only get one. (The hardcoded one)

$mail = new PHPMailer;

$mail->setFrom($from_email);

$mail->addAddress('test@test.com'); 

if ( $rows != 0 ) {
    for ( $i = 0; $i < $rows; $i ++ ) {
        $row = clean_fetch_assoc($result);
        $mail->addAddress("test2@test.com"); 
    }
}

var_dump($rows);
var_dump($mail->getAllRecipientAddresses());
var_dump($mail->isError());

die();

Which returns...

int 6

array (size=1)
  'test@test.com' => boolean true

boolean false

As you can see $rows returns 6 so there is nothing wrong with the loop.

Any ideas?

  • 写回答

4条回答 默认 最新

  • duanjuduo4573 2016-05-18 02:26
    关注

    There is not enought code shown to exactly and positively nail down your problem.

    But your $row strongly suggests that you're in the middle of a while($row=$stmt->fetch()), right? We don't see anything like it in the code you show.

    I think I have 99% chances to be right on this.
    Your fix is to "extract" your database data BEFORE the PHPMailer part.

    Get all your vars (Sender, From, FromName and an array of adresses... An array of BCC too? ).

    Finish your while fetch loop BEFORE getting in the PHPMailer part, which shall not be included in a loop.

    THEN, you can go on with your email construction.
    Set your Body, AltBody and Subject.
    Now is the time to loop into an address array.
    (And don't call it $row!!).

    By the way, your for loop declaration isn't as correct as you think:
    ( $i=0;$i<$rows;$i++ ) should be ( $i=0;$i<sizeOf($rows);$i++ ).
    Notice the sizeOf()?

    This code that worked perfect for me:
    I suggest you try it in order to get a confimation that PHPMailer isn't the problem.

    // This array would be defined by you fetch loop.
    $moreToAdressArray=["test1@example.com","test2@example.com","test3@example.com","test4@example.com"];
    
    $endpointMsg->AddAddress( $clientLine['email'] );
    for($i=0;$i<sizeOf($moreToAdressArray);$i++){
        $endpointMsg->AddAddress( $row[$i] );
    }
    $endpointMsg->AddBCC ($SENDER);
    

    I've had 5 times the same email with this (adresses were different than ..@example.com - Doh!).
    PHPMailer works like a charm.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵