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条)

报告相同问题?

悬赏问题

  • ¥20 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