I have database of 1000 members, which contains email ids of users and had developed code for sending mails to all users in database using single administrator account - the administrator logins were hardcoded in the code.
Now I would like to take it to next level where all the logins of admin would be stored in another database and code would read the login from database and would send mails. eg code would send mails to user#1-50 using admin#1 login and send mails to user#51-100 using admin2 logins and so on.
below is the code which I am trying, and it seems that there is some problem with multiple loops. Can someone help please.
<?php
error_reporting(E_ALL);
//error_reporting(E_STRICT);
ini_set("display_errors", "on");
ini_set('max_execution_time', 300); //300 seconds = 5 minutes
ini_set('memory_limit', '6500M'); //Maximum amount of memory a script may consume (128MB by default)
date_default_timezone_set('UTC');
require_once('../class.phpmailer.php');
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
$mail = new PHPMailer();
$body = file_get_contents("contents.html");
$body = preg_replace('/[\]/','',$body);
$mail->IsSMTP();
$mail->Host = "ssl://smtp.gmail.com";
$mail->SMTPAuth = true;
$mail->SMTPKeepAlive = true;
$mail->Port = 465;
$mail->Subject = "Subject";
$mail->AddReplyTo ('example@domain.com', 'test');
$mail->body = $body;
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!";
$mail->IsHTML(true);
$mail->MsgHTML($body);
$con = mysql_connect("localhost","root","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("test") or die(mysql_error()); //Databasename
$query1 = "SELECT * FROM loginids WHERE Count >= 1"; //count is column name in table loginids
$result1 = mysql_query($query1) or die(mysql_error());
$row1 = mysql_fetch_array ($result1);
$Counter = 1;
$startnum1 = 0;
$endnum1 = 0;
while ($Counter <= 3) {
$mail->Username = $row1["UserName"];
$mail->Password = $row1["Password"];
$mail->From = $row1["UserName"];
$mail->FromName = $row1["FromName"];
$startnum1 = $endnum1;
$endnum1 = $startnum1 + 2;
$query2 = "SELECT EmailID FROM dummy Limit $startnum1,$endnum1";
$result2 = mysql_query($query2) or die(mysql_error());
while ($row2 = mysql_fetch_array ($result2)) {
$mail->AddAddress($row2["EmailID"]);
//$mail->AddStringAttachment($row["EmailID"], "contents.html");
if(!$mail->Send()) {
echo "Mailer Error (" . str_replace("@", "@", $row2["EmailID"]) . ') ' . $mail->ErrorInfo . '<br />';
} else {
echo "Message sent From : " .$row1["UserName"]." Message sent to :" . $row2["EmailID"] . ' (' . str_replace("@", "@", $row2["EmailID"]) . ')<br />';
}
$mail->ClearAddresses();
}
$Counter++;
}
?>
Hi Guys, Thanks for your valuable feedback much appreciated. I am still learning and seems theres lot of thing to do.. I have updated my code (above) accordingly and still not able to send out mails.. I am not sure if I am using the loops correctly.. so heres what I am trying to do in the code.. I would like to send (say) 50 mails using each account.. I have 3 email accounts and $Counter is checking my account number. And startnum1/endnum1 is for no of emails each account should send.
so $startnum1, $endnum1 should also be changed dynamically for each account #.
for e.g.
account#1, $startnum1=1, $endnum1=50
account#2, $startnum1=51, $endnum1=100
and so on..
Now when I am trying this code I am getting "Mailer Error (recipient at domain.com) The following From address failed: sender1 at gmail.com : Called Mail() without being connected"