I have a email script that runs every 15 minutes and is supposed to send a email once using PHP mailer. For some reason, it's sending out 3 emails a time.
Here's my code:
<?php
// Database connect
include("class.phpmailer.php");
$sql2 = "SELECT * FROM eblast_email WHERE id = '1'";
$result2 = mysql_query($sql2);
while ($myrow2 = mysql_fetch_array($result2)){
$get_event_id = "".$myrow2['event_id']."";
$mail = new PHPMailer();
// Login information here
$mail->Subject = "Subject here";
$html.= "HTML Message here";
$plain = "Plain Message here";
$mail->Body = $html;
$mail->AltBody = $plain;
$sql = "SELECT * FROM email_users WHERE sent = 'no' LIMIT 0, 40";
$result = mysql_query($sql);
while ($myrow = mysql_fetch_array($result)){
$email_to_send_to = "".$myrow['email']."";
$rsvp_check = mysql_query("SELECT * FROM event_members WHERE event_attending='$get_event_id' AND email='$email_to_send_to'");
$rsvp_check_done = mysql_num_rows($rsvp_check);
if ($rsvp_check_done == 0) {
$mail->AddAddress($email_to_send_to);
if(!$mail->Send()) {
echo "<b>Error sending email to " . $myrow['email'] . ". </b>" . $mail->ErrorInfo;
echo "<br>";
} else {
echo "Message to " . $myrow['email'] . " has been sent.<br>";
}
mysql_query("UPDATE email_users SET sent='yes' WHERE email='".$myrow['email']."'") or die (mysql_error());
$mail->ClearAddresses();
} else {
mysql_query("UPDATE email_users SET sent='yes' WHERE email='$email_to_send_to'") or die (mysql_error());
echo "$email_to_send_to has already registered ($rsvp_check_done) -- $get_event_id && $email_to_send_to<br>";
}
sleep(2);
}
}
echo "<br>Done.";
?>
Modify the code to have write to a log file every time it runs. My guess is that it's just getting called 3 times.
Example:
file_put_contents("log.txt", $_SERVER['REQUEST_TIME'] . "\n", FILE_APPEND);
Related
I have a table in the db that queues and stores all the mails that are sent through a script that inserts data into this table.
Another script in php manages the sending, given the high number of emails, to avoid spamming it sends them in groups of 50.
it repeats the loop until all the tuples have been sent.
(there is a column for each tuple with queued or sent set)
The problem is the following:
Given, precisely, the high number of submissions, at a certain point the script crashes (chrome) with the error "ERR_TOO_MANY_REDIRECTS".
How can i resolve?
<?php
require_once '../dbh.inc.php';
include_once '../functions.php';
error_reporting(E_STRICT | E_ALL);
date_default_timezone_set('Etc/UTC');
require '../PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail-> isSMTP();
$mail->Host = 'smtps.*****.it';
$mail->SMTPDebug = 0;
$mail->Port = 465;
$mail->SMTPAuth = true;
$mail->SMTPAutoTLS = false;
$mail->SMTPSecure = 'ssl';
$mail->Username = 'intranet#*****.it';
$mail->Password = '*****';
$mail->Priority = '1';
$mail->setFrom('intranet#*****.it', '*****');
$process_count = 50;
$sql = "SELECT * FROM `email_queue` WHERE `status` = 'queued' AND `do_not_send_before` < '" .strtotime("now") ."'
ORDER BY `submission_date`, `priority` ASC LIMIT $process_count ";
$result = mysqli_query($conn, $sql);
foreach ($result as $row) {
$mail->addAddress($row['recipient']);
$mail->addReplyTo($row['reply_to']);
$mail->IsHTML(true);
$mail->CharSet = "UTF-8";
$mail->Subject = $row['subject'];
$mail->msgHTML($row['message']);
$mail->AltBody = "";
$file1 = $row['file1'];
$file2 = $row['file2'];
$file3 = $row['file3'];
$file4 = $row['file4'];
$file5 = $row['file5'];
$filepath = '/web/htdocs/www.*****.it/home/it/intranet/uploads/mails/';
if (!empty($file1)) {
$file1 = $filepath.$file1;
$mail->AddAttachment($file1);
}
if (!empty($file2)) {
$file2 = $filepath.$file2;
$mail->AddAttachment($file2);
}
if (!empty($file3)) {
$file3 = $filepath.$file3;
$mail->AddAttachment($file3);
}
if (!empty($file4)) {
$file4 = $filepath.$file4;
$mail->AddAttachment($file4);
}
if (!empty($file5)) {
$file5 = $filepath.$file5;
$mail->AddAttachment($file5);
}
if (!$mail->send()) {
echo "Mailer Error (" . str_replace("#", "#", $row["recipient"]) . ') ' . $mail->ErrorInfo . '<br />';
break; //Abandon sending
} else {
$now = strtotime("now");
//echo "Messaggio inviato a :" . ' (' . str_replace("#", "#", $row['recipient']) . ')<br />';
//Mark it as sent in the DB
$sql1 = "UPDATE `email_queue` SET `status` = 'sent', `sent_date` = '$now' WHERE `id` = '" . $row['id'] . "'";
$result = mysqli_query($conn, $sql1);
if ($result) {
echo "Mail Inviata correttamente a: (" . str_replace("#", "#", $row["recipient"]) . ') <br />';
} else {
echo "error $sql1 </br>";
}
}
// Clear all addresses and attachments for next loop
$mail->clearAddresses();
$mail->clearAttachments();
}
$sql2 = "SELECT COUNT(*) AS count FROM `email_queue` WHERE `status` = 'queued';";
$result2 = mysqli_query($conn, $sql2);
$row2 = mysqli_fetch_assoc($result2);
$count = $row2['count'];
echo "$count";
if ($count > 0) {
header('Location: '.$_SERVER['PHP_SELF']);
} else {
header('Location:' . $USER_LOCATION . "index.php?emails=success");
}
?>
At the end of script, you have else-if condition which always results in a redirect. If the count of rows is bigger than 0, you redirect it to the same script, without end. It results in a never-ending loop of redirects.
Just do not redirect in this case, or redirect to another script that won't redirect to another (or the same) script again.
pseudo code:
if ($count > 0) {
// do something here instead of redirect
} else {
header('Location:' . $USER_LOCATION . "index.php?emails=success");
}
header('Location: '.$_SERVER['PHP_SELF']); always redirects to same URL and there server sees infinite redirect, that's why TOO_MANY_REDIRECTS.
Do not use re-directs here. Use loop.
do {
$count = $this->getCount();
if ($count > 0) {
$this->processEmails();
}
} while ($count > 0);
Note that this process may be executed longer than 30s and it would time out your ordinary browser request. Use it as CLI command
i need to send an email with the below query but cannot get it to send? what am i doing wrong? i have removed the connect details. i just get a HTTP ERROR 500
require "defaultincludes.inc";
$to_email = "antham1616#gmail.com";
$subject = "Tomorrow Jobs";
$body = "$sql =
Select * From mrbs_entry Where from_unixtime(start_time, '%Y-%m-%d') = CURDATE() + INTERVAL 1 DAY AND driver = 'Kim Emery' ORDER by start_time asc";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<br> <strong>Bus:</strong> ".$row["room_id"]. " Driver: ".$row["driver"]. " Trip: ".$row["name"] . " Fee: ".$row["fee"] . " <tr>";
{echo "<br> Time: " . $row["pickup_time"]. " Name: ". $row["pickup_address"]. " Going: ". $row["pickup_1_going"]. "<br>";}
}
} else {
echo "0 results";
}
echo "<img src='images/bus checks.jpg' alt='checks' />";
$headers = "From: office#ndct.co.uk";
if ( mail($to_email, $subject, $body, $headers)) {
echo("Email successfully sent to $to_email...");
} else {
echo("Email sending failed...");
}
?>```
There's probably a syntax error somewhere in your code. Have a look in the error log.
This like looks weird:
$body = "$sql =
i have wrote a php script to send an emails.In this script it checks whether current date is equal with the emailfly_date and email is = o.if that satisfies email will sent to the user.this script is working for one user at a one time.that if database has five records that that matches current criteria it sends the email only to the last id.what is the reason for this?
id
250
251
252
253
it will send the email only to the ID 253.but not to the others.
Here is mycode
<?php
include_once 'dbconnect.php';
$query = "SELECT ID,email, emailfly_date, CURRENT_DATE AS nowtime FROM xxx WHERE reqnum = '' AND email_sent = '0'";
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
$regid = $row['ID'];
$activemail= $row['nowtime'];
/*echo $regid . '<br />';
echo $activemail. '<br />';*/
}
$query = mysql_query("SELECT ID, email, reqnum, emailfly_date, email_sent FROM xxx WHERE ID = '$regid' AND emailfly_date = '$activemail'");
$rowdata = mysql_fetch_array($query);
$ID = $rowdata['ID'];
$email = $rowdata['email'];
$reqnum = $rowdata['reqnum'];
$emailfly_date = $rowdata['emailfly_date'];
$email_sent = $rowdata['email_sent'];
if ($reqnum != '') {
/* echo "not sucess";*/
} elseif ($email_sent == '1') {
/* echo "email already sent";*/
} elseif ($email_sent == '1' && $reqnum != '') {
/* echo "no way to send";*/
}
elseif($email_sent == '0' && $reqnum == '' ){
$ulink = "http://xxx.eewfewt.net/tttt/yyyy.php?ID=$regid";
$to = $email;
$subject = 'hoo'; // Give the email a subject
$message = '
Thanks for Using Trial Version!
If You Wish to Continue with the Monthly Paid Package.
------------------------
Click Below
Site Renewal link: ' . $ulink . '
'.$emailfly_date.'
------------------------ ';
$headers .= 'From:werwerwer.aaa.net' . "\r\n";
$headers .= 'Bcc:ewrwerer.aaa.net' . "\r\n";
mail($to, $subject, $message, $headers);
$upadtequery = mysql_query("UPDATE xxx SET email_sent ='1' WHERE ID = '$regid'");
echo "sucess";
}
else{
echo "bye";
}
?>
From $query = mysql_query(.....bla...bla... to last else{
echo "bye";
} Put all this inside your while loop.
Also if you fetch data from same table try to use single query.
mysql_() is depreciated now. So try to use mysqli_()
include_once 'dbconnect.php';
$query = "SELECT ID,email, emailfly_date, CURRENT_DATE AS nowtime FROM xxx WHERE id in ('250','251','252','253') AND email_sent = '0'";
$result = mysql_query($query) or die(mysql_error());
while($rowdata = mysql_fetch_array($result))
{
$ID = $rowdata['ID'];
$email = $rowdata['email'];
$reqnum = $rowdata['reqnum'];
$emailfly_date = $rowdata['emailfly_date'];
$email_sent = $rowdata['email_sent'];
$ulink = "http://xxx.eewfewt.net/tttt/yyyy.php?ID=$regid";
$to = $email;
$subject = 'hoo'; // Give the email a subject
$message = '
Thanks for Using Trial Version!
If You Wish to Continue with the Monthly Paid Package.
------------------------
Click Below
Site Renewal link: ' . $ulink . '
'.$emailfly_date.'
------------------------ ';
$headers .= 'From:werwerwer.aaa.net' . "\r\n";
$headers .= 'Bcc:ewrwerer.aaa.net' . "\r\n";
mail($to, $subject, $message, $headers);
$upadtequery = mysql_query("UPDATE xxx SET email_sent ='1' WHERE ID = '$ID'");
echo "sucess";
}
Ok what i want is to go through database and send an email to every email whose database stored time and date is lower then current date and whose sent value is 0. Whats working is it goes through the loop of email and sends the emails however what i want to do is to set sent value in the database from 0 to 1 so that if i ran the program in the future it wont send the emails again to people that i already have send an email. i tried updating the send value to 1 inside my while loop but when i execute my file it stops after just one email, so it doesn't loop through database
<?php
include_once("db.php");
$date = new DateTime();
//echo $date->format('Y-m-d H:i:s')
$query = "SELECT timedate, email, sent, msgid FROM mailer";
$result = mysql_query($query);
echo "<table>";
while($row = mysql_fetch_array($result)){
$tot = $row['timedate'];
$ema = $row['email'];
$sendflag = $row['sent'];
$mess = $row['msgid'];
if(strtotime($tot) > time()) {
//echo "<tr><td>" .$row['timedate']."</td><td>";
echo"database dates higher then now dates" . "<br>";
echo "<tr><td>" .$row['email']."</td><td>" ."<br>";
echo "<tr><td>" .$row['timedate']."</td><td>" ."<br>";
echo "<tr><td>" .$row['sent']."</td><td>" ."<br>";
}
else {
echo"database dates lower then now dates" . "<br>";
echo "<tr><td>" .$row['email']."</td><td>". "<br>";
echo "<tr><td>" .$row['timedate']."</td><td>" ."<br>";
echo "<tr><td>" .$row['sent']."</td><td>" ."<br>";
$subject = "This is subject";
$message = "This is simple text message.";
$header = "From:abc#somedomain.com \r\n";
$to = $row['email'];
$retval = mail ($to,$subject,$message,$header);
if( $retval == true )
{
echo "Message sent successfully..." ."<br>";
}
else
{
echo "Message could not be sent..." ."<br>";
}
//$sql = "UPDATE mailer SET sent = 1 WHERE msgid = $mess";
$query = "UPDATE mailer SET sent = 1 WHERE msgid = $mess";
$result = mysql_query($query);
}
}
mysql_close();
?>
Rename the $result at the end to $result2 for example. You are overwriting the variable that you are looping.
First off, I am aware I am open to SQL injection, this is just a prototype. But it still should be working.
For the life of me I can't figure out why I can't pull an item out of my array. What could I possibly be doing wrong? I've been fiddling with this seemingly simple query for way too long and I can't seem to get it to pull out data. I feel like it is something so simple....
$query = 'SELECT * FROM users WHERE email = "' . $email . '"';
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_assoc($result);
$ID = $row['ID'];
I am getting no result for $ID ....
Here is my entire code:
<html>
<head>
<?php
$email = $_GET["email"];
$servername="localhost";
$username="*****";
$password="*****";
$database="*****";
$conn= mysql_connect($servername,$username,$password)or die(mysql_error());
mysql_select_db("$database",$conn);
$query = 'SELECT email FROM users WHERE email = "' . $email . '"';
$result = mysql_query($query) or die(mysql_error());
//Checks if the email address exists in the system already
if (mysql_num_rows($result) ) {
die("Duplicate email found!");
}
else {
//use current date/time combination times the number 11 times the ID to get a unique confirmation number.
$query = 'SELECT * FROM users WHERE email = "' . $email . '"';
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_assoc($result);
$ID = $row['ID'];
echo $row;
$date = date("mydhis");
$date2 = $date * 11 * $ID;
echo $ID . " <-> " . $date . " <-> <p>" . $date2;
$sql="insert into users (first,last,displayname,email,password,verification_email)values('$_GET[first]','$_GET[last]','$_GET[display]','$_GET[email]','$_GET[password]','$date2')";
$result=mysql_query($sql,$conn) or $string = mysql_error();
$confirmlink = "http://www.somewebsite.com/android/confirm.php?" . $date2;
$to = $_GET['email'];
$subject = "Thank you for Registering!";
$message = "Hello " . $_GET['display'] . " and thank you for registering with the Smeet app! To confirm your email address (and let us know you aren't a bot), please click the following link: " . $confirmlink;
$from = "noreply#smeet.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers) or die('You have successfully registered however mail servers are currently down, you may or may not receive a confirmation email');
print "<h1>You have registered successfully</h1>";
print "You will receive an email shortly with instructions on how to confirm your email address.</a>";
}
?>
</body>
</html>
Thanks for any help at resolving this.
It was a simple answer and I figured it out!
My $ID was being pulled before the record was created, that's why it was blank! Dumb mistake on my part.