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.
Related
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 am trying to send email of which one of the data is from database, i tried to look on some post here in stack-overflow but they were different;
I tried to send without the data from the database and it works fine,but after adding database fetch its not working
<?php
include 'include/connect.php';
if (isset($_POST['book2'])) {
$id = mysqli_real_escape_string($conn, $_GET['id']);
$sql = "SELECT room_price FROM room_details WHERE id='$id';";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$room_price = $row['room_price'];
$email = $_POST['email'];
$room_type = $_POST['room_type'];
$checkin = $_POST['checkin'];
$checkout = $_POST['checkout'];
$adults = $_POST['adults'];
$children = $_POST['children'];
$mailTo = "booking#johndoexxx.com";
$headers = "Guest sent e-mail from: " . $email;
$txt = "New booking received, room for " . $adults . " adults and " . $children . " child / children, reservation starts on " . $checkin . " up to " . $checkout . " whereby " . $room_type . " room is selected, Please respond to the sender!";
$heading = "New Booking!";
mail($mailTo, $heading, $headers, $txt);
header("Location: index.php?bookingsent");
}
}
} else {
echo "Booking failed to process!, observe your inputs carefully!";
}
?>
was expecting it will fetch data from form and will send the data into the targeted email.
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.
The code below shows one email address as a output but I want to get a list of all
email addresses (seperated by comma) of customer table. How can I get that?
<?php
$SQLstring = "SELECT email FROM customers";
$QueryResult = #mysqli_query($DBConnect, $SQLstring)
or die("<p>Unable to execute the query.</p>" .
"<p> Error code " . mysqli_errno($DBConnect) . ":" . mysqli_error ($DBConnect))."</p>";
$NumRows = mysqli_num_rows($QueryResult);
if ($NumRows == 0)
{
echo "<p>No email found.</p>";
}
else
{
for($i = 1; $i <= $NumRows; $i++)
{
$Row = mysqli_fetch_row($QueryResult);
$email = stripslashes($Row[0]);
echo $email;
}
}
?>
This is mysqli not mysql you're using so things work a little differently...
Assuming you've created your mysqli connection with something like $DBConnect = new msqli( ... );
It's probably better to store the result before you manipulate it; try something like:
$success = $DBConnect->real_query($SQLstring);
if($success) {
$result = $DBConnect->store_result();
while($row = $result->fetch_array(MYSQLI_ASSOC)) {
echo $row['email'] . "<br />\n"; //for debugging purposes
}
}
$result->free();
Change:
$email = stripslashes($Row[0]);
To:
$email = stripslashes($Row[$i]);
And you should be set
The PHP docs say that mysqli_num_rows may not return the correct number of rows until you've retrieved all rows, so perhaps, instead of using a row count, just keep fetching rows until you run out:
while ($Row = mysqli_fetch_row($QueryResult))
{
$email = stripslashes($Row[0]);
echo $email;
}
EDIT: If you want to store the emails in an array rather than just echoing them, simply change it to this:
while ($Row = mysqli_fetch_row($QueryResult))
{
$email[] = stripslashes($Row[0]);
}
Now $email will be an array containing all of the emails.
use mysql_fetch_assoc in while loop
$NumRows = mysqli_num_rows($QueryResult);
if ($NumRows == 0)
{
echo "<p>No email found.</p>";
}
else
{
while($row = mysql_fetch_assoc($QueryResult)){
$email = stripslashes($row['email']);
echo $email;
}
}
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);