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 django5安装失败
  • ¥60 ios系统storm sniffer 写入http规则加密如何破解?
  • ¥15 Java与Hbase相关问题
  • ¥15 后缀 crn 游戏文件提取资源
  • ¥15 ANSYS分析简单钎焊问题
  • ¥20 bash代码推送不上去 git fetch origin master #失败了
  • ¥15 LOL外服加入了反作弊系统,现在游戏录像rofl文件离线都无法打开
  • ¥15 在centos7安装conda
  • ¥15 c#调用yolo3 dll文件获取的数据对不上
  • ¥20 WPF 如何实现多语言,label 和cs(live Charts)中是否都能翻译