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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • duanfan9859 2016-05-17 13:10
    关注

    first store your multiple emails like this

    $this->db->select('ID, email, firstName, lastName');
    $this->db->from('contact');
    $this->db->where_in("ID",$Id);
    $query=$this->db->get();
    $res=$query->result_array();
    
    for($i=0; $i<count($res); $i++){
      $toArr[$i]['email']=$res[$i]['email'];
      $toArr[$i]['name']=$res[$i]['firstName'].' '.$res[$i]['lastName'];    
    }
    $row["email"]=$toArr;
    

    then addAddress

    foreach($row as $v){
     $mail->AddAddress($v["email"], $v["name"]);
    }
    
    评论
  • doulin4844 2016-05-23 11:13
    关注

    May this code will help you, plz change that as you need.

    $result = mysqli_query($mysql, 'SELECT full_name, email, photo FROM mailinglist WHERE sent = false');
    
    foreach ($result as $row) { 
        $mail->addAddress($row['email'], $row['full_name']);
    
        if (!$mail->send()) {
            echo "Mailer Error (" . str_replace("@", "&#64;", $row["email"]) . ') ' . $mail->ErrorInfo . '<br />';
            break; //Abandon sending
        } else {
            echo "Message sent to :" . $row['full_name'] . ' (' . str_replace("@", "&#64;", $row['email']) . ')<br />';
            //Mark it as sent in the DB
            );
        }
        // Clear all addresses for next loop
        $mail->clearAddresses();
    }
    
    评论
  • dongyuan8312 2016-05-23 12:26
    关注

    Your trying to send email to same recipients Multiple Times,To avoid this there is condition inside AddAddress to remove same Recipients Kindly comment checkUnique($address) function inside AddAddress function.

    You will find this function inside Class.phpmailer.php file

    function AddAddress($address, $name = "") {
        /*if(!$this->checkUnique($address)){
            return;
        }*/
        $cur = count($this->to);
        $this->to[$cur][0] = trim($address);
        $this->to[$cur][1] = $name;
    }
    
    /** 
     * Added function for unique address Aug 29 Nitin
     *  @param string $address
     * */
    
    
    function checkUnique($address){
        $flag= true;
        for($i=0;$i<count($this->to);$i++){
            if(empty($this->to[$i][0]) ||  $this->to[$i][0]==$address){
                return false;
            }
        }
        for($i=0;$i<count($this->cc);$i++){
            if(empty($this->cc[$i][0]) || $this->cc[$i][0]==$address){
                return false;
            }
        }
        for($i=0;$i<count($this->bcc);$i++){
            if(empty($this->bcc[$i][0]) || $this->bcc[$i][0]==$address){
                return false;
            }
        }
        return $flag;
    }
    
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 关于#abap#的问题,请各位专家解答!
  • ¥20 内网通过公网访问外网问题
  • ¥20 谁有这个东西 继续教育的
  • ¥15 怎么使请求通过cors
  • ¥15 WDM 驱动ACPI 相关疑问
  • ¥15 prism 跨窗体共享数据绑定 wpf
  • ¥15 hdl designer突然用不了系统的moduleware组件,请问有人遇到或者怎么解决吗?
  • ¥15 0基础计算机毕设,应该从哪开始?
  • ¥60 使用DKT40脑图划分ROI区域
  • ¥15 有偿解决C51单片机液晶屏12864显示问题