send multiple email using phpmailer, i am using Gmail SMTP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • afroz ahmad
    New Member
    • Mar 2013
    • 18

    send multiple email using phpmailer, i am using Gmail SMTP

    i am trying to send multiple mail using phpmailer. I have a problem. When i click send button, just zxc@gmail.com and abc@yahoo.com receives the message.

    How to change it? I'm use Gmail SMTP send out.

    There are 5 records in the database:

    Here is my code:

    Code:
    <?php
    
    $body=$_POST['message'];
    $subject=$_POST['sub'];
    
    //error_reporting(E_ALL);
    error_reporting(E_STRICT);
    
    date_default_timezone_set('America/Toronto');
    
    require_once("class.phpmailer.php");
    
    
    $con=mysql_connect("localhost","root","") or
    die("could not connect:".mysql_error());
    
    mysql_select_db("bulkemail");
    $qry=mysql_query("SELECT * FROM email_id", $con);
    if(!$qry)
    {
    die("Query Failed: ". mysql_error());
    }
    while($row = mysql_fetch_array($qry))
    {
    
    $id= $row['email'];
    
    $mail             = new PHPMailer();
    
    $mail->IsSMTP(); // telling the class to use SMTP
    
    $mail->Host       = "stmp.gmail.com"; // SMTP server
    
    $mail->SMTPDebug  = 1;                     // enables SMTP debug information (for testing)
    
    // 1 = errors and messages
    
    // 2 = messages only
    
    $mail->SMTPAuth   = true;                  // enable SMTP authentication
    
    $mail->SMTPSecure = 'ssl'; 
    
    $mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
    
    $mail->Port       = 465;                   // set the SMTP port for the GMAIL server
    
    $mail->CharSet = "big5";
    
    $mail->Username   = "abc@gmail.com";  // GMAIL username
    
    $mail->Password   = "......";            // GMAIL password
    
    $mail->SetFrom("abc@gmail.com", ''); // set reply id
    
    $mail->AddReplyTo("abc@gmail.com",""); 
    
    $mail->Subject    = ($subject); // subject
    
    $mail->AltBody    = "Hey, check out this new post on www.techaltum.in"; // optional, comment out and test
    
    $mail->MsgHTML("$body"); // message 
    
    $address = ($id);
    
    $mail->AddAddress($address);
    
    
    if(!$mail->Send()) {
      echo "Mailer Error: " . $mail->ErrorInfo;
    } else {
      echo "Message sent!";
    }
    }
    ?>
  • Luuk
    Recognized Expert Top Contributor
    • Mar 2012
    • 1043

    #2
    Line#33 is wrong:
    Code:
    $mail->Host       = "stmp.gmail.com"; // SMTP server
    should it not be:
    Code:
    $mail->Host       = "smtp.gmail.com"; // SMTP server
    ?

    oooh, wait, that is corrected on line #45 ;)

    Comment

    • Luuk
      Recognized Expert Top Contributor
      • Mar 2012
      • 1043

      #3
      What is the output of this script?

      Is it 5 times "Message sent!" ?

      Is there any extra output for line #35 "$mail->SMTPDebug = 1;" ??

      Comment

      • afroz ahmad
        New Member
        • Mar 2013
        • 18

        #4
        There is 5 e-mail id's are in my Database but only 2 e-mail id's receive mail but other one didn't receive mail

        Comment

        • Luuk
          Recognized Expert Top Contributor
          • Mar 2012
          • 1043

          #5
          I just copied your script

          changed all references to 'abc@gmail.com' to my_own_address@ gmail.com

          changed line4 to: $body="test";
          changed line5 to: $subject="test" ;

          change database stuff at line #15 and #18 to match my database

          entered 5 emailaddresses in this table...

          and it worked.....

          I therefore see two options:
          1) your password is wrong (line #35)
          (re-check your password)
          2) emailaddresses in database are wrong.
          maybe change line #27 to check the addresses:
          Code:
          $id= $row['email']; echo $id;

          Comment

          • afroz ahmad
            New Member
            • Mar 2013
            • 18

            #6
            i have check all my code which you suggest but still i am not able to send more than 2 mail

            Comment

            • Luuk
              Recognized Expert Top Contributor
              • Mar 2012
              • 1043

              #7
              Again: What is the output of this script?

              Or, what happens if you put these addreses in your database:
              abc+1@gmail.com
              abc+2@gmail.com
              abc+3@gmail.com
              abc+4@gmail.com
              abc+5@gmail.com

              You should receive those 5 messages in the box of abc@gmail.com

              Comment

              • afroz ahmad
                New Member
                • Mar 2013
                • 18

                #8
                the output is
                abc1@gmail.com Message sent!
                abc2@gmail.com Message sent!
                abc3@gmail.com
                abc4@gmail.com
                abc5@gmail.com

                and only 2 email id receive mail

                Comment

                • Luuk
                  Recognized Expert Top Contributor
                  • Mar 2012
                  • 1043

                  #9
                  hmmzzzzz, why did you leave out the '+' ??

                  But i'm afraid i don't see an answer to your problem.....

                  Comment

                  • afroz ahmad
                    New Member
                    • Mar 2013
                    • 18

                    #10
                    can you send me you coding

                    Comment

                    • Luuk
                      Recognized Expert Top Contributor
                      • Mar 2012
                      • 1043

                      #11
                      it's the same as your code, just different emailaddresses
                      and i downloaded phpmailer from here

                      Comment

                      • afroz ahmad
                        New Member
                        • Mar 2013
                        • 18

                        #12
                        now i can send email to more people but now i am getting new problem when i send mail then first email will go to one person, the second email sent will go to that same person plus another, the third one will go to those two plus one more, and so on.

                        Comment

                        • Rabbit
                          Recognized Expert MVP
                          • Jan 2007
                          • 12517

                          #13
                          That's because on line 67 of your original code, you call AddAddress. That will add an address to the To line, it doesn't replace what's there. You need to call ClearAddresses( ) before you add an address.

                          Comment

                          • Luuk
                            Recognized Expert Top Contributor
                            • Mar 2012
                            • 1043

                            #14
                            @Rabbit:
                            i thought it wen ok, because of the 'new' in line #29:
                            Code:
                            $mail             = new PHPMailer();

                            Comment

                            • Rabbit
                              Recognized Expert MVP
                              • Jan 2007
                              • 12517

                              #15
                              Refer to the documentation's example here: http://phpmailer.worxware.com/index.php?pg=exampledb. Notice their use of ClearAddresses at the bottom. There is no need to keep creating instances and setting values that only need to be set once.

                              Comment

                              Working...