php header function not working after mail function [duplicate] - php

This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 8 years ago.
The form is submitting and sending the email successfully, but the page not redirecting to google. What am I doing wrong?
<?php
$to = "test#gmail.com";
$email_title = "test from email";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = ($_POST["name"]);
$email = ($_POST["email"]);
$message = ($_POST["message"]);
$info = " Name: $name \r\n Email: $email";
if(mail($to, $email_title, $message, $info)){
header('Location: http://www.google.com/');
}
else {
echo "there is an error";
}
}
?>

Without knowing more (for example, what the actual error looks like or what actually happens in the browser), I can only speculate. Headers can only be sent before any data is sent to the browser. My best guess is something somewhere is sending data down to the browser before the header() call, which therefore negates the header redirect.

Related

PHP - Contact form send mail with wrong characters (cannot accept UTF-8) [duplicate]

This question already has answers here:
How can I send email with UTF-8 encoding?
(2 answers)
UTF-8 all the way through
(13 answers)
Closed 4 years ago.
I tried several times to implement UTF-8 for receiving mails from my php contact form, but without any luck.
Code looks like this:
<?php
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$subject = $_POST['subject'];
$mailFrom = $_POST['email'];
$message = $_POST['message'];
$mailTo = "info#mnbl.eu";
$headers = "Od: ".$mailFrom;
$txt = "Dostal jste e-mail od ".$name.".\n\n".$message;
mail($mailTo, $subject, $txt, $headers);
if (empty($name) || empty($subject) || empty($mailFrom) || empty($message)) {
header("Location: ../page-contact-form.php?form=empty");
exit();
} else {
if (!filter_var($mailFrom, FILTER_VALIDATE_EMAIL)) {
header("Location: ../page-contact-form.php?form=email");
exit();
} else {
header("Location: ../page-contact-form.php?mailsend");
exit();
}
}
}
?>
I have <meta charset="UTF-8"> in my head, accept-charset="utf-8"in form tag.
Still i recieve mails with weird symbols (input language is Czech).
Do I need anything else?
Thanks in advance.

PHP mail on submit [duplicate]

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 get a contact form using the built in mail function to work but I'm running into some problems.
What I would like:
On clicking 'submit' the form is sent to defined emailadres.php.
If sent successfully, a confirmation message is displayed.
If not sent, an error message is displayed.
What is currently happening:
Confirmation message already shows on page load; regardless of form submission.
Mail is not sent.
Code I'm using:
if ($_POST["submit"]) {
mail ($to, $subject, $body, $from);
$sendErr = "Your message has been sent";
} else {
$sendErr = "Your message could not be sent";
}
I'm fairly new to all this so any help figuring out where my thinking stalls would be appreciated. If I need to post more parts of the form I will.
The code you are using does not even check if the mail was sent successfully, it only checks it the formular was submitted. mail() returns true if the mail was sent successfully, false if not. So you can check its return value:
if ($_POST["submit"]) {
$sent = mail ($to, $subject, $body, $from);
// Check here if the mail was sent or not
if ($sent) {
$sendErr = "Your message has been sent";
} else {
$sendErr = "Your message could not be sent";
}
}

