I have a PHP mail form from a Template that sends the email to me, but it appears all the variables are blank.
<?php
header('Content-type: application/json');
$status = array(
'type'=>'success',
'message'=>'Email sent!'
);
$name = #trim(stripslashes($_POST['yourname']));
$email = #trim(stripslashes($_POST['youremail']));
$subject = #trim(stripslashes($_POST['yoursubject']));
$message = #trim(stripslashes($_POST['yourmessage']));
$email_from = $email;
$email_to = 'kylef33#gmail.com';
$body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Message: ' . $message;
$success = #mail($email_to, $subject, $body, 'From: <'.$email_from.'>');
echo json_encode($status);
die;
?>
And here, the HTML:
<div class="col-sm-6">
<h1>
Contact Form
</h1>
<p>
Fill out the form to enquire directly and we will get back to you as soon as possible.
</p>
<div class="status alert alert-success" style="display: none">
</div>
<form id="main-contact-form" class="contact-form" name="contact-form" method="post" action="sendemail.php" role="form">
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<input name="yourname" type="text" class="form-control" required="required" placeholder="Name">
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<input name="youremail" type="text" class="form-control" required="required" placeholder="Email address">
</div>
</div>
</div>
<div class="row">
<div class="col-sm-12">
<div class="form-group">
<textarea name="yourmessage" id="message" required class="form-control" rows="8" placeholder="Message"></textarea>
</div>
<div class="form-group">
<button type="submit" class="btn btn-danger btn-lg">Send Message</button>
<input type="hidden" name="yoursubject" value="Enquiry">
</div>
</div>
</div>
</form>
</div>
But the resulting email is:
Name:
Email:
Subject:
Message:
With none of the responses. Where am I going wrong?
Edit:
If I set the variables manually in the php file the message comes through fine:
$name = 'John';
$email = 'email#email.com;
$subject = 'Enquiry Form';
$message = 'Message here.';
I think the php file isn't getting the variables from the form correctly. How do I fix this?
I don't see the header of the email. try to add this:
$headers = "MIME-Version: 1.0\r\nFrom: $noReplay\r\nReply-To: $noReplay\r\nContent-Type: text/html; charset=utf-8";
where $noReplay is the email address to show to the receiver and the header is sent as a last parameter in mail() function.
Hope this helps!
Keep on coding,
Ares.
Try By use simple code and Set Content-type: text/html in headers it may useful:
$name = $_POST['yourname'];
$email = $_POST['youremail'];
$subject = $_POST['yoursubject'];
$message = $_POST['yourmessage'];
$email_from = $email;
$email_to = 'kylef33#gmail.com';
$body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Message: ' . $message;
//set the headers
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'From: <'.$email_from.'>' . "\r\n";
// Mail it
mail($email_to, $subject, $body, $headers);
Related
My server receives emails but none of the fields are shown.
All fields are empty in the email? Do you have any suggestions? I have tried everything that I know but no results. Form fields does not pass to the php
<?php
// variables start
$name = "";
$email = "";
$message = "";
$name = trim($_POST['contactNameField']);
$email = trim($_POST['contactEmailField']);
$message = trim($_POST['contactMessageTextarea']);
// variables end
// email address starts
$emailAddress = 'mail#domain.com';
// email address ends
$subject = "Message From: $name";
$message = "<strong>From:</strong> $name <br/><br/> <strong>Message:</strong> $message";
$headers .= 'From: '. $name . '<' . $email . '>' . "\r\n";
$headers .= 'Reply-To: ' . $email . "\r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
//send email function starts
mail($emailAddress, $subject, $message, $headers);
//send email function ends
?>
for this form
<form action="php/contact.php" method="post" class="contactForm" id="contactForm">
<div class="form-field form-name">
<label class="contactNameField color-theme" >Name:<span>(required)</span></label>
<input type="text" name="contactNameField" id="contactNameField" />
</div>
<div class="form-field form-email">
<label class="contactEmailField color-theme" >Email:<span>(required)</span></label>
<input type="text" name="contactEmailField" id="contactEmailField" />
</div>
<div class="form-field form-text">
<label class="contactMessageTextarea color-theme" >Message:<span>(required)</span></label>
<textarea name="contactMessageTextarea" id="contactMessageTextarea"></textarea>
</div>
<div class="form-button">
<input type="submit" class="btn bg-highlight text-uppercase font-900 btn-m btn-full rounded-sm shadow-xl contactSubmitButton" value="Gönder" />
</div>
Thank you
I have tried too many different ways to get reply-to from the $_request[email] but it keeps sending the mails with the $from CGI- mailer, although all the body on my mail work´s fine..
I have tried too many ways but i can't find where is my problem.. i have looked at several answers to this question here but not any one fixes my problem.. this is my code.
<?php
$subject = 'Contacact from website';
$to = 'contact#myhosting.com';
$emailTo = $_REQUEST['email'];
// an email address that will be in the From field of the email.
$name = $_REQUEST['name'];
$email = $_REQUEST['email']; // i can't get this going to the reply-to section on the mail
$phone = $_REQUEST['phone'];
$msg = $_REQUEST['message'];
$email_from = $name.'<'.$email.'>';
$headers = "MIME-Version: 1.1";
$headers .= "Content-type: text/html; charset=utf-8";
$headers .= 'From: ' . $fromName . ' <' . $fromEmail .'>' . " \r\n" .
'Reply-To: '. $fromEmail . "\r\n" .
'X-Mailer: PHP/' . phpversion();
// Send email
mail($sendTo, $subject, $emailText, implode("\n", $headers));
$message .= 'Name : ' . $name . "\n";
$message .= 'Email : ' . $email . "\n";
$message .= 'phone : ' . $phone . "\n";
$message .= 'Message : ' . $msg;
if (#mail($to, $subject, $message, $email_from)) {
// Transfer the value 'sent' to ajax function for showing success message.
echo 'sent';
} else {
// Transfer the value 'failed' to ajax function for showing error message.
echo 'failed';
}
?>
and this is my form:
<form name="contactForm" id='contact_form' method="post" action='email.php'>
<div class="row">
<div class="col-md-4">
<div id='name_error' class='error'>write your name</div>
<div>
<input type='text' name='name' id='name' class="form-control" placeholder="Name">
</div>
<div id='email_error' class='error'>Write a valid email</div>
<div>
<input type='email' name='email' id='email' class="form-control" placeholder="
email">
</div>
<div id='phone_error' class='error'>Write a phone number.</div>
<div>
<input type='tel' name='phone' id='phone' class="form-control" placeholder="Your name">
</div>
</div>
<div class="col-md-8">
<div id='message_error' class='error'>Please write your message here</div>
<div>
<textarea name='message' id='message' class="form-control"
placeholder="Message or quotation"></textarea>
</div>
</div>
<div class="col-md-12">
<p id='submit'>
<input type='submit' id='send_message' value='Enviar' class="btn btn-line">
</p>
<div id='mail_success' class='success'>We received your message :)</div>
<div id='mail_fail' class='error'>Please try again :/</div>
</div>
</div>
</form>
There are a few things wrong with what you posted.
Firstly, the first line for this block of code:
$message .= 'Name : ' . $name . "\n";
$message .= 'Email : ' . $email . "\n";
$message .= 'phone : ' . $phone . "\n";
$message .= 'Message : ' . $msg;
should not have a leading dot for $message .=, it should read as:
$message = 'Name : ' . $name . "\n";
Then this line:
if (#mail($to, $subject, $message, $email_from))
Since you're using $email_from as the last argument, mail() as you did for the other instance where you are sending mail, is using a valid From: with an E-mail address as it's coming "from", when the 2nd one does not contain that, you only declared it as $email_from = $name.'<'.$email.'>';.
What you will need to do is add the From: as you did for the first mailing instance.
Side note: The # symbol is an error suppressor. You might want to remove that during testing/development.
Consult the manual on the mail() function for more detail:
https://www.php.net/manual/en/function.mail.php
I design and developed one website in which I have one contact form. I wanted to send inquiry email through contact form. But I am getting following error in console. I tried on local system as well as on server.
Access to XMLHttpRequest at 'file:///E:/clients/website/contact.php' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.
Here is my HTML Code :
<form id="contactform" action="contact.php" name="contactform" method="post" class="form-validation" autocomplete="off">
<div class="row">
<div class="col-lg-4 col-md-4 col-sm-6 col-xs-12">
<div class="single-input">
<input type="text" placeholder="First Name*" name="Fname" id="Fname">
</div> <!-- /.single-input -->
</div>
<div class="col-lg-4 col-md-4 col-sm-6 col-xs-12">
<div class="single-input">
<input type="text" placeholder="Last Name*" name="Lname" id="Lname">
</div> <!-- /.single-input -->
</div>
<div class="col-lg-4 col-md-4 col-sm-12 col-xs-12">
<div class="single-input">
<input type="email" placeholder="Your Email*" name="email" id="email">
</div> <!-- /.single-input -->
</div>
</div> <!-- /.row -->
<div class="single-input">
<input type="text" placeholder="Subject" name="sub" id="subject">
</div> <!-- /.single-input -->
<textarea placeholder="Write Message" name="message" id="message"></textarea>
<button type="submit" value="SEND" id="submit" class="tran3s p-color-bg">Send Message</button>
</form>
Here is contact.php
<?php
if(!$_POST) exit;
// Email address verification, do not edit.
function isEmail($email) {
return(preg_match("/^[-_.[:alnum:]]+#((([[:alnum:]]|[[:alnum:]][[:alnum:]-]*[[:alnum:]])\.)+(com|coop|cr|cs)$|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i",$email));
}
if (!defined("PHP_EOL")) define("PHP_EOL", "\r\n");
$Fname = $_POST['Fname'];
$Lname = $_POST['Lname'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
if(get_magic_quotes_gpc()) {
$message = stripslashes($message);
}
$address = "sales#abc.com";
$e_subject = 'You\'ve been contacted by ' . $Fname . '.';
$e_body = "You have been contacted by $Fname with regards to $subject, their additional message is as follows." . PHP_EOL . PHP_EOL;
$e_content = "\"$message\"" . PHP_EOL . PHP_EOL;
$e_reply = "You can contact $Fname via email, $email";
$msg = wordwrap( $e_body . $e_content . $e_reply, 70 );
$headers = "From: $email" . PHP_EOL;
$headers .= "Reply-To: $email" . PHP_EOL;
$headers .= "MIME-Version: 1.0" . PHP_EOL;
$headers .= "Content-type: text/plain; charset=utf-8" . PHP_EOL;
$headers .= "Content-Transfer-Encoding: quoted-printable" . PHP_EOL;
if(mail($address, $e_subject, $msg, $headers)) {
// Email has sent successfully, echo a success page.
echo "<fieldset>";
echo "<div id='success_page'>";
echo "<h1>Email Sent Successfully.</h1>";
echo "<p>Thank you <strong>$Fname</strong>, your message has been submitted to us.</p>";
echo "</div>";
echo "</fieldset>";
} else {
echo 'ERROR!';
}
XMLHttpRequest does not support file scheme.
You could try this:
Solution 1: Use http/https instead of local file.
I would never use the file protocol for server-side files, if you need to test your .php files locally, it's better to use a localhost (like XAMPP) and test your code.
Solution 2 (don't skip the solution 1): Try this if solution 1 didn't work, but please don't use the file protocol (otherwise it won't work anyway).
Try to add the following header to the HTTP response:
Access-Control-Allow-Origin: *
The header above will allow your scripts to read the response regardless of the origin.
Note: Don't use Access-Control-Allow-Origin: * on your production code, otherwise you're exposing your website to security vulnerabilities.
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 6 years ago.
HTML:
<form action="php/send-contact.php" class="contact-form" name="contact-form" method="post">
<div class="row">
<div class="col-sm-6 cols">
<input type="text" name="name" required="required" placeholder="Name*">
</div>
<div class="col-sm-6 cols">
<input type="email" name="email" required="required" placeholder="Email*">
</div>
<div class="col-sm-6 cols">
<input type="text" name="subject" required="required" placeholder="Subject*">
</div>
<div class="col-sm-12 cols">
<textarea name="message" required="required" cols="30" rows="5" placeholder="Message*"></textarea>
</div>
<div class="col-sm-12 cols">
<input type="submit" name="submit" value="Send Message" class="btn btn-send">
</div>
</div>
</form>
php/send-contact.php:
<?php
$name = #trim(stripslashes($_POST['name']));
$email = #trim(stripslashes($_POST['email']));
$subject = #trim(stripslashes($_POST['subject']));
$message = #trim(stripslashes($_POST['message']));
$email_from = $email;
$email_to = 'hello#domain.co.uk';//replace with your email
$body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Message: ' . $message;
$success = #mail($email_to, $body, 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Message: ' . $message);
?>
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<script>
alert("Thank you for getting in touch. We will contact you as soon as possible.");
</script>
<meta HTTP-EQUIV="REFRESH" content="0; url=../index.html">
</head>
The HTML alert activates and refreshes as it should, but it doesnt send any email...
I have tried numerous email address recipients.
Also, are there any special measures I should take into account (regarding the PHP elements) when adding Google ReCaptcha to this form?
So I have tested this, and I think it is doing what you want.
$name = htmlentities($_POST['name']);
$email_from = htmlentities($_POST['email']);
$subject = htmlentities($_POST['subject']);
$message = htmlentities($_POST['message']);
$email_to = 'Admin#Domain.com';//replace with your email
$headers = "From: webmaster#example.com" . "\r\n" . "CC: ". $email_from; //This adds a from field, as well as CC's the person submitting the request.
//Build the body of the eamil
$body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email_from . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'message: ' . $message;
$success = mail($email_to, "Contact from site X, regarding: ".$subject, $body,$headers);
?>
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<?php if($success){ //If the email was sent correctly?>
<script>
alert("Thank you for getting in touch. We will contact you as soon as possible.");
</script>
<?php header('Location: ../index.html'); }else{?>
<script>
alert("There was an error when sending the email, please try again later.");
</script>
<?php header('Location: ../index.html'); } //If the email falied?>
</head>
The other file (the html) remains the same. Simply replace your code in php/send-contact.php with this.
I have a php contact form on my website. However when my customers input their e-mail address and submit their message I got a reply to my own e-mail. For example if they enter adam#yahoo.com I recieve the e-mail as myusername#myhostingprovider.com .... How can I fix this so the reply section shows the e-mail the customer input? Thank you.
Here is my code for my contact form
<form action="php/contact.php" method="post" class="contactForm" id="contactForm">
<fieldset>
<div class="formValidationError bg-red-dark color-white" id="contactNameFieldError">
<p class="center-text uppercase small-text">Name is required!</p>
</div>
<div class="formValidationError bg-red-dark color-white" id="contactEmailFieldError">
<p class="center-text uppercase small-text">Mail address required!</p>
</div>
<div class="formValidationError bg-red-dark color-white" id="contactEmailFieldError2">
<p class="center-text uppercase small-text">Mail address must be valid!</p>
</div>
<div class="formValidationError bg-red-dark color-white" id="contactMessageTextareaError">
<p class="center-text uppercase small-text">Message field is empty!</p>
</div>
<div class="formFieldWrap">
<label class="field-title contactNameField" for="contactNameField">Name:<span>(required)</span></label>
<input type="text" name="contactNameField" value="" class="contactField requiredField" id="contactNameField"/>
</div>
<div class="formFieldWrap">
<label class="field-title contactEmailField" for="contactEmailField">Email: <span>(required)</span></label>
<input type="text" name="contactEmailField" value="" class="contactField requiredField requiredEmailField" id="contactEmailField"/>
</div>
<div class="formTextareaWrap">
<label class="field-title contactMessageTextarea" for="contactMessageTextarea">Message: <span>(required)</span></label>
<textarea name="contactMessageTextarea" class="contactTextarea requiredField" id="contactMessageTextarea"></textarea>
</div>
<div class="formSubmitButtonErrorsWrap">
<input type="submit" class="buttonWrap button button-grey contactSubmitButton" id="contactSubmitButton" value="SUBMIT" data-formId="contactForm"/>
</div>
</fieldset>
</form>
Also here is the code for the PHP script
<?php
// variables start
$name = "";
$email = "";
$message = "";
$name = trim($_POST['contactNameField']);
$email = trim($_POST['contactEmailField']);
$message = trim($_POST['contactMessageTextarea']);
// variables end
// email address starts
$emailAddress = 'myemail#yahoo.com';
// email address ends
$subject = "Mywebsite.com | Mobile - Message From: $name";
$message = "<strong>From:</strong> $name <br/> <strong>E-Mail:</strong> $email </br><br/> <strong>Message:</strong> $message";
$headers = 'From: '. $name . '<' . $email . '>' . "\r\n";
$headers = 'Reply-To: ' . $email . "\r\n";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers = 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
//send email function starts
mail($emailAddress, $subject, $message, $headers);
//send email function ends
?>
In your code, you haven't been adding to your header string, you've just been resetting it each time.
What you're finally sending to the mail function in the header variable is just;
'Content-type: text/html; charset=iso-8859-1' . "\r\n"
What you need to do is either where you set $headers = , change the equals sign so it looks like this $headers .=
As that means that it will add on to the String. Or, just format it as one massive string.
<?php
$headers = 'From: '. $name . '<' . $email . '>' . "\r\n";
$headers .= 'Reply-To: ' . $email . "\r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
//send email function starts
mail($emailAddress, $subject, $message, $headers);