I just started learning php not long ago, and I am currently building a "contact us" form which will send the user input to my email. I have been on this for days thinking I will figure it out, but I am not getting it. I wanna also be able to receive the user's input in my email and also be able to detect the user's IP address. When I submitted the form, I received every other input but the IP address though I used "localhost".
I tried with the <input type="hidden" name="message" value="<?php echo $_SERVER['REMOTE_ADDR']; ?>"> but I read online that it's better to do it without the <input type="hidden"> and just process everything in the form processor script. Please kindly help me with this.
<?php
$emailError = "";
$messageError = "";
function getUserIp(){
$client = $_SERVER['HTTP_CLIENT_IP'];
$forward = $_SERVER['HTTP_X_FORWARD_FOR'];
$remote = $_SERVER['REMOTE_ADDR'];
if(filter_var($client, FILTER_VALIDATE_IP)) {
$ip = $client;
}elseif(filter_var($forward, FILTER_VALIDATE_IP)) {
$ip = $client;
}else{
$ip = $remote;
}
return $ip;
}
if(isset($_POST['submit'])){
//declares variable
$email = $_POST["email"];
$subject = $_POST["subject"];
$message = $_POST["message"];
if(empty($_POST['email'])){
$emailError = "Please enter your email";
}
if(empty($_POST['subject'])){
$subjectError = "Please enter a subject?";
}
}
if(!empty($_POST['email']) && !empty($_POST['subject'])){
// Send the email
$to = "you#yourdomain.com";
$email = "From: $email";
$subject = "Subject: $subject";
$message = "$message" . "\n\n\n==- Sent from the website with IP Address: " . $ip . " -==";;
$headers = "From: $email,";
$send_contact=mail($to,$email,$subject,$message,$headers);
header("Location: domain");
}
?>
change below section --
if(!empty($_POST['email']) && !empty($_POST['subject'])){
// Send the email
$to = "you#yourdomain.com";
$ip =getUserIp();
$email = "From: $email";
$subject = "Subject: $subject";
$message = "$message" . "\n\n\n==- Sent from the website with IP Address: " . $ip . " -==";;
$headers = "From: $email,";
$send_contact=mail($to,$email,$subject,$message,$headers);
header("Location: domain");
}
Related
The form only works if the user submitting the form is using a .com email address. If the user is using a .com address I receive an email from them containing their form information (content). But, if anyone with another email address for example .co.uk submits the form, it does not send an email to myself (billy#hotmail.co.uk), please help. The enquiry.php code is below:
<?php
$email_to = "billy#hotmail.com";
$firstname = $_POST["firstname"];
$surname = $_POST["surname"];
$telephone = $_POST["telephone"];
$email = $_POST["email"];
$time = $_POST["time"];
if (empty($firstname)) {
show_error("Please fill in your Name - hit back in the browser to correct");
}
if (empty($surname)) {
show_error("Please fill in your Surname - hit back in the browser to correct");
}
if (empty($email)) {
show_error("Please fill in your Email Address - hit back in the browser to correct");
}
if (empty($telephone)) {
show_error("Please fill in your Telephone Number - hit back in the browser to correct");
}
if (empty($time)) {
show_error("Please select a collection time slot - hit back in the browser to correct");
}
$email = htmlspecialchars($_POST['email']);
if (!preg_match("/([\w\-]+\#[\w\-]+\.[\w\-]+)/", $email)) {
show_error("E-mail address not valid");
}
$email_from = $_POST["email"];
$message = $_POST["message"];
$email_subject = "Easter 2018 Order Form";
$headers =
"From: $email_from .\n";
"Reply-To: $email_from .\n";
$message =
"First Name: ". $firstname .
"\r\nSurname: " . $surname .
"\r\nTelephone Number: " . $telephone .
"\r\nEmail Address: " . $email .
"\r\nTime Slot: " . $time .
ini_set("sendmail_from", $email_from);
$sent = mail($email_to, $email_subject, $message, $headers, "-f"
.$email_from);
if ($sent) {
header("location:
http://www.billyfarroll.co.uk/thank-you.html");
} else {
echo "There has been an error sending your comments. Please try later.";
}
function check_input($data, $problem='') {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0) {
show_error($problem);
}
return $data;
}
function show_error($myError) {
?>
OK the problem is with your regex.
The thing is that when you use this regex
([\w\-]+\#[\w\-]+\.[\w\-]+)
it will match a result on these kind of address mail
billy#hotmail.co.uk
but the matched result will be
billy#hotmail.co
because you're askin for a_word#a_word.a_word
So the email address is valid according to your code, but isn't existing
You have to recheck your regex.
Maybe try
("/.*#.*\..*/")
<?php
if(isset($_POST['submit']))
{
$email=$_POST['email'];
$comment=$_POST['comment'];
$captcha=$_POST['g-recaptcha-response'];
if(!$captcha)
{
echo 'Please check the the captcha form.';
}
$response=file_get_contents("https://www.google.com/recaptcha/api/sitev erify?secret="secretkey" &response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']);
if($response.success==false)
{
header('Location:http://mywebsite.com/.errordocs/404.html');
}
else
{
header('Location:http://mywebsite.com/thankyou.php');
}
}
if (isset ($_POST['Side_Form'])){
$name = $_POST['name'];
$last = $_POST['last'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$comments = $_POST['comments'];
// Build the email (replace the address in the $to section with Enjoy's contact e-mail)
$to = 'somebody#somewhere.com';
$subject = "Main Contact Form";
$message = "Name: $name
Email: $email
Phone: $phone
Comments: $comments";
$headers = "From: $email \r\n";
mail($to, $subject, $message, $headers);
}
?>
The problem is when the recaptcha is verified on the form, it displays the checkmark, user can click submit, but I get a blank screen and email doesn't send. Any help is Greatly appreciated, have been working on this for about 3 weeks.
your currently redirecting before actually sending the email. here is your code restructured. also fixed the recaptcha response processing
<?php
if (isset($_POST['submit'])){
$email = $_POST['email'];
$comment = $_POST['comment'];
$captcha = $_POST['g-recaptcha-response'];
if (! $captcha){
echo 'Please check the the captcha form.';
exit();
}else{
$response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=" . $secretkey . "&response=" . $captcha . "&remoteip=" . $_SERVER['REMOTE_ADDR']);
$responseKeys = json_decode($response,true);
if (intval($responseKeys["success"]) !== 1){
header('Location:http://mywebsite.com/.errordocs/404.html');
exit();
}else{
if (isset($_POST['Side_Form'])){//not sure what this is, hopefully you do :-)
$name = $_POST['name'];
$last = $_POST['last'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$comments = $_POST['comments'];
// Build the email (replace the address in the $to section with Enjoy's contact e-mail)
$to = 'somebody#somewhere.com';
$subject = "Main Contact Form";
$message = "Name: $name
Email: $email
Phone: $phone
Comments: $comments";
$headers = "From: $email \r\n";
mail($to,$subject,$message,$headers);
header('Location:http://mywebsite.com/thankyou.php');
exit();
}
}
}
}
?>
I need to send an email to a new user and myself when they register an account. I need to know how to it send through my admin email instead of the weird email address that the GoDaddy server uses to send it.
Here's my PHP code:
<?php
require_once('recaptchalib.php');
$privatekey = "privatekey";
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
if (!$resp->is_valid) {
die ("reCAPTCHA was wrong, try again!");
} else {
mysql_connect("host", "username", "password");
mysql_select_db("database");
$hash = sha1(rand (0,1000) );
$name = $_POST['name'];
$bmonth = $_POST['month'];
$bday = $_POST['day'];
$byear = $_POST['year'];
$sq = $_POST['security_q'];
$sq_ans = sha1($_POST['security_q_ans']);
$email = $_POST['email'];
$pass = sha1($_POST['pass']);
$insert_query = "INSERT INTO users (account_act_hash, name, bmonth, bday, byear, securityq, securityq_ans, email, password) VALUES ('$hash','$name','$bmonth','$bday','$byear','$sq','$sq_ans','$email','$pass')";
$insertion_result = mysql_query($insert_query);
if($insertion_result) {
$to = "my admin email";
$subject = "New account created";
$name = $_POST['name'];
$bmonth = $_POST['month'];
$bday = $_POST['day'];
$byear = $_POST['year'];
$email = $_POST['email'];
$ip = $_SERVER['REMOTE_ADDR'];
$message = 'EMAIL
Here is the account info.
Name: $name
Birthday: $bmonth-$bday-$byear
Email: $email
Registration IP Address: $ip
'
$header = "A new account has been created.";
}
if($_POST){
mail($to, $subject, $message, $header);
}
}
?>
<?php
$to2 = $_POST['email'];
$subject2 = "Activate Your Account";
$email2 = $_POST['email'];
$message2 = '
Thanks for registering an account!
Your account has been created and can be used when you activate your account by clicking the below link!
------------------------------------------------------------------------
Email: '.$email2.'
------------------------------------------------------------------------
Please click this link to activate your account:
https://www.mysite.com/activation.php?email='.$email2.'&account_act_hash='.$hash.'
';
if($_POST) {
mail($to2, $subject2, $message2);
}
?>
Directly from the mail doc page you need to specify from in the header:
// Additional headers
$headers .= 'From: Birthday Reminder <birthday#example.com>' . "\r\n";
You are getting a _ weird email address_ because you have not specified who the email should be coming from.
You have a bunch of additional problems:
HEREDOC
I'm assuming this is what you were trying to do
$message = <<<EMAIL
Here is the account info.
Name: $name
Birthday: $bmonth-$bday-$byear
Email: $email
Registration IP Address: $ip
EMAIL;
MySQL
mysql_* functions are deprecated, read the read box.
This will also lead to don't trust user input, prepared statements are going to be your friend.
Misc
When using double quotes, use curly braces with your variables:
$foo = 'test';
echo "This a {$foo}!"; // This is a test!
In this case die(), is a very harsh way to end a script in terms of user experience.
I have put together a form with validation with help from this tutorial: http://www.w3resource.com/php/form/php-form-validation.php
I am hoping somebody can point out where I have gone wrong? As my validation works, I get a thankyou message but no mail comes through to my inbox :(
<?php
if (isset($_POST['submit'])) {
//checking name
if(empty($_POST['full_name']))
$msg_name = "*";
$name_subject = $_POST['full_name'];
//check phone number
if(empty($_POST['Phone_Num']))
$msg_Phone = "*";
$email_subject = $_POST['Phone_Num'];
//check email
if(empty($_POST['email_addr']))
$msg_email = "*";
$email_subject = $_POST['email_addr'];
$email_pattern = '/^\w+([\.-]?\w+)*#\w+([\.-]?\w+)*(\.\w{2,3})+$/';
preg_match($email_pattern, $email_subject, $email_matches);
if(!$email_matches[0])
$msg2_email = "Please enter a valid email address";
}
// validation complete
if(isset($_POST['submit'])){
if($msg_name=="" && $msg2_name=="" && $msg_email=="" && $msg2_email=="" && $msg2_Message=="")
$msg_success = "Thankyou for your enquiry";
//send mail
$to = "someone#email.com";
$subject = "Data collected thorugh from";
$message = "<p>".$_POST["full_name"]."</p><p>".$_POST["Phone_Num"]." </p><p>".$_POST["email_addr"]."</p><p>".$_POST["Message"]."</p>";
$from = "someone#email.com";
mail($to,$subject,$message);
}
?>
The PHP mail() function does not allow for sending email through SMTP with authentification. If you want do this, there is a PEAR package.
This article gives some explanation.
I have the below PHP contact form that has a CAPTCHA code to ensure is correct. However, when I reply to the email from the website it puts a random email which i believe is the server admin, however, I want it to be the persons email who sent the form in. below is the code, could you possibly be able to help me?
<?php session_start();
if(isset($_POST['Submit'])) { if( $_SESSION['chapcha_code'] == $_POST['chapcha_code'] && !empty ($_SESSION['chapcha_code'] ) ) {
$youremail = 'info#example.com';
$fromsubject = 'www.example.co.uk';
$title = $_POST['title'];
$fname = $_POST['fname'];
$mail = $_POST['mail'];
$phone = $_POST['phone'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$to = $youremail;
$mailsubject = 'Message from Website'.$fromsubject.' Contact Page';
$body = $fromsubject.'
The person that contacted you is: '.$fname.'
Phone Number: '.$phone.'
E-mail: '.$mail.'
Subject: '.$subject.'
Message:
'.$message.'
|---------END MESSAGE----------|';
echo "Thank you for your message. I will contact you shortly if needed.<br/>Go to <a href='/index.html'>Home Page</a>";
mail($to, $subject, $body);
unset($_SESSION['chapcha_code']);
} else {
echo 'Sorry, you have provided an invalid security code';
}
} else {
echo "You must write a message. </br> Please go to <a href='/contact.html'>Contact Page</a>";
}
?>
You'll need some headers so the from address is the users mail.
Also refer to the mail docs
try this
$headers = "From: $mail\r\n";
$headers .= "Reply-To: $mail\r\n";
mail($to, $subject,$body,$headers);