Question

I am trying to debug a newsletter mailing script for a project I am working on. This used to work perfectly fine with PHP Mailer, however I ended up changing my mailer to Swift Mailer and since I have been having the most bizarre problem.

I keep getting the following warning, after the first iteration of the while loop (one e-mail is sent out to the first e-mail address in the mysql result set, then I get this warning):

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, integer given in /home1/username/public_html/domain/newslettermailer.php on line 15

This is my newlettrmailer.php script:

   include('inc/connect.inc.php');

    $query = "SELECT * FROM `newsletter`";

    $result = mysqli_query($mysqli, $query);

    $status = Array();

    require('mailer/swift_required.php');


    while($row = mysqli_fetch_array($result)){
        $email =  $row['email'];

        echo $email . " GOOD<br/>";

      // Create the Transport
      $transport = Swift_SmtpTransport::newInstance('mail.domain.com', 25)
      ->setUsername('support@domain.com')
      ->setPassword('password');

      // Create the Mailer using your created Transport
        $mailer = Swift_Mailer::newInstance($transport);

        $mailTo = Array();
        $mailTo[] = $email;  


    $message = Swift_Message::newInstance('Newsletter Test!')
      ->setFrom(array('support@domain.com' => 'Subject'))
      ->setTo($mailTo);

    $body = '<strong style="text-decoration:underline;">Test</strong>';
      $message->setBody($body,'text/html');


          // Send the message
        $result = $mailer->send($message);



        if($result==0)
        { //MESSAGE FAILED
            $status .= $email . ': <span style="color:red; font-weight:bold;">Fail</span><br/>';
        }
        else
        { //SUCCESS!
            $status .= $email . ': <span style="color:green; font-weight:bold;">Success!</span><br/>';
        }

    }

While trying to debug, I modified the script above to the following, and got not errors and no warnings what so ever, and all e-mails printed out:

         include('inc/connect.inc.php');

        $query = "SELECT * FROM `newsletter`";

        $result = mysqli_query($mysqli, $query);

        $status = Array();

        require('mailer/swift_required.php');


        while($row = mysqli_fetch_array($result)){
            echo $row['email'] . '<br/>';

        }

I have been trying to debug this for quite a while now and am really having a difficult time with this, I appreciate any suggestions as to why this is happening.

Many thanks in advance!

Was it helpful?

Solution

You're reusing the variable $result:

$result = $mailer->send($message);

So on the second iteration of the while loop, it's trying to pass that to mysqli_fetch_array() instead of the result return from mysqli_query().

Use a different variable for this second result.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top