Send mails via SMTP from mysql database problem

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Send mails via SMTP from mysql database problem

    Hi, this is my first post here...
    I am very happy to be part of this community.

    I am trying to make a script to send mails using a SMTP with a gmail account, the script seems to work...but sends the message only to the first 9 database rows...

    this is the script:

    PHP Code:
    <html>
    <head>
    <title>Gmail SMTP test</title>
    </head>
    <body>

    <?php




    error_reporting
    (E_STRICT);

    date_default_timezone_set('America/Toronto');

    require_once(
    '../class.phpmailer.php');

    //database login
    $host "localhost"//database location
    $user "root"//database username
    $pass ""//database password
    $db_name "test"//database name

    //database connection
    $link mysql_connect($host$user$pass);
    mysql_select_db($db_name);

     
    $adresa mysql_query("SELECT email FROM users");
         
    $query sprintf("SELECT * FROM users");
         
        
    $result mysql_query($query);
         
        
    $count 1;
        while (
    $row mysql_fetch_assoc($result)) {
        


    $mail             = new PHPMailer();

    $body             file_get_contents('contents.html');
    $body             eregi_replace("[\]",'',$body);

    $mail->IsSMTP(); 
    $mail->Host       "smtp.gmail.com"
    $mail->SMTPDebug  1;                     


    $mail->SMTPAuth   true;
    $mail->SMTPSecure "ssl";
    $mail->Host       "smtp.gmail.com";
    $mail->Port       465;
    $mail->Username   "account_username@gmail.com";
    $mail->Password   "account_password";

    $mail->SetFrom($row['email'], 'My_Name');

    $mail->AddReplyTo($row['email'], $row['email']);

    $mail->Subject    " Subject message!";

    $mail->AltBody    "To view the message, please use an HTML compatible email viewer!"// optional, comment out and test

    $mail->MsgHTML($body);

    $address $row['email'];
    $mail->AddAddress($address"name");


    if(!
    $mail->Send()) {
      echo 
    "Mailer Error: " $mail->ErrorInfo;
    } else {
      echo 
    "Message sent!";
    }
    }
    ?>

    </body>
    </html>

    I don't know where I am wrong....
    I want this script to send emails to 100 adresses, gmails limit.

    Thank you in advance and...I'm sorry for my english because is not that good...

    #2
    SELECT * FROM users LIMIT 100

    Comment


      #3
      tryed...doesn't work, the script just stop working after 9 mails ...
      -----
      I worked a little bit on this, but still doesn't work properly....now sends 13 mail and stop

      PHP Code:
      <?php
      require("class.phpmailer.php"); //this is downloaded

      $mail = new phpmailer();

      $mail->From     "site_email@gmail.com"//my email adress
      $mail->FromName "site name"//name used in mail
      $mail->Mailer   "smtp";

      $mail->IsSMTP();
      //$mail->Host     = "smtp.gmail.com"; //smtp server
      $mail->SMTPDebug  1
                                                 
      // 1 = errors and messages
                                                 // 2 = messages only
      $mail->SMTPAuth   true;                  // enable SMTP authentication
      $mail->SMTPSecure "ssl";                 // sets the prefix to the servier
      $mail->Host       "smtp.gmail.com";      // sets GMAIL as the SMTP server
      $mail->Port       465;                   // set the SMTP port for the GMAIL server
      $mail->Username   "site_email@gmail.com";  // GMAIL username
      $mail->Password   "email_password";            // GMAIL password

      @MYSQL_CONNECT("host_name","host_user","host_password");
      @
      mysql_select_db("database_name");
      $query  "SELECT id, nume, email FROM adrese";
      $result = @MYSQL_QUERY($query);

      while (
      $row mysql_fetch_array ($result))
      {
          
      // HTML body
          
      $body  "hello <font size=\"4\">" $row["nume"] . "</font>, <p>";
          
      $body .= "my message<p>";
          
      $body .= "message end! <br>";
          
      $body .= "last row from email message";

          
      // Plain text body (for mail clients that cannot read HTML)
          
      $text_body  "hello" $row["nume"] . ", \n\n";
          
      $text_body .= "my message\n";
          
      $text_body .= "message end! \n";
          
      $text_body .= "last row from email message";

          
      $mail->Body    $body;
          
      $mail->AltBody $text_body;
          
      $mail->AddAddress($row["email"], $row["nume"]);
        
      //  $mail->AddStringAttachment($row["photo"], "YourPhoto.jpg");

          
      if(!$mail->Send())
              echo 
      "There has been a mail error sending to " $row["email"] . "<br>";
          else
              echo 
      "" $row["id"] . "The message to adress " $row["email"] . " was sent<br>";
              

          
      // Clear all addresses and attachments for next loop
          
      $mail->ClearAddresses();
          
      $mail->ClearAttachments();
      }
      ?>
      Last edited by cedry2k; 24.04.12, 16:14. Reason: script modified

      Comment


        #4
        Maybe its empty field when stops so try:
        PHP Code:
        $query  "SELECT * FROM adrese WHERE email != ''"
        <!DOCTYPE html PUBLIC "-//WAPFORUM.RS

        Comment


          #5
          there are no empty fields...i am testing with a small database...aboute 50 rows, added manually just for tests

          Comment


            #6
            Just an idea: what if you add the receivers as "BCC"?
            Maybe SMTP server is not responding after you send some first emails (9 or 13 like in your case).

            My mailing list, for example, puts all receivers in BCC, then after sending first 50 emails, waits a bit, and sends others.
            mysterio.al - programming is a functional art

            Comment


              #7
              Doesn't work,sends all the messages to the first row in database (my email)...
              Today I will test the script on a webhost to see the difference between localhost and webhost ...maybe my localhost it's not configured properly.....

              Comment

              Working...
              X