...

View Full Version : Sending email id with php and mysql.



pdloan
07-23-2012, 10:30 AM
I have stored the email id in the database and want to sent a mail on that email id.


<?php
$username=$_POST['uname'];

$con=mysql_connect("localhost","root","") or ("Cannot connect localhost");

mysql_select_db('project',$con) or die("connection field");

$q=mysql_query("select email from admin where username='$username'");

$numrows=mysql_num_rows($q);

if($numrows!=0)
{
while( $row=mysql_fetch_assoc($q))
{
$emailid = $row['email'];
}
$to = $emailid;
$subject = "Test mail";
$message = "Your security code is 5555.";
$from = "abc.sample@gmail.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";

}
else
die ("That username doesnot exist");
?>

It sows the "Warning: mysql_num_rows() expects parameter 1 to be resource"
I am new to php please help me out.Thanks in advance.

tangoforce
07-23-2012, 12:22 PM
This means your SQL query failed. Presumably because you've not run $username through mysql_real_escape_string(). If you type in something into the username box (such as an ' for example) then it will break your SQL string and when you put it through mysql_query the mysql server will not parse it correctly, return a false instead of a resource result and consequently your call to mysql_num_rows() will return an error.

pdloan
07-23-2012, 01:48 PM
Sir, I corrected the above error after removing error i again checked then it showed the following error "Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() on line 29 "
The code on line 29 is" mail($to,$subject,$message,$headers); "
The file code is as below :-


<?php
$username=$_POST['uname'];

$con=mysql_connect("localhost","root","") or ("Cannot connect localhost");

mysql_select_db('project',$con) or die("connection field");

$q=mysql_query("select email from admin where uname='$username'");

$numrows=mysql_num_rows($q);

if($numrows!=0)
{
while( $row=mysql_fetch_assoc($q))
{
$emailid = $row['email'];
}
$to = $emailid;
$subject = "Test mail";
$message = "Your security code is .";
$from = "abc.sample@gmail.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";

}
else
die ("That username doesnot exist");
?>

Please help me out.
Waiting for your answer sir.

tangoforce
07-24-2012, 02:17 AM
Sir, I corrected the above error

So why does the code look identical? - You have not used mysql_real_escape_string() and nothing else looks different either.

In other words, you have made no changes :confused:

How do you expect me to help you if you won't make changes and post the same code? :rolleyes:

pdloan
07-24-2012, 09:25 AM
Sorry sir but i m not getting where to use the following statement mysql_real_escape_string(). Previously i used username instead of using uname hence it was showing that error. Then i changed it to uname the error shown was "Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() on line 29 "
The code on line 29 is" mail($to,$subject,$message,$headers); " .
Sorry but please explain me where to use and how to use (mysql_real_escape_string() statement) i dont know much about php .
Please help me out.

tangoforce
07-24-2012, 11:09 AM
FYI:

$username = mysql_real_escape_string($_POST['uname']);

The function description is available on www.php.net

As for your email problem, thats a server configuration issue. We can't help you with that, you will need to show the php.ini settings.

pdloan
07-24-2012, 12:09 PM
sir can you tell, while installation weather we have to make any changes in php.ini file. I tried to attach the php.ini file but it is showing upload file error.

Arcticwarrio
07-24-2012, 01:31 PM
something like this maybe?


<?php
$username = mysql_real_escape_string($_POST['uname']);

$con=mysql_connect("localhost","root","") or ("Cannot connect localhost");

mysql_select_db('project',$con) or die("connection field");

$q=mysql_query("SELECT `email` FROM `admin` WHERE `uname` = '".$username);

$numrows=mysql_num_rows($q);

if($numrows!=0)
{
while( $row=mysql_fetch_assoc($q))
{
$emailid = $row['email'];
}
$to = $emailid;
$subject = "Test mail";
$message = "Your security code is .";
$from = "abc.sample@gmail.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";

}
else
die ("That username doesnot exist");
?>

pdloan
07-24-2012, 02:04 PM
I made these changes but it is showing there is some problem related to php.ini file.

Arcticwarrio
07-24-2012, 02:42 PM
you could inject it into a mail handler:

download php mailer script from here: http://phpmailer.sourceforge.net

put class.phpmailer.php in the same folder as your functions.php

