I have this php code:
<?php
//include database
include 'db.php';
//grab the emails from the database
$sql = "SELECT email FROM `emails`";
$result = mysqli_query($con, $sql);
while($row = mysqli_fetch_array($result, MYSQL_ASSOC)){
// update the database
//sanitinzw
$row = mysqli_real_escape_string($con, $row['email']);
//mail the emails
$to = $row;
$subject = 'HELLOO';
$message = 'alooooo';
$headers = 'From: admin#admin.com' . "\r\n" .
'Reply-To: admin#admin.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
}
?>
It works just fine; however, when I add a query to it only the first email in my database is sent here it what it looks like.
<?php
//include database
include 'db.php';
//grab the emails from the database
$sql = "SELECT email FROM `emails`";
$result = mysqli_query($con, $sql);
while($row = mysqli_fetch_array($result, MYSQL_ASSOC)){
// update the database
//sanitinzw
$row = mysqli_real_escape_string($con, $row['email']);
//mail the emails
$to = $row;
$subject = 'hello';
$message = 'aloooooo';
$headers = 'From: admin#admin.com' . "\r\n" .
'Reply-To: admin#admin.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
$sql = "UPDATE `emails` SET times_used = times_used + 1 WHERE email = '$row' ";
$result = mysqli_query($con, $sql);
}
?>
for some reason it only updating the first email in the database and when I try to echo out $row only the first email is echoe'd
Thanks for the help
You are rewriting the $result variable, which is also used in the while loop. After the first iteration, $result is set to the result of the update query, so there are no other rows that can be fetched.
You can write this, for example:
$sql = "UPDATE `emails` SET times_used = times_used + 1 WHERE email = '$row' ";
$result2 = mysqli_query($con, $sql);
That should work (if there is no other mistake).
not sure mysqli_real_escape_string exists as a function... or at least php.net doesn't know about it.. more over, with mysql_real_escape_string (what I guess you want to use) the arguments go the other way around:
$row = mysql_real_escape_string($row['email'],$con);
Related
I've got stuck trying to echo multiple rows from a MySql database into the $message part of the email. The attached code only returns one result.
I want to send 1 email, in the email I want to output the 10 rows from the SQL query.
I actually receive the same email ten separate times rather than one email with ten rows.
$sql = "SELECT * FROM stoic_quotes Order By rand() Limit 0,10";
$result = $conn->query($sql);
while($row = $result->fetch_assoc()) {
$to = 'email#email.com';
$subject = 'Daily Stoic Email';
$message = $row["Maxim"]. "</br>";
$headers = 'From: email#email.com' . "\r\n" .
'Reply-To: email#email.om' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
}
Move the mail call out of the loop
// don't select all (*) fields if you only need one
$sql = "SELECT Maxim FROM stoic_quotes Order By rand() Limit 0,10";
$result = $conn->query($sql);
$message = '';
while($row = $result->fetch_assoc()) {
$message .= $row["Maxim"]. "</br>";
}
// prepare email headers
$to = 'email#email.com';
$subject = 'Daily Stoic Email';
$headers = 'From: email#email.com' . "\r\n" .
'Reply-To: email#email.om' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
// then send
mail($to, $subject, $message, $headers);
I haven't tested the code but give this a try:
$to = 'email#email.com';
$subject = 'Daily Stoic Email';
$sql = "SELECT * FROM stoic_quotes Order By rand() Limit 0,10";
$result = $conn->query($sql);
while($row = $result->fetch_assoc()) {
$message = $row["Maxim"]. "</br>";
}
$headers = 'From: email#email.com' . "\r\n" .
'Reply-To: email#email.om' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
I have placed the to, subject, header and mail function out of the loop.
Cheers
I am trying to send out emails based on specific results pulled from my database. I'm trying to do a foreach statement inside my while loop to send out an email for each result, but I am only getting one result to come out even though I know there should be more than one.
Here is my call to the database and my while loop:
//get USER info
$getTenant = $db->query("SELECT user_fname, user_lname, user_email FROM users WHERE user_id={$user_id}");
$tenant = $getTenant->fetch_array();
$user_fname = $tenant['user_fname'];
$user_lname = $tenant['user_lname'];
$user_email = $tenant['user_email'];
// SELECT statement for RENTALS.
$getRentals = $db->query("SELECT rental_id, user_id, unit_id, rental_renewdate, rental_balance FROM rentals");
// Search each result to send email
while($rental = $getRentals->fetch_assoc()) {
$rental_id = $rental['rental_id'];
$user_id = $rental['user_id'];
$unit_id = $rental['unit_id'];
$rental_renewdate = strtotime($rental['rental_renewdate']);
$rental_balance = $rental['rental_balance'];
And here is my attempt at creating a foreach statement to send out an email for each row that gets pulled:
if ($today == $notify_prior) {
// SELECT statement for EMAILS for PRIOR.
$getPrior = $db->query("SELECT email_subject, email_h1, email_body, email_fromemail FROM emails WHERE email_id=1");
$prior = $getPrior->fetch_array();
$email_subject = $prior['email_subject'];
$email_h1 = $prior['email_h1'];
$email_body = $prior['email_body'];
$email_fromemail = $prior['email_fromemail'];
$getPrior->close();
foreach ($getRentals as $prior_warnings){
$prior_warnings[] = array($rental_id, $user_fname, $user_lname, $user_email);
/* PRIOR NOTIFICATION CUSTOMER EMAIL*/
$to = '$user_email';
$subject = $email_subject;
$headers = 'Content-type: text/html; charset=iso-8859-1\r\n';
$headers .= 'From: '. $email_fromemail . '\r\n' . 'Reply-To: '. $email_fromemail . '\r\n' . 'X-Mailer: PHP/' . phpversion();
$message = '<p>'. $user_fname .','. $user_lname .'</p>
<h1>'. $email_h1 .'</h1>
<p>'. str_replace(array("{UNIT_NUMBER}","{DUE_DATE}"), array($unit['unit_number'],date('m-d-Y',$rental_renewdate)), stripslashes($email_body)) .'</p>';
mail($to, $subject, $message, $headers);
}
}
}
The only thing I seemed to have accomplished is creating an infinite loop that sends tons of emails out for one guy.
I want to be able to send multiple rows from a database into one email. So far all I get is two (or more) emails containing one row in each. How do I go about getting everything into the one email?
This is my code. Keeping the mail() outside the while loop only gives me the last entry. Keeping it in the while loop sends two emails.
$sql = "SELECT productid, kind, qty, price, GROUP_CONCAT(product) as product FROM orderitems LEFT JOIN Products ON orderitems.code = Products.productid WHERE orderitems.customerid = $customerid GROUP BY productid";
$result = mysqli_query($db, $sql) or die(mysqli_error($db));
while($row = mysqli_fetch_array($result)) {
$product = $row['product'];
$productid = $row['productid'];
$to = "email#gmail.com";
$subject = "Order";
$emailBody = "ID: ".$product."\n"."Product: ".$productid."\n";
$emailBody .= "Total: ".$total."\n";
$headers = 'From: Email <no-reply#someemailaddress>' . "\r\n" .
'Reply-To: someemailaddress' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $emailBody, $headers); }
I'd be really happy if someone could give me a nudge in the right direction!
I think you only want to repeat this string.
ID: ".$product."\n"."Product: ".$productid."\n";
So it should be in loop and other should not. Like:
$sql = "SELECT productid, kind, qty, price, GROUP_CONCAT(product) as product FROM orderitems LEFT JOIN Products ON orderitems.code = Products.productid WHERE orderitems.customerid = $customerid GROUP BY productid";
$result = mysqli_query($db, $sql) or die(mysqli_error($db));
$emailBody = '';
$total = 0;//I dont know what is total for you
while($row = mysqli_fetch_array($result)) {
$product = $row['product'];
$productid = $row['productid'];
$emailBody .= "ID: ".$product."\n"."Product: ".$productid."\n";
$total = $total + 1; //Just for example
}
$to = "email#gmail.com";
$subject = "Order";
$emailBody .= "Total: ".$total."\n";
$headers = 'From: Email <no-reply#someemailaddress>' . "\r\n" .
'Reply-To: someemailaddress' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $emailBody, $headers);
The problem is that you have the mail() function inside the while loop, so for each loop the function send an email.
You could extract the mail() from the loop, and only collect the rows you need inside the loop, something like that:
$emailBody = '';
while($row = mysqli_fetch_array($result)) {
$product = $row['product'];
$productid = $row['productid'];
$emailBody .= "ID: ".$product."\n"."Product: ".$productid."\n";
$emailBody .= "Total: ".$total."\n";
}
$to = "email#gmail.com";
$subject = "Order";
$headers = 'From: Email <no-reply#someemailaddress>' . "\r\n" .
'Reply-To: someemailaddress' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $emailBody, $headers); }
I want to be able to put the name of the person in the email. But I don't know how to retain the value outside the loop. Also, I'm wondering if this will work if there is more than one result.
Here is the php:
// WHO HAS BEEN A MEMBER FOR ONE YEAR
function joinDateFilter(){
$query = mysql_query("SELECT * FROM user WHERE date_joined = DATE_SUB(curdate(), INTERVAL 1 YEAR)");
$mail_to = "";
while ($row = mysql_fetch_array($query)){
echo $row['name']." - ".$row['email']."\n";
$mail_to = $row['email'].", ";
$name = $row['name'];
}
if (!empty($mail_to)){
sendEmail($mail_to);
}
}
// SEND EMAIL
function sendEmail($mail_to) {
$from = "webmaster#mydomain.com";
$message = "Happy Anniversary!";
$headers = 'From: '.$from."\r\n" .
'Reply-To:'.$_POST['email']."\r\n" .
"Content-Type: text/html; charset=iso-8859-1\n".
'X-Mailer: PHP/' . phpversion();
mail($mail_to, "Congratulations ".$name."!", $message, $headers);
}
In that example above, the mail never reads their name, as the value seems to be forgotten outside of the while loop. Can someone help me?
Currently, your code overwrites the email id stored in $mail_to in each iteration of the while loop. So, it will have only the email id of the last user. And you need name of each user (stored in $name and used in the function). You need to send the email whenever a user is found. Try this:
// WHO HAS BEEN A MEMBER FOR ONE YEAR
function joinDateFilter(){
$query = mysql_query("SELECT * FROM user WHERE date_joined = DATE_SUB(curdate(), INTERVAL 1 YEAR)");
while ($row = mysql_fetch_array($query)){
echo $row['name']." - ".$row['email']."\n";
$mail_to = $row['email'];
$name = $row['name'];
if (!empty($mail_to))
sendEmail($mail_to, $name);
}
}
// SEND EMAIL
function sendEmail($mail_to, $name) {
$from = "webmaster#mydomain.com";
$message = "Happy Anniversary!";
$headers = 'From: '.$from."\r\n" .
'Reply-To:'.$_POST['email']."\r\n" .
"Content-Type: text/html; charset=iso-8859-1\n".
'X-Mailer: PHP/' . phpversion();
mail($mail_to, "Congratulations ".$name."!", $message, $headers);
}
To solve your problem you need to put the send mail function inside the loop and add a name parameter in the function since you are using it inside the function.
Something like this might do the trick:
// WHO HAS BEEN A MEMBER FOR ONE YEAR
function joinDateFilter(){
$query = mysql_query("SELECT * FROM user WHERE date_joined = DATE_SUB(curdate(), INTERVAL 1 YEAR)");
$mail_to = "";
$name = "";
while ($row = mysql_fetch_array($query)){
echo $row['name']." - ".$row['email']."\n";
$mail_to = $row['email'].", ";
$name = $row['name'];
if (!empty($mail_to) and !empty($name)){
sendEmail($mail_to, $name);
}
}
}
// SEND EMAIL
function sendEmail($mail_to, $name) {
$from = "webmaster#mydomain.com";
$message = "Happy Anniversary!";
$headers = 'From: '.$from."\r\n" .
'Reply-To:'.$_POST['email']."\r\n" .
"Content-Type: text/html; charset=iso-8859-1\n".
'X-Mailer: PHP/' . phpversion();
mail($mail_to, "Congratulations ".$name."!", $message, $headers);
}
If you use the phpmailer, you could do some like:
function joinDateFilter(){
require_once("class.phpmailer.php");
$mail = new PHPMailer();
$mail->From = "webmaster#mydomain.com";
$mail->Subject = "Congratulations";
$mail->Body = "Happy Anniversary!";
$query = mysql_query("SELECT * FROM user WHERE date_joined = DATE_SUB(curdate(), INTERVAL 1 YEAR)");
while ($row = mysql_fetch_array($query)){
$mail->AddBCC($row['email'],$row['name']);
}
$mail->Send();
$mail->ClearAddresses();
}
Or if you want to have diffrent Subjects than you could do this...
function joinDateFilter(){
require_once("class.phpmailer.php");
$mail = new PHPMailer();
$mail->From = "webmaster#mydomain.com";
$mail->Body = "Happy Anniversary!";
$query = mysql_query("SELECT * FROM user WHERE date_joined = DATE_SUB(curdate(), INTERVAL 1 YEAR)");
while ($row = mysql_fetch_array($query)){
$mail->Subject = "Congratulations " . $row['name'];
$mail->AddAddress($row['email'],$row['name']);
$mail->Send();
$mail->ClearAddresses();
}
}
Anyway, you save 1 function and its kind of handy to :D
And its also very easy to add attachments or sending html emails and stuff like this. How to do most things can be googled. So you may need a small tutorial.
If you download the mailer pac, you really only need the class.phpmailer.php the rest is not necessary :)
This should work:
<?php
// WHO HAS BEEN A MEMBER FOR ONE YEAR
function joinDateFilter(){
$query = mysql_query("SELECT * FROM user WHERE date_joined = DATE_SUB(curdate(), INTERVAL 1 YEAR)");
$mail_to = "";
while ($row = mysql_fetch_array($query)){
echo $row['name']." - ".$row['email']."\n";
$mail_to = $row['email'].", ";
$name = $row['name'];
if (!empty($mail_to)){
sendEmail($mail_to, $name);
}
}
}
// SEND EMAIL
function sendEmail($mail_to, $name) {
$from = "webmaster#mydomain.com";
$message = "Happy Anniversary!";
$headers = 'From: '.$from."\r\n" .
'Reply-To:'.$_POST['email']."\r\n" .
"Content-Type: text/html; charset=iso-8859-1\n".
'X-Mailer: PHP/' . phpversion();
mail($mail_to, "Congratulations ".$name."!", $message, $headers);
}
?>
Keep in mind that mail function can be sometimes problematic. Google about that issue. What you can use as an replacement for the mail function is SwiftMailer
I'm currently working on a PHP/MySQL script that does the following, in this order:
1) Checks DB for any videos that need converting
2) Once determined that a video needs to be converted, it begins to convert
*3) Notifies the "creator" of video that it's been created.
*4) Notifies all users who are "receivers" of the video, that they have a new video.
Note, I'm looking for help on the starred numbers above.
Here's my basic db/table structure (some parts intentionally left out, because I don't want to type them:
video_data:
id
creator_id
needs_process
video_info
id
video_id (relational to 'id' in table above).
receiver_id
tblusers
usrID
usrFirst
usrLast
usrEmail
Here's what I currently have in my script:
<?php
require("connection.php");
$rs = mysql_db_query($DBname,$sql,$link);
$data = mysql_query("SELECT * FROM video_data WHERE needs_process=1 LIMIT 1") or die(mysql_error());
while($row = mysql_fetch_array( $data ))
{
$id = $row['id'];
$sender = $row['sender_id'];
//Convert The Video Here (I've already written this code).
//Maybe Create a Thumbnail Too (I've already written this code).
}
My question is: In the code above, how would I both email the "creator" (always 1 person) and email the receivers (sometimes multiple receivers). Moreover, these would probably be "separate" or "different" emails. For example, the creator would receive an email saying "Thanks for uploading!", while the receivers would receive one saying "You've got a new video."
Any help on this would be great!
It's something like this:
<?php
require("connection.php");
$rs = mysql_db_query($DBname,$sql,$link);
$data = mysql_query("SELECT * FROM video_data WHERE needs_process=1 LIMIT 1") or die(mysql_error());
while($row = mysql_fetch_array( $data ))
{
$id = $row['id'];
$sender = $row['sender_id'];
//Convert The Video Here (I've already written this code).
//Maybe Create a Thumbnail Too (I've already written this code).
//mail the creator
$data2 = mysql_query("SELECT * FROM tblusers WHERE usrID=$row[creator_id] LIMIT 1") or die(mysql_error());
$creatordata = mysql_fetch_array( $data2 );
mailtocreator($creatordata['userEmail'], $creatordata['usrFirst'].' '.$creatordata['usrLast']);
//mail the receiver
$data3 = mysql_query("SELECT * FROM tblusers t, video_info vi WHERE vi.video_id = $row[id] AND vi.receiver_id = t.usrID") or die(mysql_error());
while($row2 = mysql_fetch_array($data3))
{
mailtocreator($row2['userEmail'], $row2['usrFirst'].' '.$row2['usrLast'], $creatordata['usrFirst'].' '.$creatordata['usrLast']);
}
}
function mailtocreator($toemail, $toname)
{
//do send mail routine
$subject = 'video processed';
$message = 'hello '.$toname.',
Thank you for uploading video.';
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($toemail, $subject, $message, $headers);
}
function mailtoreceiver($toemail, $toname, $fromname)
{
//do send mail routine
$subject = 'new video received';
$message = 'hello '.$toname.',
You just got new video from '.$fromname.'.';
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($toemail, $subject, $message, $headers);
}
Basically, it's just around this query:
$data2 = mysql_query("SELECT * FROM tblusers WHERE usrID=$row[creator_id] LIMIT 1") or die(mysql_error());
which retrieve detail info of current uploader (taken from $row[creator_id]) and
$data3 = mysql_query("SELECT * FROM tblusers t, video_info vi WHERE vi.video_id = $row[id] AND vi.receiver_id = t.usrID") or die(mysql_error());
which retrieve all receiver by querying from table tblusers and video_info then send those information to mailtocreator and mailtoreceiver respectively.