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); }
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 want to make a birthday reminder and from here I don't know what to do.
In my MySQL users DB U have birthdate with [month/day/year] And is not working,no error, i updated my bday with this day and still no activity.
This is what I have so far:
<?php
$conn = new PDO('mysql:host=localhost;dbname=tbl', 'user', 'pass');
$today = date("m.d.y");
$sqlb = "SELECT `birthdate`, `name`, `surename` FROM `mls_users` WHERE birthdate = '$today'";
$userz = $conn->query($sqlb);
foreach ($userz as $row) {
$name = $row['name'];
$surename = $row['surename'];
echo 'Todays is'.$name.' '.$surename.' birthday';
}
?>
I suppose you want to create an email notification.
$mail_content = '';
foreach ($userz as $row) {
$name = $row['name'];
$surename = $row['surename'];
$mail_content .= 'Todays is'.$name.' '.$surename.' birthday. <br>';
}
// send mail to you
// mail code
if($mail_content){
$to = 'nobody#example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $mail_content, $headers);
}
If you have the users email IDs saved in your Db you can send email to them
foreach ($userz as $row) {
$name = $row['name'];
$surename = $row['surename'];
//mail code
//email message
$mail_content = 'Happy birthday '.$name.' '.$surename.' !. <br>';
$to = $row['user_email']; //users email field
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $mail_content, $headers);
}
You can automate the script executing process using CRON Job
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 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 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);