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)) {
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
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 7 years ago.
I am trying to send an email using php but unable to do it, whereas I am getting data into MySQL database.
I have posted my whole code, please check and let me know where's the mistake.
$objConnect = mysql_connect("localhost","username","pwd");
$objDB = mysql_select_db("database");
$strName = $_POST["name"];
$strEmail = $_POST["email"];
$to = $strEmail;
$subject = 'the subject';
$message = 'hello';
$headers = 'From: mymail#gmail.com' . "\r\n" .
'Reply-To: mymail#gmail.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$strSQL = "insert into data (name, email) VALUES ('".$strName."','".$strEmail."')";
$objQuery = mysql_query($strSQL);
if(!$objQuery)
{
$arr['StatusID'] = "0";
$arr['Message'] = "Cannot save data!";
}
else
{
mail($to, $subject, $message, $headers);
$arr['StatusID'] = "1";
$arr['Message'] = "Data saved successfully";
}
mysql_close($objConnect);
echo json_encode($arr);
This code is really working ...
<?php
$to = 'to_email#gmail.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: mymail#gmail.com' . "\r\n" .
'Reply-To: mymail#gmail.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$errLevel = error_reporting(E_ALL ^ E_NOTICE); // suppress NOTICEs
if(!mail($to, $subject, $message, $headers)){
error_reporting($errLevel);
}
?>
You have not added mail() function in your code then how will it send the mail and to send email from localhost it should be properly configured. Below code will work on the server.
$objConnect = mysql_connect("localhost","username","pwd");
$objDB = mysql_select_db("database");
$strName = $_POST["name"];
$strEmail = $_POST["email"];
$to = $strEmail;
$subject = 'the subject';
$message = 'hello';
$headers = 'From: mymail#gmail.com' . "\r\n" .
'Reply-To: mymail#gmail.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
if(!mail($to, $subject, $message, $headers)){
echo 'Mail cant be send';
}
else
{
$strSQL = "insert into data (name, email) VALUES ('".$strName."','".$strEmail."')";
$objQuery = mysql_query($strSQL);
if(!$objQuery)
{
$arr['StatusID'] = "0";
$arr['Message'] = "Cannot save data!";
}
else
{
mail($to, $subject, $message, $headers);
$arr['StatusID'] = "1";
$arr['Message'] = "Data saved successfully";
}
echo json_encode($arr);
}
mysql_close($objConnect);
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); }