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
Related
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 have some affiliate script that I buy, however this script doesn't have function to
"Send password and login id to all my affiliate" weekly or maybe once per 2 week.
This is because in my experience he/she do not have many members not coming back because not only forgot password but also forgot username.
I want every month the members get email example
To: cubaan04#hotmail.com
From : admin#admin.com
Subject : Reminder : Your login name and password for website
Hi Firstname,
We worry if you forgot your login name and password. We love to see you back and active in our network. Here the detail...
your login name : Cubaan04
your password : Cubaan04
please login to website
Your truely
admin website
I found this script but dont know how to modified it
<?php
$db = mysql_connect("localhost", "plazacen_t", "plazacentrey");
mysql_select_db("plazacen_rak",$db);
$result = mysql_query("SELECT `email` FROM `affiliate_users`");
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
sendMail($row[0]);
}
mysql_free_result($result);
function sendMail($to){
$subject = 'the subject';
$message = 'hello Shahrul';
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
}
?>
it not work
and how to put password and login in massage from the database?
Thanks you for read this, iam just noob for this
hope get help from you guys
updated
From our friend answer, i modified it. Yes it send email now but not work like i want, i put
<?php
$db = mysql_connect("localhost", "plazacen_rak", "rakan852");
mysql_select_db("plazacen_rak",$db);
$result = mysql_query("SELECT email,password,name FROM `affiliate_users`");
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
sendMail($row[0],$row[1],$row[2]);
}
mysql_free_result($result);
function sendMail($to,$pwd,$id){
$subject = 'the subject';
$message = 'your login name:". $id ." password: ". $pwd ."';
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
}
?>
what I received is email like this;
From : webmaster#example.com
the subject
04/04/2015 9.59 PM
your login name:". $id ." password: ". $pwd ."
it is not change the $id to username in database, so do the $pwd
don't know whats wrong, any further help. TQ
The problem is with your sendMail function, You cannot take variables inside single quotes modify it as
function sendMail($to,$pwd,$id){
$subject = 'the subject';
$message = "your login name: {$id} password: {$pwd}";
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
}
another alternative could be
function sendMail($to,$pwd,$id){
$subject = 'the subject';
$message = 'your login name:' . $id .' password: '. $pwd;
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
}
Both will work like a charm.
First change your query to:
$result = mysql_query("SELECT email,password,userid FROM `affiliate_users`");
Then change your code like this:
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
sendMail($row[0],$row[1],$row[2]);
}
mysql_free_result($result);
function sendMail($to,$pwd,$id){
$subject = 'the subject';
**$message = "your login name:". $id ." password: ". $pwd ."";**
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
}
Note
I do not know what your field names are in the affiliate_users table so replace this "email,password,userid" with your actual field name.
If you want to execute this script every month then you can use cronjob.
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); }
complete beginner at PHP and was wanting a little direction for a website I am creating. I want the admin of the website to receive an email with all of the form information aswell as it being stored in the database. The database is storing the information fine, just need an email notification. How is this achieved. My PHP code is:
<?php
session_start();
include('connection.php');
$product = $_POST['product'];
$productcomments = $_POST['productcomments'];
$name = $_POST['name'];
$address = $_POST['address'];
$age = $_POST['age'];
$delivery = $_POST['delivery'];
mysql_query("INSERT INTO orderform(product, productcomments, name, address, age, delivery)VALUES('$product', '$productcomments', '$name','$address', '$age', '$delivery')");
header("location: google.com");
$to = 'j_bussey#live.co.uk';
$subject = 'Order';
$message = 'Product: ' . $product . '<br /> Product Comments: ' . $productcomments . '<br /> ';
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
mysql_close($con);
?>
this is the very basic example of mail function. read more about mail() manual here.
$email = $_POST['email'];
$subject = "Email Subject";
$message = "Email Message Body";
mail($email, $subject, $message, "from: admin#yourdomain.com");
PHP has an awesome mail() function. I'm taking this from the documentation page here: http://us2.php.net/manual/en/function.mail.php
<?php
$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, $message, $headers);
?>
So since you're already grabbing the variables, you would just edit the $message variable in the code above to look something like this:
$message = 'Product: ' . $product . '<br /> Product Comments: ' . $productcomments . '<br /> ';
etc, etc. If you don't want to assume that they have html emails enabled, you would use \n instead of <br />
Edit:
You also need to change your header('Location: google.com'); to header('Location: http://www.google.com'); or wherever you want to redirect after the email has been sent off.
I just want to get an email from a customer table and send message to that email. I am getting this error
"Warning: mysql_num_rows() expects parameter 1 to be"
My code is as follows:
<?php
$mysql = mysql_connect("localhost", "hname", "passs", "dbname");
$getusers = mysql_query("SELECT * FROM customer");
while ($row = mysql_fetch_array($result)) {
sendMail($row['email']);
}
mysql_free_result($result);
function sendMail($to){
$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, $message, $headers);
}
?>
You are making a myslqi connection but then using mysql_* (no i) functions. You should use all mysqli.
You can try this code :
while ($row = mysql_fetch_array($result)) {
TO
while ($row = mysql_fetch_array($getusers)) {