I want to store that value in a database after computing the domain. I have done everything right as per my knowledge but data is not being sent to the table.
Note : My mysql server is in AWS and I've opened the mysql port
My code is below,
<?php
$con = mysqli_connect("localhost","user_name","passwd","db_name");
$sql = "INSERT INTO contact_form (name, mobile, email, message) VALUES ('".$_POST['name']."', '".$_POST['phone']."', '".$_POST['email']."', '".$_POST['message']."')";
if (mysqli_query($con, $sql)) {
echo "New record created successfully";
$to = 'contact#xxx.com';
$subject = 'Contact Form';
$from = 'contact#xxx.com';
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Create email headers
$headers .= 'From: '.$from."\r\n".
'Reply-To: '.$from."\r\n" .
'X-Mailer: PHP/' . phpversion();
// Compose a simple HTML email message
$message = '<html><body>';
$message .= '<h1 style="color:#f40;">Hi!</h1>';
$message .= '<p>Name :'. $_POST['name'] .' </p>';
$message .= '<p>Contact Number :'.$_POST['phone'] .'</p>';$message .= '<p>Email :'.$_POST['email'] .'</p>';
$message .= '<p>Message :'.$_POST['message'] .'</p>';
$message .= '</body></html>';
// Sending email
mail($to, $subject, $message, $headers);
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($con);
}
mysqli_close($con);
?>
Please help me out. I'm not good in mysql. Thanks in advance !!
Ahamed, first of all you need to check if you have set your $_POST and it is not empty. Then you need to assign this data to custom variables.
I hope it helps.
$con = mysqli_connect("localhost","user_name","passwd","db_name");
if (isset($_POST['name']) && isset($_POST['phone']) && isset($_POST['email']) && isset($_POST['message'])){
$username = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$message = $_POST['message'];
$sql = "INSERT INTO contact_form (name, mobile, email, message) VALUES ('$username', '$phone', '$email', '$message')";
$result = $con->query($sql);
}
Related
I want to send a mail using php through Gmail. I've seen many websites how to do it but I don't Understand how to do them. I am still stuck at the code I've written at first. Please help me how to connect my website (hosted on GoDaddy) to gmail account, and tell correct path to do that.
I am writing a code for clearer vision. In this php code, I am trying to add data in database and if the code works, the user should get an email from the company
if (isset($_POST['name']) && isset($_POST['mobile']) && isset($_POST['email']) && isset($_POST['practice']) && isset($_POST['date']) && isset($_POST['time']))
{
include "_credentials.php";
$name = $_POST['name'];
$mobile = $_POST['mobile'];
$email = $_POST['email'];
$practice = $_POST['practice'];
$date = $_POST['date'];
$time = $_POST['time'];
$sql = "INSERT INTO <database> (Name, Number, email, Practice, Time, Date) VALUES ('".$name."', ".$mobile.",'".$email."','".$practice."','".$date."','".$time."');";
if ($conn->query($sql) === TRUE) {
$to = $email;
$host = "ssl://smtp.gmail.com";
$subject = "Appointment Confirmation";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: abc#gmail.com' . "\r\n";
$txt = "Hello ".$name.",\n Your Appointment is booked with Doctor at Clinic on ".$date." at ".$time.".";
mail($to,$subject,$message,$headers);
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
}
First of all you need need to check for error response on mail() function using the following snippet
$success = mail('example#example.com', 'My Subject', $message);
if (!$success) {
$errorMessage = error_get_last()['message'];
}
After the error message , you will be able to sort it out
I have tried sending email from a basic html form by php mail() function and It has given me false return
Code:
<?php
if(isset($_POST['submit'])){
$title = $_POST['title'];
$name = $_POST['first_name'];
$name1 = $_POST['last_name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$company=$_POST['lead_object'];
$skype= $_POST['skype_id'];
$to = 'himanshu#webkidukan.com';
$subject = 'Advertiser Form Details';
$from = 'ssing648#gmail.com';
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Create email headers
$headers .= 'From: '.$from."\r\n".
'Reply-To: '.$from."\r\n" .
'X-Mailer: PHP/' . phpversion();
// Compose a simple HTML email message
$message = '<html><body>';
$message .= '<h1 style="color:#f40;">Hi Sir</h1>';
$message .= '<p style="color:#080;font-size:18px;">Details of Advertiser Form</p>';
$message .= '<p style="color:#080;font-size:18px;"><?php $title . " " . $name . " " . "?>wrote the following:"<?php . "\n\n" . $name . "\n\n" . $name1 . "\n\n" .$email . "\n\n" .$phone . "\n\n" .$company. "\n\n" .$skype. "\n\n"?> </p>';
$message .= '</body></html>';
$t = mail($to, $from, $message, $headers,$subject);
var_dump($t);exit;
if ($t) {
echo "Mail Sent. Thank you " . $name . " .We will contact you shortly.";
}else{
echo "Failed to send email. Please try again later";
}
echo "<script type='text/javascript'>alert('Thanks for filling out our form! We will look over your message and get back to you soon.')</script>";
echo "<script> window.location.href = 'advertiser.php';</script>";
}
?>
I have this issue in three forms together , hence posting one for the understanding the mistake, that I am doing. Can anyone of you help me with the same.Or should I go for the SMTP mail option.Also I am sending the form details in mail to the user.So also check that the way to send the flyer is right or not.
There are some unwanted PHP tags while appending message. try like this
$message = '<html><body>';
$message .= '<h1 style="color:#f40;">Hi Sir</h1>';
$message .= '<p style="color:#080;font-size:18px;">Details of Advertiser Form</p>';
$message .= '<p style="color:#080;font-size:18px;">'.$title." ".$name." wrote the following:"."\n\n".$name."\n\n".$name1."\n\n".$email."\n\n".$phone."\n\n".$company."\n\n".$skype."\n\n".'</p>';
$message .= '</body></html>';
Kindly seeking your help with the below code:
if( !$error ) {
move_uploaded_file($file_loc,$folder.$file);
$cuser = $userRow['userName'];
$res = mysql_query("SELECT * FROM users WHERE userId=".$_SESSION['user']);
$userRow = mysql_fetch_array($res);
$query = "INSERT INTO postoffers(postedby,reqName,reqEmail,reqHotel,reqOutlet,reqCnum,reqPostType,reqPostHead,reqPostDet,offerStarts,offerEnds,file,type,size) VALUES('$cuser','$rname','$remail','$rhotel','$routlet','$rcnum','$rposttype','$rposthead','$rpostdet','$rbdate','$redate','$file','$file_type','$file_size')";
$res = mysql_query($query);
if ($res) {
$errTyp = "success";
$errMSG = "Successfully Posted!";
unset($rname);
unset($remail);
unset($rhotel);
unset($routlet);
unset($rcnum);
unset($rposttype);
unset($rposthead);
unset($rpostdet);
unset($rbdate);
unset($redate);
unset($file);
$to = ;
$subject = "Your Post Offer";
$message = "Thank you " . $cuser . " for the Post Request.";
$headers = 'From: AFP Webmaster' . "\r\n" .
$headers = "MIME-Version: 1.0" . "\r\n" .
$headers = "Content-type:text/html;charset=iso-8859-1" . "\r\n" .
'Reply-To: AFP Webmaster' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
}
else {
$errTyp = "danger";
$errMSG = "Something went wrong, try again later...";
}
}
The code WORKS PERFECTLY on the following:
1. Storing the value on DB.
2. If i place a valid email like "name#gmail.com" in $to
What I want to happen now is assigning the value of the "reqEmail" from the database or the value from the textfield "remail" to $to.
So the email will go to the email who filled up the form.
i hope i made myself clear and understandable.
Thanks!
You're clearing your variables before sending the email, so PHP doesn't know where to send the email to. You need to place your "unset" functions after the PHP Mailer call. Try this:
if (!$error) {
move_uploaded_file($file_loc, $folder.$file);
$cuser = $userRow['userName'];
$res = mysql_query("SELECT * FROM users WHERE userId=".$_SESSION['user']);
$userRow = mysql_fetch_array($res);
$query = "INSERT INTO postoffers(postedby,reqName,reqEmail,reqHotel,reqOutlet,reqCnum,reqPostType,reqPostHead,reqPostDet,offerStarts,offerEnds,file,type,size) VALUES('$cuser','$rname','$remail','$rhotel','$routlet','$rcnum','$rposttype','$rposthead','$rpostdet','$rbdate','$redate','$file','$file_type','$file_size')";
$res = mysql_query($query);
if ($res) {
$to = ;
$subject = "Your Post Offer";
$message = "Thank you " . $cuser . " for the Post Request.";
$headers = 'From: AFP Webmaster' . "\r\n".
$headers .= "MIME-Version: 1.0" . "\r\n".
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n" . 'Reply-To: AFP Webmaster' . "\r\n" . 'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
$errTyp = "success";
$errMSG = "Successfully Posted!";
unset($rname);
unset($remail);
unset($rhotel);
unset($routlet);
unset($rcnum);
unset($rposttype);
unset($rposthead);
unset($rpostdet);
unset($rbdate);
unset($redate);
unset($file);
} else {
$errTyp = "danger";
$errMSG = "Something went wrong, try again later...";
}
}
Using the following php file to send mail.
But the text that is not English inside the mail body get as ???? and the $subject of the mail get as "‚º ëÃ"
what do I need to check/add/change in order to fix it?
<?php include('config.php');
header('Content-Type: text/html; charset=utf-8');
class messenger
{
public $name;
public $number;
public $email;
public $address;
public $category;
public $tool;
/*----------user registration start--------------*/
function signup()
{
$name = $_REQUEST['name'];
$number = $_REQUEST['number'];
$email = $_REQUEST['email'];
$address = $_REQUEST['address'];
$category = $_REQUEST['category'];
$tool = $_REQUEST['tool'];
$datetime = date("Y-m-d H:i:s");
//Generate the email message:
$to = 'someEmailAddress#gmail.com';
$subject = "הזמנת חדשה";
$msg = "In time: " . $datetime . "\n New Power Tool Rental: \n\n Name: " . $name . "\r\n Email: " . $email . "\n Mobile Number: " . $number . "\n Address: " . $address . "\n Category: " . $category . "\n Tool: " . $tool . "\n\n Regards\n " . $name . " \n " . $email;
$headers = "From: $email" . "\r\n";
$headers .= "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/plain; charset=UTF-8";
$headers .= "X-Mailer: PHP/" . phpversion();
//Send the email
mail($to, $subject, $msg, $headers);
//Enter data to the SQL table
$sql = "INSERT INTO user (name, number, email, address, category, tool, dateTime)
VALUES ('$name', '$number', '$email', '$address', '$category', '$tool','$datetime')";
//Check if entered to SQL and if Yes send back in json message
$result = mysql_query($sql);
if (!$result) {
$message["result"] = "Problemmm";
} else {
$mysql_insert_id = mysql_insert_id(); //get the new ID number from sql table
$message["id"] = $mysql_insert_id; //add the new ID data to the response message
$message["name"] = $_REQUEST['name'];
$message["number"] = $_REQUEST['number'];
$message["email"] = $_REQUEST['email'];
$message["address"] = $_REQUEST['address'];
$message["result"] = "successfully";
}
//Send back the json message
echo json_encode($message);
die;
}
/*----------user registration end--------------*/
}
?>
just tried to change the subject to:
$subject = "הזמנת כלי להשכרה";
$encSubject = '=?UTF-8?B?'.base64_encode($subject).'?=';
now I get the subject of the mail as:
���ú ��é ��
Setting the headers is not enough.
Both subject (must be RFC 2047) and
message (must be UTF-8 in your case) have also be in their respecive correct encoding, the php::mail doc + user-notes covers most of what you miss.
I am a beginner in php.
I am trying php to send mail.
My mail goes to mail client but it does not show the header part well as I wanted.
I am using the following codes ----
<?php
//validation expected data exists
if(!isset($_POST["name"]) ||
!isset($_POST["city"]) ||
!isset($_POST["email"]) ||
!isset($_POST["phone"]) ||
!isset($_POST["message"])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
//Subject
$name = $_POST["name"];
$subject = "NO REPLY";
$email = $_POST["city"];
$phone = $_POST["email"];
$website = $_POST["phone"];
$message = $_POST["message"];
$header = "from: $name <$email>";
$to = 'info#mishmihillsadventure.in';
$send_contact=mail($to,$subject,$message,$header);
//Check, if message sent to your email
// Display message "We've recived your information"
if($send_contact){
echo "We've received your contact information";
}
else{
echo "ERROR";
}
?>
$email = $_POST["city"];
$phone = $_POST["email"];
Is this really what you want? Shouldn't it be:
$email = $_POST["email"];
And try the following headers:
$header = 'From: ' . $name . '<' . $email . '>' . "\r\n";
Use (atmost) following headers while sending mail via PHP -
$header = "MIME-Version: 1.0" . "\r\n";
$header .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$header .= "From: $name <$email>" . "\r\n";
//If want to `CC` someone add
$header .= 'Cc: abc#email.com' . "\r\n";
Using variables in Double quotes is fine.
You can try something like in code mentioned below,
<?php
$to = 'test#to.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: test#from.com' . "\r\n" .
'Reply-To: test#from.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
if(mail($to, $subject, $message, $headers)) {
echo 'Msg send..';
} else {
echo 'Msg Not send..';
}
?>