php header redirect doesn't work - php

I've created a mail form. A PHP page reads the input, sends it out to a mail and then it should redirect to a certain page. Pretty straight forward, done it before. The mail gets send, but you don't get redirected.
HTML form:
<form action="mailversturen.php" method="post">
<input type="text" name="naam" placeholder="Naam" class="inputtype" /><br />
<input type="text" name="email" placeholder="Email" class="inputtype" /><br />
<textarea name="bericht" placeholder="Bericht"></textarea>
<input type="reset" value="Reset" class="buttontype" />
<input type="submit" value="Verstuur" class="buttontype" />
</form>
PHP code:
<?php
$name = $_POST['naam'];
$email = $_POST['email'];
$message = $_POST['bericht'];
$to = "name#domain.com";
$subject = "Bericht van $name";
$headers = "From: $email \r\n";
mail($to,$subject,$message,$headers);
header('Location: http://www.newlocation.nl/');
?>
What am I doing wrong?

Try adding #ob_start(); after your opening <?php tag.
This turns on output buffering. I use it in combination with all header(...) redirects.

Related

mail.php can't get it to email the correct data

I've no experience with php, due to an old formtomail cgi script no longer working on a host server I decided to switch to a simple html to php mail for a contact page on a website. I took a template online and set up the html page but having difficulty editing the mail.php file so that all the data requests on my html form get emailed over to me. The template I use just had email and message. My html contact page has different requests i.e contact name not name, and asks for more information. so I need the php form to email all this information and need help on how to edit the php file to include whats requested on the html contact page.
I have a contact form HTML page with the following:
<form action="mail.php" method="POST">
<p class="bodytextgrey">
Contact Name:<br /><input type="text" name="contact name" size="50" /><br />
Firm Name:<br /><input type="test" name="firm name" /><br /><br />
E-mail: (Please enter the email address you would like the document is to be sent to)<br />
<input type="text" name="email" size="50" /><br />
Job Number:<br /><input type="test" name="job number" /><br /><br />
Document you require:<br /><form action=""><select name="Document you require">
<option value="Data Sheet">Data Sheet</option>
</select><br/ ><br />
Discount code:<br /><input type="text" name="Discount code" size="20" /><br /><br />
<input type="submit" value="Send"><input type="reset" value="Clear">
</form>
I then have a mail.php with this (MY EMAIL is my actual email):
<?php $name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent="From: $name \n Message: $message";
$recipient = "MY EMAIL";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!";
?>
Would also like it to bring up an existing thank you.html page rather than just a thankyou word on a white page so also need help adding in how I link this.
Any help would be appreciated
Formatting your code will help immensely to start debugging this. I've reformatted your current HTML with some indenting and line breaks to help make it clear:
<form action="mail.php" method="POST"> <!-- missing closing tag -->
<p class="bodytextgrey"> <!-- missing closing tag -->
Contact Name:<br />
<input type="text" name="contact name" size="50" /><br />
Firm Name:<br />
<input type="test" name="firm name" /><br /><br />
E-mail: (Please enter the email address you would like the document to be sent to)<br />
<input type="text" name="email" size="50" /><br />
Job Number:<br />
<input type="test" name="job number" /><br /><br />
Document you require:<br />
<form action=""> <!-- nested form in a form will break things -->
<select name="Document you require">
<option value="Data Sheet">Data Sheet</option>
</select><br/ ><br />
Discount code:<br />
<input type="text" name="Discount code" size="20" /><br /><br />
<input type="submit" value="Send">
<input type="reset" value="Clear">
</form>
There are a few things wrong with this:
Opening <p> with no </p> (closing tag) anywhere
<form action=""> nested inside of an existing form isn't allowed and will cause submission issues (see "Permitted content" at https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form)
The closing </form> that exists matches the <form action="">, but you're missing a </form>
You don't have a message field anywhere
Here's the updated HTML with my suggested fixes:
<form action="mail.php" method="POST">
<p class="bodytextgrey">
Contact Name:<br />
<input type="text" name="contact_name" size="50" /><br />
Firm Name:<br />
<input type="test" name="firm_name" /><br /><br />
E-mail: (Please enter the email address you would like the document to be sent to)<br />
<input type="text" name="email" size="50" /><br />
Job Number:<br />
<input type="test" name="job_number" /><br /><br />
Document you require:<br />
<select name="document">
<option value="Data Sheet">Data Sheet</option>
</select><br/ ><br />
Discount code:<br />
<input type="text" name="discount" size="20" /><br /><br />
Message:<br />
<textarea name="message"></textarea><br /><br />
<input type="submit" value="Send">
<input type="reset" value="Clear">
</p>
</form>
As others have mentioned, it's not clear what the 500 error is, so that will need to be resolved before this will work. But here's a cleaned up version of the PHP. This has been simply tested at https://3v4l.org/WePRh, though it won't show the form or any input values, but it does prove that the script runs. This is formatted better, includes all form inputs, and should make things easier to work with:
<?php
// get all form values
$input['Contact name'] = $_POST['contact_name'] ?? '';
$input['Firm name'] = $_POST['firm_name'] ?? '';
$input['Email'] = $_POST['email'] ?? '';
$input['Job number'] = $_POST['job_number'] ?? '';
$input['Document'] = $_POST['document'] ?? '';
$input['Discount'] = $_POST['discount'] ?? '';
$input['Message'] = $_POST['message'] ?? '';
// generate the email content (i.e. each line is like "Contact name: Mark")
$formContent = '';
foreach($input as $key => $value) {
$formContent .= $key . ": " . $value . "\n";
}
$to = "my#email.com"; // TODO: replace this with your email address
$subject = "Contact Form";
/*
Note that this was likely causing downstream issues. Most email clients (gmail, outlook) will mark emails as spam
if you try to send an email from an email address that your server isn't configured to send from.
See https://stackoverflow.com/questions/17533863/how-to-configure-php-to-send-e-mail for more info.
It's also safer to define an email address like "no-reply#example.com" to send your emails from.
*/
$mailheader = "From: no-reply#example.com";
mail($to, $subject, $formContent, $mailheader) or die('Error sending email');
// redirect to the thank you page
// TODO: update this URL to be yours
header('Location: http://www.example.com/thankyou.html');
exit;
?>
You can use the header() function:
Example:
header('location: thankyou.html');

