I am making a database validation system. Upon validation, the handler is supposed to send the data to an email address, while simultaneously updating the data to another database. The values of id, name, board, query and rand are received from the previous page.
Now, with the given code, I am unable to update the table named 'answers' within my database. The line of code for this functionality is in bold.
<?php
$servername = "localhost";
$username = "root";
$password = '';
$dbname = "bsp";
$conn1 = new mysqli($servername, $username, $password, $dbname);
$id="";
$value = "";
$name = "";
$board = "";
$query="";
$id = $_POST['id'];
$name = $_POST['name'];
$board = $_POST['board'];
$query = $_POST['query'];
$rand = $_POST['rand'];
$value=$_POST['option'];
$to="";
$subject="";
$msg="";
$headers="";
if($value=='delete')
{
$sql="DELETE FROM rti WHERE ID=$id";
if ($conn1->query($sql) === TRUE) {
echo "Record deleted successfully";
} else {
echo "Error deleting record: " . $conn1->error;
}
}
else if($value=='validate')
{
**$sql="INSERT INTO answers (name, board, query, rand) VALUES (\"$name\", \"$board\", \"$query\", \"$rand\")";**
if($board=='BRCA')
{
$to = 'email#gmail.com';
$subject = 'RTI Query: Do not reply to this email.';
$msg = "Your code is " . $rand . ".";
$msg .= " Visit localhost/BSP/landing.php and post the code, and subsequently your answer.";
$headers = 'From: contact#yoursite.com';
mail($to, $subject, $msg, $headers);
}
else if($board=='BSW')
{
$to = 'email#rediffmail.com';
$subject = 'RTI Query: Do not reply to this email.';
$msg = $query;
$headers = 'From: contact#yoursite.com';
mail($to, $subject, $msg, $headers);
}
else if($board=='BSA')
{
$to = 'email#iitd.ac.in';
$subject = 'RTI Query: Do not reply to this email.';
$msg = $query;
$headers = 'From: contact#yoursite.com';
mail($to, $subject, $msg, $headers);
}
else if($board=='BSP')
{
$to = 'email#gmail.com';
$subject = 'RTI Query: Do not reply to this email.';
$msg = $query;
$headers = 'From: contact#yoursite.com';
mail($to, $subject, $msg, $headers);
}
else if($board=='BHM')
{
$to = 'email#gmail.com';
$subject = 'RTI Query: Do not reply to this email.';
$msg = $query;
$headers = 'From: contact#yoursite.com';
mail($to, $subject, $msg, $headers);
}
}
if(mail($to, $subject, $msg, $headers)==TRUE)
{
echo "The mail was sent successfully.";
}
$conn1->close();
?>
Related
I cant seem to find a solution to this on the site. What I would like to do is to have the user click on a button. This will then send a mail to me or someone in the office and the mail would then contain the logged in user details. I am sending the mail successfully, but I am unsure on how to get the code to send me the users info. Here is my code:
include 'contact/config.php';
require_once 'class.user.php';
$user_home = new USER();
if(!$user_home->is_logged_in())
{
$user_home->redirect('index.php');
}
$stmt = $user_home->runQuery("SELECT * FROM tbl_client_info WHERE UCODE=:uid");
$stmt->execute(array(":uid"=>$_SESSION['userSession']));
$row = $stmt->fetch(PDO::FETCH_ASSOC);
error_reporting (E_ALL ^ E_NOTICE);
$post = (!empty($_POST)) ? true : false;
if($post)
{
$name = $row['firstname'];
$email = $row['billingemail'];
$subject = "myDraadloze Package Contact Card";
$message = stripslashes($_POST['Please contact user regarding example']);
$error = '';
if(!$error)
{
$mail = mail(WEBMASTER_EMAIL, $subject, $message,
"From: ".$name." <".$email.">\r\n"
."Reply-To: ".$email."\r\n"
."X-Mailer: PHP/" . phpversion());
if($mail)
{
echo 'OK';
}
}
}
?>
Any help would really be appreciated.
Thanks
include 'contact/config.php';
require_once 'class.user.php';
$user_home = new USER();
if(!$user_home->is_logged_in())
{
$user_home->redirect('index.php');
}
else
{
$stmt = $user_home->runQuery("SELECT * FROM tbl_client_info WHERE UCODE=:uid");
$stmt->execute(array(":uid"=>$_SESSION['userSession']));
$row = $stmt->fetch(PDO::FETCH_ASSOC);
error_reporting (E_ALL ^ E_NOTICE);
$post = (!empty($_POST)) ? true : false;
if($post)
{
$name = $row['firstname'];
$email = $row['billingemail'];
$subject = "myDraadloze Package Contact Card";
$email_to = " email#gmail.com";
$message = .= "Firstname: ".($name)."\n\n";
$message = .= "Email: ".($email)."\n\n";
$message = .= "Firstname: ".($name)."\n\n";
$error = '';
if(!$error)
{
$headers = 'From: your Website/email'."\r\n".
'Reply-To: Email.#ste.com'."\r\n" .
'X-Mailer: PHP/' . phpversion();
if(#mail($email_to, $subject, $message, $headers))
{
echo 'OK';
}
}}
?>
Hope this helps!
the reply-to and from format doesnt look right. I would just create a variable for them and then stick it in the code.
$name = $row['firstname'];
$email = $row['billingemail'];
$subject = "myDraadloze Package Contact Card";
$message = stripslashes($_POST['Please contact user regarding example']);
$error = '';
$replyemail="'Reply-To: ".$name."<".$email.">'"."\r\n"; //looks likes this 'Reply-to: customer name <customeremail#whatever.com'\r\n
$fromemail="'from: ".$name."<".$email.">'"."\r\n"; //looks likes this 'from: customer name <customeremail#whatever.com'\r\n
//yours looked like this
//from:customername <customeremail#whatever.com\r\n
//these are headers and need to be seperated with '
//the ' should be at start and before \r\n
if(!$error)
{
$mail = mail(WEBMASTER_EMAIL, $subject, $message,$fromemail.$replyemail
."X-Mailer: PHP/" . phpversion());
Let say I have a class, for example:
class User{
function callname(){
$user = $_SESSION['id'];
$query = ("SELECT * FROM user WHERE user.id='$user'");
while ($result=mysql_fetch_array($query)){
echo ($result['username']);}}}
And then, make instance for User object:
$user = new User;
Code to send mail:
if($_SERVER["REQUEST_METHOD"] == "POST"){
$username = trim($_POST['username']);
$check = mysql_num_rows(mysql_query("SELECT * FROM user WHERE username='$username'"));
if ($check==TRUE){
$name = $user->callname();
$to = "myemail#domain.com";
$subject = "Example Subject";
$headers = "From: My Domain".'\r\n'.
"MIME-Version: 1.0".'\r\n'.
"Content-Type: text/html; charset=ISO-8859-1".'\r\n'.
'X-Mailer: PHP/' . phpversion();
$message = "Hai $name, this is the new message.";
mail($to, $subject, $message, $headers);
} else {
?>
<script type="text/javascript">
alert("Sorry, username not exist !");
</script>
<?php
}
}
Mail function was working correctly and I have received an email too. The problem is
$name didn't print the name of user in the email. I've tried this $name = $user->callname(); on different page without the if() and it was working.
\r\nMIME-Version: 1.0 and so on was print in the From header.
I think you need to return $result['username'] instead of echoing it.
For special characters be read as such you need them in double quotes (").
A attempt to correct your code:
class User{
function callname(){
$user = $_SESSION['id'];
$query = mysql_query("SELECT * FROM user WHERE user.id='$user'");
while ($result=mysql_fetch_array($query)){
return $result['username'];}}}
Next part:
if($_SERVER["REQUEST_METHOD"] == "POST"){
$username = mysql_real_escape_string(trim($_POST['username']));
$check = mysql_num_rows(mysql_query("SELECT * FROM user WHERE username='$username'"));
if ($check==TRUE){
$name = $user->callname();
$to = "myemail#domain.com";
$subject = "Example Subject";
$headers = "From: My Domain"."\r\n".
"MIME-Version: 1.0"."\r\n".
"Content-Type: text/html; charset=ISO-8859-1"."\r\n".
'X-Mailer: PHP/' . phpversion();
$message = "Hai $name, this is the new message.";
mail($to, $subject, $message, $headers);
} else {
?>
<script type="text/javascript">
alert("Sorry, username not exist !");
</script>
<?php
}
Stuck on a simple php mail thing. Can anybody spot where I've gone wrong. Would be happy for any help.
<?php
$to = "example#website.co.uk";
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
mail($name, $email, $subject, $message);
if(mail($name, $email, $subject, $message)) {
echo "E-Mail Sent";
} else {
echo "There was a problem";
}
?>
The first comment pretty much said it all, if you need a beginner-friendly tutorial of the mail() function, you should check out PHP Sending E-mails
<?php
$to = "example#website.co.uk";
$name = 'From:'.$_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
if(mail($to, $subject, $message, $name)) {
echo "E-Mail Sent";
} else {
echo "There was a problem";
}
?>
Try this instead, it would need spam proofing etc...
<?php
$to = 'example#example.com';
$subject = $_POST['subject'];
$message = $_POST['message'];
$headers = 'From: contact#yoursite.com';
mail($to, $subject, $message, $headers);
?>
<?php
$m = "";
$to = "anymail#gmail.com";
$from = "anymail#mydomainname.com";
$subject = 'subject';
$message = "Any Message";
$headers = "From: $from\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-type:text/html; charset=iso-8859-1\n";
if ( mail($to, $subject, $message, $headers) ) {
$m = "Email sent successfully"; exit();
}else {
$m = "Email can't be sent.";exit();
}
exit();
?>
<!DOCTYPE HTML><html>
<head><meta charset="utf-8"><title>Untitled Document</title></head>
<body>
<h2>
<?php
echo $m;
?> </h2>
</body></html>
I am unable to get confirmation. Could anyone point out the problem?
use this code, its the exit() which stops the execution and not printing value of $m
if ( mail($to, $subject, $message, $headers) ) {
$m = "Email sent successfully";
//exit();
}else {
$m = "Email can't be sent.";
//exit();
}
//exit();
How do I get the phone number parameter, which I added, into the body of the email sent by this php script?:
$post = (!empty($_POST)) ? true : false;
if($post)
{
$name = stripslashes($_POST['name']);
$email = trim($_POST['email']);
$phone = trim($_POST['phone']);
$subject = trim($_POST['subject']);
$message = stripslashes($_POST['message']);
$error = '';
// Check name
if(!$name)
$error .= 'Name required! ';
// Check email
if(!$email)
$error .= 'E-mail required! ';
if($email && !ValidateEmail($email))
$error .= 'E-mail address is not valid! ';
// Check phone
if(!$phone)
$error .= 'Phone number required! ';
// Check message
if(!$message)
$error .= "Please enter your message!";
if(!$error)
{
$mail = mail(WEBMASTER_EMAIL, $subject, $message,
"From: ".$name." <".$email.">\r\n"
."Reply-To: ".$email."\r\n"
."X-Mailer: PHP/" . phpversion());
if($mail)
echo 'OK';
}
else
echo '<div class="errormsg">'.$error.'</div>';
}
I've added $phone to the $post section, created the feild in my form, now I just need to get the contents returned in the email that is recieved. Any tips greatly appreciated.
If I understood correctly what you want, you need something like that:
$message = "Phone: $phone\r\n" . $message;
before
$mail = mail(WEBMASTER_EMAIL, $subject, $message,
"From: ".$name." <".$email.">\r\n"
."Reply-To: ".$email."\r\n"
."X-Mailer: PHP/" . phpversion());