This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 5 years ago.
I am trying to create a contact form using php and a html form. When the user clicks submit I want the details that they have entered into the form to be sent in an email.
When the user clicks submit they are shown a message to say the message has been sent but when I check my email nothing has been sent.
Here is my code, please note the email has been changed to show the code here.
<div id = "form">
<form action ="contact2.php" method="post">
Name:
<input type="text" name="name">
<br>
<br>
Email:
<input type="text" name="email">
<br>
<br>
Message:
<br>
<br>
<TEXTAREA NAME="message" ROWS=6 COLS=40>
</TEXTAREA>
<br>
<br>
<input type="submit" value="Submit">
</div>
</form>
<?php
$field_name = $_POST['name'];
$email = $_POST['email'];
$field_message = $_POST['message'];
$mail_to = 'example#yahoo.co.uk';
$subject = 'Message from a site visitor ' . $field_name;
$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$email."\n";
$body_message .= 'Message: '.$field_message;
$headers = "From: $email\r\n";
$headers .= "Reply-To: $email\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Thank you for the message. We will contact you shortly.');
window.location = 'contact.php';
</script>
else { ?>
<script language="javascript" type="text/javascript">
alert('Message failed. Please, send an email to example#yahoo.co.uk');
window.location = 'contact.php';
</script>
<?php
}?>
you want to have all that javascript wrapped in a php echo or else it doesnt work.
so...
if ($mail_status) {
echo '<script language="javascript" type="text/javascript">';
thats just a bit of the code you need to implement...
you want to echo every javascript line too.
Related
I am using Js and PHP, so basically I would like form submissions to be sent via emails with out the page to reload which it is not working. Any help would be great thanks.
This is just a simple form where you submitted and the input data appears below where it should be sent via email as well.
<form id="myForm" method="post">
Name: <input name="name" id="name" type="text" /><br />
<input type="button" id="submitFormData" onclick="SubmitFormData();" value="Submit" />
</form>
<br/>
Your data will display below..... <br />
==============================<br />
<div id="results">
<!-- All data will display here -->
</div>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
function SubmitFormData() {
var name = $("#name").val();
$.post("./submit.php", { name: name },
function(data) {
$('#results').html(data);
$('#myForm')[0].reset();
});
}
</script>```
Here another file on form process.
<?php
if( $_SERVER['REQUEST_METHOD'] == 'POST') {
$name = $_POST['name'];
echo $name;
$to = 'name#name.com';
$subject = " Test Enquiries";
// Always set content-type when sending HTML email
$headers .= "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// More headers
$headers .= 'From: domain#domain.com' . "\r\n";
// Email body content
$message = "
<p><strong>Name:</strong> <span>{$name}</span></p>
";
mail($to,$subject,$message,$headers);
if (mail ($to, $subject, $message, $headers))
{
echo '<p>Your message has been sent!</p>';
}
else
{
echo '<p>Something went wrong, go back and try again!</p>';
}
}
?>
I want to send an email when someone click on my website's download link. I am working in PHP and JavaScript. The following two snippets are in same file.
HTML
<form method="post">
<div class="form-group">
<input type="email" class="form-control" placeholder="Enter Email ID" id="email" onchange="check();" require pattern="[a-z0-9._%+-]+#[a-z0-9.-]+\.[a-z]{2,3}$"/>
</div>
<div class="form-group">
<label style="font-size:14px;"><input type="checkbox" id="toggle"> I agree to receive emails from MarketerBoard. We will not share your any personal information with anyone.</label>
</div>
<div class="form-group" style="min-height:40px;">
<a style="display:none;" href="/images/docs/EMAIL-MARKETING-GUIDE.pdf" name="downloadBtn" download class="mb-button" id="dwn-btn" onchange="check();" onClick="sendmail();">Get your free guide</a>
</div>
</form>
JavaScript
if(isset($_POST['downloadBtn']))
{
$to = 'examle#example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: example#example.com' . "\r\n" .
'Reply-To: example#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
//echo 'Email Sent.';
}
I validate the email id of user from using following code. I want to send the email on click of the link, which does not seems to work.
<script type="text/javascript">
function check() {
document.getElementById("toggle").checked = false;
var email_x = document.getElementById("email").value;
filter = /^([a-zA-Z0-9_\.\-])+\#(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
if (filter.test(email.value)) {
document.getElementById("email").style.border = "2px solid teal";
var userEmail = document.getElementById('email').value
$('#toggle').click(function () {
//check if checkbox is checked
if ($(this).is(':checked') & filter.test(email.value)) {
$("#dwn-btn").show(); }
else {
$("#dwn-btn").hide();}});
return true;
} else {
document.getElementById("email").style.border = "2px solid red";
$("#dwn-btn").hide();
return false;
}
}
</script>
Firstly check/change your PHP mail configuration:
Open your php.ini file
Search for the line that reads [mail function]
Add/change the details of your mail server. This could be a local mail server or the mail server of your ISP.
Save/close the php.ini file
Restart your web server
Example code below :
<form action="" method="post">
<input type="submit" value="Send email" />
<input type="hidden" name="send_mail" value="1" />
</form>
<?php
if(isset($_POST['send_mail']))
{
$to = 'nobody#example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: nobody#example.com' . "\r\n" .
'Reply-To: nobody#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
echo 'Email Sent.';
}
?>
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 5 years ago.
I have problem with send email. When I send a form, I get a blank message in my inbox. It looks like this:
Mail:
New message from:
E-mail:
Topic:
Sent message:
The form has validation, it works correctly. The problem is that I don't know why messages come empty to my inbox.
Here is a php code:
$name = $_POST['name'];
$mail = $_POST['mail'];
$topic = $_POST['topic'];
$message = $_POST['message'];
$email_to = "example#qmail.com";
$email_subject = "You received a new message";
$email_body = "Mail: \r\n";
$email_body .= "New message from: " . $name . "\r\n";
$email_body .= "E-mail:" . $mail . "\r\n";
$email_body .= "Topic: " . $topic . "\r\n";
$email_body .= "Sent message: " . $message . "\r\n";
$success = mail($email_to, $email_subject, $email_body);
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=success-form\index.html\">";
}
else{
print "<script>console.log('error'); </script>";
}
?>
Form in HTML:
<form method="post" action="../form.php" >
<input name="name" type="text" placeholder="Name and Surname">
<label></label>
<input name="mail" type="email" placeholder="Email address">
<label></label>
<input name="topic" type="text" placeholder="Topic">
<label></label>
<textarea name="message" placeholder="Message..."></textarea>
<label></label>
<input type="submit" class="send-btn" value="Send">
</form>
Have you tried adding a few headers? For example:
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/plain; charset=utf-8\r\n";
$headers .= "Content-Transfer-Encoding: 8bit\r\n";
$headers .= "FROM:".$email_from."\r\n"; // Variable to define
Then, call you mail command like this:
$success = mail($email_to, $email_subject, $email_body, $headers);
I have a mail form that will not submit the input placed into the text area to the e-mail that I have inserted.
I am completely new to PHP.
This form has worked for me in the past.
I am trying to run the form from the freehostia.com server, I have the first upgraded plan which advertises SMTP.
Here is my HTML:
<form action="contact.php" method="post">First name:
<br>
<input class="A" id="B" type="text" name="name" size="25">
<br>
E-mail:
<br>
<input class="A" id="A" type="text" name="email" size="35">
<textarea id="A" name="message" rows="10" cols="50" placeholder="Please provide your information..."></textarea>
<div>
<input class="size" type="submit" value="Send!" />
</div>
</form>
Here's my PHP content:
<?php
$field_name = $_POST['name'];
$field_email = $_POST['email'];
$field_message = $_POST['message'];
$mail_to = 'XXXX#XX.com';
$subject = 'MESSAGE FROM SITE VISITOR ' . $field_name;
$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message: '.$field_message;
$headers = "From: $email\r\n";
$headers .= "Reply-To: $email\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Thank you for the message. We will contact you shortly.')
window.location = 'index.html';
</script>
<?php }
else { ?>
<script language="javascript" type="text/javascript">
alert('Message failed. Please, send an email to XXXX#XX.com');
window.location = 'contact.html';
</script>
<?php } ?>
Everything appears to be in place to me. I can't for the life of me figure out what I have done wrong here.
where is ur '$email' variable in,
$headers = "From: $email\r\n";
$headers .= "Reply-To: $email\r\n";
where it initialized?
go this website: https://github.com/HouKun1230/Jquery-mobile-with-PHP-MYSQL and look the email.php which is a email sending simple. It may help you.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
i've got a basic php script for simple contact form with name, email and message inputs.
I wish to include few more options in it but don't know how to. I've searched but couldn't find all in one solution for. I would like to:
1. Send a copy to senders email
I would like to include input for sender to have an option to receive a copy of he's submit to he's email if he checkes that input in the form.
2. Upload a file
Also if possible in the same php script i wish to give a possibility for the sender to attach a file (preferably img extensions only) when submiting a form.
3. Thank you message
Not sure about this, but now i have a simple thank you message in echo when form is submited. If possible, i wish for this message to stay visible for 5 seconds then redirect to index.html.
Here is php for the form:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent="Name: $name \nEmail: $email \nMessage: $message";
$recipient = "test123#...";
$subject = "Contact";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo
"<div style='display: block; text-align: center;'>" .
"<span style='font-size: 14px;'>You message has been sent!</span>" .
"<a href='index.html'>Go back</a>" .
"</div>";
?>
and demo jsfiddle of the form setup.
Thanks for any help.
This is a global setup just to let you know how I would do this (if I wanted to do this on 1 page, but it's better to make functions, etc.)
EDIT: Please note also that I don't know if this works. Maybe there are errors but I have done this just to get you started.
<?php
//Check if form submitted
if (isset($_POST)) {
//this all will run when form is submitted
//First sanitize you data thats been posted
$name = htmlentities($_POST['name'], ENT_QUOTES, 'UTF-8');
$email = htmlentities($_POST['email'], ENT_QUOTES, 'UTF-8');
$message = htmlentities($_POST['message'], ENT_QUOTES, 'UTF-8');
//make a error array to hold errors
$error = array();
//check if fields are not empty you can also do other checks
if (!empty($name) || !empty($email) || !empty($message))
//here you could do extra checks.. like check if emai is really a email...
if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
//email invalid
array_push($error, 'email not valid');
}
//for image you could also do a if...
if(isset($_FILES)) {
$uploads_dir = 'YOUR DIR'
$name = $_FILES['image']['name'];
$type = $_FILES['image']['type'];
$size = $_FILES['image']['size'];
$temp = $_FILES['image']['tmp_name'];
$error = $_FILES['image']['error'];
if ($error === 4) {
//No file was selected
return false;
}
else
{
//do your stuff with the image here...
move_uploaded_file($temp, "$uploads_dir/$temp");
}
///you could do more ifs.. but if all is good then do the mail
$subject = 'new contact form message';
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($email, $subject, $message, $headers);
$success = "here the success message";
} else {
//some fields are empty
array_push($error, 'some fields are empty');
}
?>
<!-- THE ENCTYPE IS NEEDED FOR IMAGES -->
<form action="submit.php" name="contact-form" id="contact-form" method="post" enctype="multipart/form-data">
<input name="name" placeholder="Name" type="text" id="name" required />
<input name="email" placeholder="Email" type="email" id="email" required />
<textarea name="message" placeholder="Message" id="message" required></textarea>
<input type="file" id="upload-file" accept="image/*" />
<div class="clear"></div>
<input type="checkbox" id="copy" name="copy" />
<label for="copy">Send a copy to my email</label>
<div class="clear"></div>
<input type="submit" value="Submit" form="contact-form" name="submit-form" />
</form>
<?php
if (isset($success) && !empty($success)) {
//echo the success
echo $success
}
if (isset($error) && !empty($error)) {
//loop trough error
foreach ($error as $e) {
echo $e;
}
}
?>