PHP Sending Email Comma error [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 6 years ago.
I am having trouble with sending emails through PHP.
So I have a form which submits to a PHP script which sends an email:
<?php
$to = "myemail#email.com";
$subject = "[Contact Form]";
$name = $_POST["name"];
$contactNumber = $_POST["contactNumber"];
$email = $_POST["email"] ;
$message = $_POST["message"];
$body = "Someone has sent a new message from the contact form. \n \n Message from: " . $name . "\n Contact Number: ". $contactNumber ."\n Email: ". $email ."\n \n Message: ". $message;
if (mail($to, $subject, $body)) {
echo ("<p>Email successfully sent!</p>");
} else {
echo ("<p>Email delivery failed…</p>");
}
?>
And the email is sent fine when for example the message is one line such as:
"Hi there how is it going?"
And fine if it is multiple lines such as
"Hi there
how is it going?"
But when I try and type a message with a comma such as
Hello there,
how is it going?
It fails?
Is there a way I can just treat the whole thing as a string possibly? Would this also fail on any other characters or is this issue just because of the way I am writing the PHP script?
This might be an obvious fix but I am new to PHP so apologies! I have tried looking around for an answer but nothing seems to fix what I am looking for.
Thanks for any help!
Try using headers in your mail function, like this:
$headers = 'MIME-Version: 1.0\r\n';
$headers .= 'Content-type: text/html; charset=UTF-8\r\n';
mail($to, $subject, $body, $headers)

PHP form: Cannot modify header information - headers already sent [duplicate]

This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 9 years ago.
I know that this question has been asked many times, however, I can't seem to find solutions that are relevant to my situation, since they mostly deal with wordpress.
Here is my mail form:
<?php
$to = "email#gmail.com" ;
$from = $_REQUEST['email'] ;
$name = $_REQUEST['name'] ;
$headers = "From: $from";
$subject = "Contact Submission From domain.com";
$fields = array();
$fields{"name"} = "name";
$fields{"title"} = "title";
$fields{"email"} = "email";
$fields{"phone"} = "phone";
$fields{"prefer_phone"} = "pref_phone";
$fields{"prefer_email"} = "pref_email";
$fields{"message"} = "message";
$fields{"referral"} = "referral";
$body = "Here is their submitted message:\n\n"; foreach($fields as $a => $b){ $body .= sprintf("%20s: %s\n\n",$b,$_REQUEST[$a]); }
if($from == '') {print "You have not entered an email, please hit back and resubmit";}
else {
$send = mail($to, $subject, $body, $headers);
if($send)
{header( "Location: http://www.domain.com/sent.html" );}
else
{print "We encountered an error sending your mail, please notify support#domain.com";}
}
?>
The email sends just fine, but I get the titular error for the redirect:
Warning: Cannot modify header information - headers already sent by (output started at /home/wills5/public_html/send_henry.php:1) in /home/wills5/public_html/send_email.php on line 23
Edit: It was frickin' whitespace before line 1 apparently, thanks guys.
If the message says the error is in line 1, then it is typically leading whitespace, text >or HTML before the opening
Chances are you have whitespace besides or above your <?php tag, or HTML or some other type of "output". Maybe even a byte order mark.
<?php
echo "Correct";
// or vvv, but not with echo
// header("Location: http://www.example.com/");
?>
(space)
(space) <?php
echo "Incorrect";
header("Location: http://www.example.com/");
?>
<div align="center">Text</div>
<?php
echo "Incorrect";
header("Location: http://www.example.com/");
?>
Footnote: As per Gavin's suggestion, and I quote: "It is good form to leave off the closing php tag on class files for this reason. It prevents the inadvertent inclusion of white-space after an included file."

getting error while redirecting [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
“Warning: Headers already sent” in PHP
I have a php page which when i fill out email filed and press enter it connect to the mail.php
in this page after sending mail i want to go back to the page that i was but it gives me this error :
Warning: Cannot modify header information - headers already sent by (output started at /home/mysite/public_html/users/teachers/mail.php:3) in /home/mysite/public_html/users/teachers/mail.php on line 15
this is the mail.php code :
<html>
<body>
<?php
$email = $_GET['email'] ;
$subject =$_GET['author'] ;
$message = $_GET['text'] ;
$to = "mail#mail.com";
$from = $email;
$headers = "From:" . $from;
mail($to,$subject,$message,$headers,$from);
?>
<script language="javascript">
alert('your mail has sent !');</script>
<?php
header('location:../teachers/index.php');
?>
</body>
</html>
what should i do ?
In HTTP, a response is split into two sections: headers and body. They are separated by a double line break.
By printing <html><body> at the top of your mail.php script, you have effectively told PHP you are done with headers and ready for output. As PHP is sending back the information to Apache, it has sent back the complete header set already (it needs to, because you have now started to send the actual response body).
You have two options:
Enable output buffering in your PHP installation (PHP will then buffer the response body until the end of the script's execution or until you explicitly call one of the ob*end() methods.
Change your page to send the email and then redirect before printing any output to the browser.
Stop sending anything on the page where redirection is expected.
your code should look like this
<?php
$email = $_GET['email'] ;
$subject =$_GET['author'] ;
$message = $_GET['text'] ;
$to = "mail#mail.com";
$from = $email;
$headers = "From:" . $from;
mail($to,$subject,$message,$headers,$from);
header('location:../teachers/index.php');
?>
You have something in your code which writes something to the output buffer. The redirect has to be done before any output is written to the output buffer.
This won't work:
<?php
echo 'bla';
header('Location: index.php');
This will work:
<?php
header('Location: index.php');

Categories