PHP file name added to URL but not called from html

When using a html form to call php to send an email with the details in the form to an email address specified in the php. The php file name is added to the URL but no email is sent and the page shown in blank.
The website is being hosted on my.ionos.co.uk (1&1) using php 7.3.
<form method="post" action="contact-form.php">
<input class="contact" type="text" name="Name" placeholder="Name">
<input class="contact" type="text" name="Email" placeholder="Your Email Address">
<input class="contact" type="text" name="Subject" placeholder="Subject">
<textarea class="contact" name="Message" rows="10" placeholder="Message"></textarea>
<input class="contact-submit" type="submit" value="Send Message">
</form>
<?php
if(isset($_POST['submit'])) {
$name = $_POST['Name'];
$mailFrom = $_POST['Email'];
$subject = $_POST['Subject'];
$message = $_POST['Message'];
$mailTo = "X#hotmail.com";
$headers = "From: ".$mailFrom;
$txt = "You have recieved an email from ".$name.".\n\n".$Message;
if(mail($mailTo, $subject, $txt, $headers)){
echo "<h1>Mail send successfully</h1>";
} else {
echo "<h1>Mail failed to send<h1>";
}
header("Location: contact.html?mailsend");
}
?>
I'd appreciate it if someone could help, thanks in advance!
Try to change:
if(isset($_POST['submit'])) {
with
if(isset($_POST['Email'])) { //or another field in your form. If you want you can also add in your submit button: name="submit"
Checking out your code seems there is not an input with name submit. So you never enter in the if case.

php and html email forms

I am creating a website for my church choir in HTML. I have a "contact us" page written in HTML. It has a form for the user to send the choir director an email from the website. I am aware that I have to write the email in php in order for the email to send. Do I need to duplicate the html file and rewrite it in PHP?
<form name="Send-mail" action="mailto:email#email.com" method="post">
<label>Name:</label>
<input class="nice" name="name" type="text"/>
<label>Email:</label>
<input class="nice" name="email" type="text"/>
<label>Subject:</label>
<input class="nice" name="subject" type="text"/>`enter code here`
<label>Message:</label>
<text-area type="text" name="message"></text-area>
<input class="centered" type="submit" name="submit" value="Send email"/>
</form>
I need the server to send the email from the website to the email of our choir director.
You would need to create the PHP file where there is an email sending logic,eg: sendEmail.php. The form tag would change to:
<form name="Send-mail" action="sendEmail.php" method="post">
Below is how you can send the mail in php from form inputs.
<form action="mail.php" method="post">
<label>Name:</label>
<input class="nice" name="name" type="text"/>
<label>Email:</label>
<input class="nice" name="email" type="text"/>
<label>Subject:</label>
<input class="nice" name="subject" type="text"/>`enter code here`
<label>Message:</label>
<text-area type="text" name="message"></text-area>
<input class="centered" type="submit" name="submit" value="Send email"/>
</form>
You will have to name your php file mail.php like in case below
mail.php
<?php
$to = $_POST['email'];
$subject = $_POST['subject'];
$sender = $_POST['name'];
$message = $_POST['message'];
$message .= "<h1>This is Message from $sender.</h1>";
$header = "From:senderemail#somedomain.com \r\n";
$header .= "Cc:senderemail#somedomain.com \r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-type: text/html\r\n";
$retval = mail ($to,$subject,$message,$header);
if( $retval == true ) {
echo "Message sent successfully...";
}else {
echo "Message could not be sent...";
}
?>
You will need to ensure that email for this line of code is correctly set.
$header = "From:senderemail#somedomain.com \r\n";
$header .= "Cc:senderemail#somedomain.com \r\n";
In otherword, if your site email is support#hey.com
Try Replacing it snderemail#somedomain and it will work. You can also try the replacement with some other emails like good working gmail and let me know what happen. Thanks

I do not now why my form fails

I wrote this html and php code to send a form, but I do not understand why it fails.
my code :
html
<form action="formulari.php" method="post">
<p>
Nombre:<input name= "name" type="text";>
</p>
<p>
email:<input name= "email" type="text";>
</p>
<p>Comentario:<textarea rows="4" cols="50">
</textarea name= "message"></p>
<input type="submit" value="enviar" >
<input type="reset" value="borrar" >
</form>
php
<?php
$name = $_POST [ 'name'];
$email = $_POST [ 'email'];
$message = $_POST['comentari'];
$to = "email#gmail.com";
$subject = "Formulari_contacte";
mail ( $to, $subject, $message, $email);
header('Location: ../index.html?message=form_submitted');
?>
Can anyone help ?
Try the following:
HTML:
<form action="formulari.php" method="post">
<p>Nombre: <input name="name" type="text"></p>
<p>email: <input name="email" type="text"></p>
<p>Comentario: <textarea rows="4" cols="50" name="message"></textarea></p>
<input type="submit" value="enviar">
<input type="reset" value="borrar">
</form>
PHP
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$to = "email#gmail.com";
$subject = "Formulari_contacte";
$mail = mail( $to, $subject, $message, $email);
// Check if email is send
if( $mail ) {
header('Location: ../index.html?message=form_submitted');
} else {
echo 'Email not send';
}
?>
I removed the ";" and unnecessary spaces in HTML and PHP. You also had the name of the textarea in the closing tag.
Also note that you are open for injection. Users are able to write some javascript code to you.
If you analyze the html code, in that way you text area will miss the name.
You need to set the name of the text area in the opening tag
<textarea name="comment">Enter text here...</textarea>
Plus your are using a different name in the server side, if you are using name ='message' you will need to use 'message' in to get the POST value
Reference:
http://www.w3schools.com/tags/att_textarea_name.asp
I guess you are unable to fetch textarea value
<form action="formulari.php" method="post">
<p>Nombre:<input name= "name" type="text"></p>
<p>email:<input name= "email" type="text"></p>
<p>Comentario:
<textarea name= "comentari" rows="4" cols="50">
</textarea>
</p>
<input type="submit" value="enviar" >
<input type="reset" value="borrar" >
</form>
Thanks to all that answered my question, with your help and google i have made this formulary that has a anti-spam protection and a system to send the user to a page if the mail has been send.
HTML:
<form action="formulari.php" method="post">
<p>Nombre: <input name="name" type="text"></p>
<p>email: <input name="email" type="text"></p>
<input name="edat" style=" display:none;">
<p>Comentario: <textarea rows="4" cols="50" name="message"></textarea></p>
<input type="submit" value="enviar">
<input type="reset" value="borrar">
</form>
PHP:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$to = "xavicarreragimbert#gmail.com";
$subject = "Formulari_contacte";
if (!empty($_POST['edat'])){
echo "Lo sentimos el sistema antispam ha detectado que es possiblemente un envio realizado a traves de spam";
}
else{
mail( $to, $subject, $message, $email);
header('Location: ../index.html?message=form_submitted');
}
?>

Can someone find where's wrong in this code PHP email

I'm not receiving mails on the email mail#example.com. Below is my form code and my send-mail.php code. Can anyone help me with this cause everything seems working great bu i'm not receiving any emails. I'm using localhost as the server.
Contact form:
<form id="contactForm" action="#" method="post">
<p>Email us by filling in the form below. Make sure you fill in the message and all fields.</p>
<fieldset>
<div>
<input name="name" id="name" type="text" class="form-poshytip" title="Enter your name" />
<label>Name</label>
</div>
<div>
<input name="web" id="web" type="text" class="form-poshytip" title="Enter your surname" />
<label>Surname</label>
</div>
<div>
<input name="email" id="email" type="text" class="form-poshytip" title="Enter your email address" />
<label>Email</label>
</div>
<div>
<textarea name="comments" id="comments" rows="5" cols="20" class="form-poshytip" title="Enter your comments"></textarea>
</div>
<!-- send mail configuration -->
<input type="hidden" value="mail#example.com" name="to" id="to" />
<input type="hidden" value="Enter the subject here" name="subject" id="subject" />
<input type="hidden" value="send-mail.php" name="sendMailUrl" id="sendMailUrl" />
<!-- ENDS send mail configuration -->
<p><input type="button" value="Send" name="submit" id="submit" /> <span id="error" class="warning">Message</span></p>
</fieldset>
</form>
<p id="sent-form-msg" class="success">Form data sent. Thanks for your feedback.</p>
<!-- ENDS form -->
and here is the send-mail.php
<?php
//vars
$subject = $_POST['subject'];
$to = explode(',', $_POST['to'] );
$from = $_POST['mail#example.com'];
//data
$msg = "NAME: " .$_POST['name'] ."<br>\n";
$msg .= "EMAIL: " .$_POST['email'] ."<br>\n";
$msg .= "WEBSITE: " .$_POST['web'] ."<br>\n";
$msg .= "COMMENTS: " .$_POST['comments'] ."<br>\n";
//Headers
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=UTF-8\r\n";
$headers .= "From: <".$from. ">" ;
//send for each mail
foreach($to as $mail){
mail($mail, $subject, $msg, $headers);
}
?>
$_POST['subject'];
$_POST['to'];
$_POST['myemail#gmail.com'];
$_POST['name'];
$_POST['email'];
$_POST['web'];
$_POST['comments'];
I didn’t find any of these elements in your form. That's the reason why nothing is happening.Try
echo '<pre>';
print_r($_POST);
This will give you the posted array when the form is submitted.
i have some suggestion.if u have kept the 'to' address as hidden in the form then why cant u try keeping it directly in sendmail function and in $from you try to keep
<?php
$to="kurtfarrugia92#gmail.com";
$from =$_POST['field_name'];
// not the mail id because i didn't see any field with name as "kurtfarrugia92#gmail.com"
?>
You cannot use this function to send mail from localhost. I am not sure but you should try PHP mailer for this task.

Categories