Send mails via SMTP from mysql database problem

Started on the old forum in April 2012 · view the original in the archive · old links redirect here

6 replies · 1,335 views · started 14 years ago · 1 min read

4 participants
cedry2k Junior · 15 posts #1

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:

<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...

do{ beer++ }while(sober); ===//\==\//=== while($lifes_a_bitch == true) { live(); } die;

cedry2k Junior · 15 posts #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
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();
}
?>

do{ beer++ }while(sober); ===//\==\//=== while($lifes_a_bitch == true) { live(); } die;

arnage Lead · 811 posts #4

Maybe its empty field when stops so try:

$query  = "SELECT * FROM adrese WHERE email != ''";
<!DOCTYPE html PUBLIC "-//***WAPFORUM**.**RS***
cedry2k Junior · 15 posts #5

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

do{ beer++ }while(sober); ===//\==\//=== while($lifes_a_bitch == true) { live(); } die;

Mysterio Lead · 630 posts #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

cedry2k Junior · 15 posts #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.....

do{ beer++ }while(sober); ===//\==\//=== while($lifes_a_bitch == true) { live(); } die;

Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close