if you dont yet have one make one and add the following or add this to your existing one

functions.php

<?php
function Email($To,$Code) {
include ("class.phpmailer.php");

$mail = new PHPMailer();

$mail->IsSMTP(); // set mailer to use SMTP
$mail->SMTPDebug = 1;
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->SMTPSecure = 'ssl';
$mail->Host = "smtp.gmail.com"; // specify main server
$mail->Port = 465;

$mail->Username = "abc.sample@gmail.com"; // SMTP username
$mail->Password = "pass"; // SMTP password

$mail->From = "abc.sample@gmail.com";
$mail->FromName = "Admin - ".$From;
$mail->AddAddress($To);

$mail->WordWrap = 50; // set word wrap to 50 characters
$mail->IsHTML(true); // set email format to HTML

$mail->Subject = "Test mail";
$mail->Body = "Your security code is ".$Code;

if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
}
?>

then use this to send your mail


<?php
include ("functions.php");
$username = mysql_real_escape_string($_POST['uname']);

$con=mysql_connect("localhost","root","") or ("Cannot connect localhost");

mysql_select_db('project',$con) or die("connection field");

$q=mysql_query("SELECT `email` FROM `admin` WHERE `uname` = '".$username);

$numrows=mysql_num_rows($q);

if($numrows!=0)
{
while( $row=mysql_fetch_assoc($q))
{
$emailid = $row['email'];
}
if (Email($emailid, $row['code']) == 'Sent'){
echo "Mail Sent.";
}
}
else
die ("That username doesnot exist");
?>

Arcticwarrio
07-24-2012, 02:47 PM
actually you should never have more than 1 username the same so you could use this:


<?php
include ("functions.php");
$con=mysql_connect("localhost","root","") or die("Cannot connect localhost");
mysql_select_db('project',$con) or die("connection field");
$q=mysql_query("SELECT `email` FROM `admin` WHERE `uname` = '".mysql_real_escape_string($_POST['uname']));
if (mysql_num_rows($q) != 0){
$row = mysql_fetch_assoc($q);
if (Email($row['email'], $row['code']) == 'Sent'){
echo "Mail Sent.";
}
}else{die ("That username doesnot exist");}
?>

pdloan
07-24-2012, 03:24 PM
thanks for the help i checked it, now mail is sent. All the problem was i was running on the localhost. But when all the files are uploaded on the server it is directly sending the email. Thank you very much Arcticwarrio for helping.:D

Keleth
07-24-2012, 03:38 PM
Well, if you don't have a mail server setup and running on your local system, of course you can't send mails from it :)

tangoforce
07-25-2012, 02:03 AM
$q=mysql_query("SELECT `email` FROM `admin` WHERE `uname` = '".mysql_real_escape_string($_POST['uname']));

You've missed something on that line and in your previous versions of it in your previous posts:

$q=mysql_query("SELECT `email` FROM `admin` WHERE `uname` = 'whatever'

That last apostrophe is rather important :thumbsup:

pdloan
07-25-2012, 10:02 AM
Well, if you don't have a mail server setup and running on your local system, of course you can't send mails from it :)
I totally agree with you sir, mail server setup is essential for sending email from localhost.

Arcticwarrio
07-25-2012, 11:44 PM
You've missed something on that line and in your previous versions of it in your previous posts:

$q=mysql_query("SELECT `email` FROM `admin` WHERE `uname` = 'whatever'

That last apostrophe is rather important :thumbsup:

Not when a variable is the last part of the line


query("SELECT * FROM users WHERE uname = ".$username)

tangoforce
07-26-2012, 12:02 AM
Not when a variable is the last part of the line


query("SELECT * FROM users WHERE uname = ".$username)

No thats not what I was getting at. You had ONE single quote at the beginning of the data to be inserted and not at the other end. You're now defusing the attention away from the issue suggesting you don't need quotes at all. Re-read what I wrote :thumbsup:

Arcticwarrio
07-26-2012, 08:10 AM
Ah yes, i didnt see that :)

tangoforce
07-26-2012, 10:33 AM
FYI unless its numerical, you DO need quotes for data to be inserted - imagine trying to insert a string not wrapped in quotes. How would MySQL parse that sql without them?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum