This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 8 years ago.
Ok so my contact form was working fine when I first coded it into my website but now after a few months it has just stopped sending emails through to my address. I'm not sure if this is a problem with the code or with my email? Here is my current mail form...
<form action="contact.php" method="POST" value="form" >
<p>Name:</p><br /><input type="text" name="name" maxlength="25" /><br /><br />
<p>Email address:</p><br /><input type="text" name="email" maxlength="50" /><br /><br />
<p>Message:</p><br /><textarea name="contact_text" rows="6" cols="30" maxlength="1000" ></textarea><br /><br />
<input type="submit" name="submit" />
</form>
<?php
if (isset($_POST['name']) && isset($_POST['email']) && isset($_POST['contact_text'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$text = $_POST['contact_text'];
if (!empty($name) && !empty($email) && !empty($text)) {
if(strlen($name)>25 || strlen($email)>50 || strlen($text)>1000) {
echo 'Sorry, max length for some field has been exceeded.';
} else {
$to = 'my.email.address#gmail.com';
$subject = 'Contact form submitted';
$body = $text."\n".$name;
$headers = 'From: '.$email;
if (#mail ($to, $subject, $body, $headers)) {
echo '<h5>Thanks for the message, I will be in touch soon.</h5>';
} else {
echo'<h4>Sorry an error occured, please try again later.</h4>';
}
}
}
else {
echo '<h4>All fields are required.</h4>';
}
}
?>
Try with this mail header :
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
Related
i'm trying to get a message to appear after my form has been submitted, i can get the form to submit and the page to then refresh but unsure how to add the message after the refresh.
i have no knowledge of PHP so sorry is its and obvious answer
thanks
my html & PHP if needed
<form action="action.php" method="POST" target="my_iframe" onsubmit="setTimeout(function(){window.location.reload();},10);">
<input type="text" id="name" name="name" placeholder="name" required="required">
<input type="email" id="email" name="email" placeholder="email" required="required">
<input type="tel" id="phone" name="phone" placeholder="phone">
<div class="form-row" style="display:none;"><input type="hidden" name="url" placeholder="URL"></div>
<textarea id="message" name="message" placeholder="message" style="height:200px" required="required"></textarea>
<input id="submit" type="submit" value="submit">
</form>
<iframe name="my_iframe" width="1" height="1" style="border:none"></iframe>
<?php
$to = 'zoeharrisondesign#gmail.com';
$subject = 'New Message Recieved!';
$from = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$name = $_POST['name'];
//check honeypot
if( !empty( $honeypot ) ) {
echo 'There was a problem';
}
// 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 = "$message \r\n |
From $name \r\n |
Tel: $phone";
// Sending email
if(mail($to, $subject, $message, $headers)){
echo 'Your message has been sent successfully.';
} else{
echo 'Unable to send email. Please try again.';
}
?>
Update your php sending email to this
if (mail($myEmail, $subject, $message)){
$success = "Message sent successful";
}else{
$success = "Message Sending Failed.";
}
Then add this php codes in your html codes add this code to display a message. Put it on top of your form html tag.
<?php
if (isset($success)){ echo "<div>" . $success . "</div>";}
?>
You may even add some styling on the message if you prefer.
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 6 years ago.
I been spending hours and still can't figure it out. New to PHP mail form coding. Any help would truly be appreciated! Two images below: the html and php. Pretty simple setup. I get the echo 'thank you!!' message after clicking send button, but I still don't receive any emails at all.
HTML Form:
<form action="assetslphplmail.php" method="POST">
<input placeholder="NAME" type="text" name="name">
<input p1aceholder="EMAIL" type="email" name="email">
<input p1aceholder="TELEPHONE" type="te1" name="te1">
<input p1aceholder="SUBJECT" type="text" name="subjectline">
<textarea p1aceholder="COMMENT" name="message" rows="6" cols="25" </textarea>
<input c1ass="send_button" type="submit" value="SEND">
</form>
PHP Code:
$subject1ine = $_REQUEST['subject1ine'];
$name = $_REQUEST['name'];
$emai1 = $_REQUEST['emai1'];
$tel 1' $_REQUEST['teI'];
$message = $_REQUEST['message'];
$to = "jondergmai1.com";
mail ( $to, $subjectline, $name, $emai1, $tel, $message);
echo 'Thank you!!';
Replace your PHP code with following:
<?php
$subject = $_REQUEST['subjectline'];
$name = $_REQUEST['name'];
$email = $_REQUEST['email'];
$tel = $_REQUEST['tel'];
$message = $_REQUEST['message'];
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To:'.$email . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$to = "jonder#gmail.com";
mail($to, $subject, $message, $headers);
echo 'Thank you!!';
?>
Edit From and Reply-To part.
Here are the plenty of similar answers: PHP Mail form doesn't send email
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 7 years ago.
So everything works except that I don't receive an email after sending and wonder why?
keeps getting this error "Could not execute mail delivery program '/usr/local/bin/sendmail -oi -t' in"
Html
<form action="process.php" method="post">
<ul>
<li>
<label for="name">Name:</label>
<input type="text" name="name" id="name" />
</li>
<li>
<label for="email">Email:</label>
<input type="text" name="email" id="email" />
</li>
<li>
<label for="topic">Topic:</label>
<select>
<option value="optiona">optiona</option>
<option value="optionb">optionb</option>
<option value="optionc">optionc</option>
</select>
</li>
<li>
<label for="message">your message:</label>
<textarea id="message" name="message" cols="42" rows="9"></textarea>
</li>
<li><input type="submit" value="Submit"></li>
</form>
PHP
<?php
$to = 'robin.kahrle#gmail.com';
$subject='hi there you';
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$message = <<<EMAIL
Hi! My name is $name.
$message
From $name
my email is $email
EMAIL;
$header ='$email';
if($_POST){
mail($to, $subjects, $message, $header);
$feedback = 'Thankyou for your email';
echo $feedback;
}
?>
Maybe your $header and $message definition is not ok. Try this:
<?php
// Constants
$to = 'robin.kahrle#gmail.com';
$subject='hi there you';
// Variable contents from the form
$name = (!empty($_POST['email'])) ? $_POST['name'] : '????';
$email = (!empty($_POST['email'])) ? $_POST['email'] : $to;
$message = 'Hi? my name is ' . $name;
if (!empty($_POST['message'])) $message .= . "\r\n" . $_POST['message'];
// Header
$header = 'From: ' . $email . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
// Send mail and give feedback
$accepted = mail($to, $subjects, $message, $header);
if ($accepted) {
$feedback = 'Thankyou for your email';
} else {
$feedback = 'Email not accepted';
}
echo $feedback;
This question already has answers here:
How to '$_POST' back to the same page or to a different result page in php? [closed]
(3 answers)
Closed 9 years ago.
What I want to do is to create a simple contact form. I have a file called contact.php with the simple html form:
<form action="process-contact.php" method="post">
<input type="text" name="name" placeholder="Nombre*">
<input type="text" name="company" placeholder="Compañía">
<input type="text" name="position" placeholder="Posición">
<input type="text" name="country" placeholder="País*">
<input type="text" name="email" placeholder="Correo electrónico*">
<input type="text" name="subject" placeholder="Asunto">
<textarea name="message" placeholder="Mensaje*"></textarea>
<input type="submit" value="Enviar">
</form>
And I have this php code:
$name = $_POST['name'];
$company = $_POST['company'];
$position = $_POST['position'];
$country = $_POST['country'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$headers = "Sent by: ".$name.' ('.$email.');'." Country: ".$country.'; Company: '.$company. '; Position: '.$position.';';
$to = "email#domain.com";
if($name != '' && $country != '' && $email != '' && $message != ''){
mail($to, $subject, $message, $headers); //calling php mail function
echo 'Thank\'s for contacting us. We will be answering back soon.<br><br>Go back.';
}else{
echo 'Plese verify all the fields and try to send the form again.<br><br>Go back.';
}
The php code is in another file, but I would like to have the php code on the same file file. Is that possible? How can I do it?
Thank you.
You can put everything inside one file and using action=""
Your headers had errors in them and by using those, the Email ended up in my SPAM box, so I changed those with proper headers and put other details using the $message variable.
I added header('Location: thank_you.php'); and will redirect to that page if all the fields were filled. Create a file called thank_you.php or change it to go to your home page.
I replaced:
if($name != '' && $country != '' && $email != '' && $message != ''){
by:
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['country']) ||
empty($_POST['message'])) {
echo "Fill in all the fields. All marked by an asterisk are mandatory.";
}
Form and PHP handler (tested successfully)
<?php
$name = $_POST['name'];
$company = $_POST['company'];
$position = $_POST['position'];
$country = $_POST['country'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$to = "email#example.com";
$message = "Sent by: ".$name.' ('.$email.');'." Country: ".$country.'; Company: '.$company. '; Position: '.$position.';';
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['country']) ||
empty($_POST['message'])) {
echo "Fill in all the fields. All marked by an asterisk are mandatory.";
}
else {
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-Type: text/html; charset=iso-8859-1\n";
$headers .= "From: $email" . "\r\n" .
"Reply-To: $email" . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers); //calling php mail function
header("Location: thank_you.php");
}
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form action="" method="post">
<input type="text" name="name" placeholder="Nombre*">
<input type="text" name="company" placeholder="Compañía">
<input type="text" name="position" placeholder="Posición">
<input type="text" name="country" placeholder="País*">
<input type="text" name="email" placeholder="Correo electrónico*">
<input type="text" name="subject" placeholder="Asunto">
<textarea name="message" placeholder="Mensaje*"></textarea>
<input type="submit" name="submit" value="Enviar">
</form>
</body>
</html>
Or you can use this as your PHP handler
<?php
$name = $_POST['name'];
$company = $_POST['company'];
$position = $_POST['position'];
$country = $_POST['country'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$to = "email#example.com";
$message = "Sent by: ".$name.' ('.$email.');'." Country: ".$country.'; Company: '.$company. '; Position: '.$position.';';
if(!empty($_POST['name']) &&
!empty($_POST['email']) &&
!empty($_POST['country']) &&
!empty($_POST['message'])) {
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-Type: text/html; charset=iso-8859-1\n";
$headers .= "From: $email" . "\r\n" .
"Reply-To: $email" . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers); //calling php mail function
echo 'Thanks for contacting us. We will be answering back soon.<br><br>Go back.';
// to prevent re-submission, use header but NOT with echo.
// header("Location: thank_you.php");
}else{
echo 'Please verify all the fields.<br><br>Go back.';
}
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form action="" method="post">
<input type="text" name="name" placeholder="Nombre*">
<input type="text" name="company" placeholder="Compañía">
<input type="text" name="position" placeholder="Posición">
<input type="text" name="country" placeholder="País*">
<input type="text" name="email" placeholder="Correo electrónico*">
<input type="text" name="subject" placeholder="Asunto">
<textarea name="message" placeholder="Mensaje*"></textarea>
<input type="submit" name="submit" value="Enviar">
</form>
</body>
</html>
Set the action attribute of the form to the name of the file containing the form and php code and then include all your form processing php code in the following conditional.
if(count($_POST)) > 0)
This will make sure the php code runs only when the form is submitted.
You'll want to use the isset feature of PHP, perhaps.
$name = $_POST['name'];
if(isset($name) && $name != ''){
blah....
}
else {
blah...
}
PHP has never been my strong suit so I need some assistance getting the contents of a form to send via email properly in PHP.
Here is the html form:
<form action="estimate.php" method="post" >
<fieldset>
<input type="text" class="required" name="name" value="FULL NAME*" onfocus="if (this.value=='FULL NAME*') this.value='';"/>
<input type="text" class="required" name="phone" value="PHONE NUMBER*" onfocus="if (this.value=='PHONE NUMBER*') this.value='';"/>
<input type="text" class="required" name="email" value="EMAIL*" onfocus="if (this.value=='EMAIL*') this.value='';"/>
<input type="text" class="required" name="date" value="MOVE DATE*" onfocus="if (this.value=='MOVE DATE*') this.value='';"/>
<input type="text" class="required" name="origin" value="ORIGINATING ADDRESS*" onfocus="if (this.value=='ORIGINATING ADDRESS*') this.value='';"/>
<input type="text" name="destination" value="DESTINATION ADDRESS" onfocus="if (this.value=='DESTINATION ADDRESS') this.value='';"/>
<select name="move-type">
<option value="" selected="selected">TYPE OF MOVE</option>
<option value="Private">Private</option>
<option value="Commercial">Commercial</option>
</select>
<input id="quoteSubmit"
type="image" src="_images/btn_submit.png" alt=""
onmouseover="javascript:this.src='_images/btn_submit-over.png'"
onmouseout="javascript:this.src='_images/btn_submit.png'"/>
</fieldset>
</form>
Here is the PHP I have thus far:
<?php
$emailFromName = $_POST['name'];
$emailFromPhone = $_POST['phone'];
$emailFrom = $_POST['email'];
$emailDate = $_POST['date'];
$emailOrigin = $_POST['origin'];
$emailDestination = $_POST['destination'];
$emailType = $_POST['move-type'];
if (empty($emailFromName)) {
echo 'Please enter your name.';
} elseif (!preg_match('/^([A-Z0-9\.\-_]+)#([A-Z0-9\.\-_]+)?([\.]{1})([A-Z]{2,6})$/i', $emailFrom) || empty($emailFrom)) {
echo 'The email address entered is invalid.';
} elseif (empty($emailDate)) {
echo 'You must enter a Move date.';
} elseif (empty($emailOrigin)) {
echo 'You must enter a message.';
} elseif (empty($emailDestination)) {
echo 'You must enter a message.';
} else {
$emailTo = "info#movingsimple.com";
if (!empty($emailFrom)) {
$emailHeaders = 'From: "' . $emailFromName . '" <' . $emailFrom . '>';
} else {
$emailHeaders = 'From: "The Boss" <noreply#movingsimple.com>';
}
/* Send Email */
if (mail($emailTo, $emailSubject, $emailDate, $emailHeaders)) {
echo 'Thank you! Your message has been sent.';
} else {
echo 'There was an internal error while sending your email.<br>';
echo 'Please try again later.';
}
}
?>
I am having trouble with the body of the message. I need to display all the values from the form:
From:
Email:
Phone:
Move date:
Origin:
Destination:
Move type:
but I am not exactly sure how to compile all those into the body and would appreciate some help.
thanks.
Update this part of the code
if (!empty($emailFrom)) {
$emailHeaders = 'From: "' . $emailFromName . '" <' . $emailFrom . '>';
} else {
$emailHeaders = 'From: "The Boss" <noreply#movingsimple.com>';
}
/* Send Email */
if (mail($emailTo, $emailSubject, $emailDate, $emailHeaders)) {
echo 'Thank you! Your message has been sent.';
to generate a string containing the body of the email:
if (!empty($emailFrom)) {
$emailHeaders = 'From: "' . $emailFromName . '" <' . $emailFrom . '>';
} else {
$emailHeaders = 'From: "The Boss" <noreply#movingsimple.com>';
}
// Create a string representing the body. "\n" creates a new line.
// The ".=" operator appends the string onto the existing string.
$body = "From: ".$emailFromName."\n";
$body .= "Email: ".$emailFrom."\n";
$body .= "Phone: ".$emailFromPhone."\n";
$body .= "Move Date: ".$emailDate."\n";
$body .= "Origin: ".$emailOrigin."\n";
$body .= "Destination: ".$emailDestination."\n";
$body .= "Move Type: ".$emailType."\n";
/* Send Email */
if (mail($emailTo, $emailSubject, $body, $emailHeaders)) {
echo 'Thank you! Your message has been sent.';
// ... rest of the code ...
The third parameter of the mail() function (http://us.php.net/manual/en/function.mail.php) is the message, so we create the message as a string ($body) and then pass it as a parameter to the mail() function.
Small Update: The PHP mail() help page says to use "\n" as a line ending rather than "\r\n"
The third attribute to the mail function is for body of the email not date like you are specifying $emailDate, you should specify the email body there.
Right now you're sending the $emailDate as the body of the message.. You just need to build a variable with the data you want. i.e.
$emailBody = "Move date: " . $emailDate;
I think your header variable needs to have a \r\n at the end, too.