SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict
    Join Date
    Feb 2003
    Location
    Shropshire
    Posts
    203
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHPMailer help with array and foreach loop

    I have the following code adapted from the help file that comes with PHPMailer and the PHP Anthology but I just can't work out how to pull the necessary information from the db and get it into an array for the foreach loop.

    I have tried using the mailing system on one record (all there is at the moment for testing purposes) and it takes an age to do anything, I then get a confirmation message that the email has been sent but the echo of the $mssg doesn't show the email address and I don't receive the email.

    Can someone please advise as this script has been driving me mad for most of the day. I could also do with some advise as to whether or not it will be acceptable for sending around 700 emails as this is how many the client will initially be sending.

    PHP Code:
    <?php
     
    require_once("../includes/persistentobject.php");
     require_once(
    "../includes/paging.php");
     require_once(
    "../includes/functions.php");
     require_once(
    "../includes/class.phpmailer.php");
     require_once(
    "../includes/class.smtp.php");
     require_once(
    "../includes/subscribers_class.php");
     
     
    $objsubs = new subscribers;
     
     
    // using this to get db connection as I have tried putting below query in a function in my includes class and just can't get it to work at all
     
     
    $sql "select * from subscribers where 1";
             
    $result mysql_query($sql);
             
    $list=mysql_fetch_array($result);
     
     
    //do I need a while loop??
     
         
    $subject $_POST['subject'];
         
         
    $txt "Dear ".$name."\n\n";
         
         
    $txt .= stripslashes($_POST['message'])."\n\n";
     
         
    $txt .= "-------------------------------------------------------\n";
         
    $txt .= "If you no longer wish to receive emails from The Cartoon Gallery, please unsubscribe by clicking on the link below\n";
         
    $txt .= ":: " BASEHREF "unsub.php?email=" $email " ::\n";
         
    $txt .= "-------------------------------------------------------";
     
     
    $mail = new PHPMailer;
     
     
    $mail->IsSMTP();                                      // set mailer to use SMTP
     
    $mail->Host "smtp2.example.com";  // specify main and backup server
     
    $mail->SMTPAuth true;     // turn on SMTP authentication
     
    $mail->Username "example@example.com";  // SMTP username
     
    $mail->Password "password"// SMTP password
     
     
    $mail->From "example@example.com";
     
    $mail->FromName "Myself"
     
     
    $mail->WordWrap 50;                                 // set word wrap to 50 characters
     
    $mail->AddAttachment("/var/tmp/file.tar.gz");         // add attachments
     
    $mail->AddAttachment("/tmp/image.jpg""new.jpg");    // optional name
     
    $mail->IsHTML(true);                                  // set email format to HTML
     
     
    $mail->Subject "News and Special Offers";
     
    $mail->Body    $txt;
     
     foreach(
    $list as $emlist){
        
         
    $recipientEmail $emlist['email'];
         
    $recipientName $emlist['firstname'];
         
    $recipientName .= $emlist['surname'];
         
         
    $mail->AddAddress($recipientEmail$recipientName);
         
         if(!
    $mail->Send())
         {
            
    $messg"Message sending failed to ".$recipientEmail;
            
    $messg .="Error: " $mail->ErrorInfo;
         }
         else {
         
    $messg"Messages have been sent to ".$recipientEmail;
         }
         
    sleep(1);
         
         
    //$mail->clearAddresses();
     
    }
     
    ?>

  2. #2
    is_empty(2); foofoonet's Avatar
    Join Date
    Mar 2006
    Posts
    1,000
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Break this down into biteable testable pieces and try and eliminate things one at a time.

    1st - why arent you getting anything out of the dbase?

    Try a simple mysql calls to the dbase before instantiating objects in include files.

    Get a result?

    Move on to debugging that call to new subscribers.

    Is there any dubugging you can turn on anywhere in those classes?
    Upgrading to Mysql 5? Auto-increment fields now strict
    use NULL
    Or zero or leave the field name out completely.

  3. #3
    SitePoint Addict
    Join Date
    Feb 2003
    Location
    Shropshire
    Posts
    203
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK, I have now got the function working:

    PHP Code:
    function mailing()
        {
            
    $sql mysql_query("SELECT * FROM subscribers");
            while(
    $row mysql_fetch_array($sql))
            {
            echo 
    $row['email'];
            }
        } 
    As in, I now get a result being displayed so I know that I am connecting and receivingthe correct information in the page:

    PHP Code:
    $objsubs = new subscribers;

    $objsubs->mailing(); 
    But this is where I keep coming unstuck as I don't know how to get the results into an array for the foreach loop (hope this makes sense)?? All I know is that I could use the 'while' loop in the code that sends the emails but I don't wnat to be opening and closing connections for all the emails.

  4. #4
    SitePoint Addict
    Join Date
    Feb 2003
    Location
    Shropshire
    Posts
    203
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Anyone?? Please!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •