Header location after sending email [duplicate] - php

This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 8 years ago.
I'm trying redirect users to a thank you page after submitting a form but my header location doesn't seem to work (local or online). Nothing happens when clicking the submit button (the email is sent though).
My php looks like this :
<?php
$val= $_POST['val'];
$toemail='mail#mail.com';
$name = $val['name'];
$societe = $val['societe'];
$email = $val['email'];
$phone = $val['phone'];
$msg = $val['msg'];
$subject = 'Contact';
$headers = "From: $email \r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=utf-8\r\n";
$message = "<b>Nom : </b>".$name."<br>";
$message .='<b>Societe : </b>'.$societe."<br>";
$message .='<b>Email : </b>'.$email."<br>";
$message .='<b>Telephone : </b>'.$phone."<br>";
$message .='<b>Message : </b>'.$msg;
mail($toemail, $subject, $message, $headers);
header("Location: http://example.com/thankyou.html");
?>
What am I doing wrong? Thanks in advance for your help.
Edit: Thanks for your help. If I turn error reporting I get:
Warning : Cannot modify header information - headers already sent by (output started at /path/email.php:12)

As others have mentioned, the script is sending output before attempting to send the header.
The simplest way to fix it is probably to buffer the output of email() and then send the header, as follows:
ob_start();
mail($toemail, $subject, $message, $headers);
ob_end_clean();
header("Location: http://example.com/thankyou.html");

Problem is, you are sending output before redirect.
Either use output buffering or do it on a new page.
http://ee1.php.net/manual/en/ref.outcontrol.php

very simple use following method
ob_start();
exit(header("Location: http://example.com/thankyou.html"));
Functions that send/modify HTTP headers must be invoked before any output is made. Otherwise the call fails.

Using header() you can redirect only to a page within the same site.
you can try using
header("Location: /thankyou.html");
Hope it helps.

Related

Why Headers Preventing mail from sending?

This might sound similar like previously asked questions but trust me it's not
I Was trying to send an email that uses an HTML template via PHP mail() function from Localhost and a Hostinger Server but they created different problems.
On localhost the email was being sent as plain text although there were headers
$headers =
"MIME-Version: 1.0\r\n" .
"Content-Type: text/html; charset=UTF-8";
I have gone through all the similar questions in stackoverflow and tried each and every thing but I couldn't make it work. After some more research on this I found out this
I assume, that your email client is considering the smtp-server "unsafe", and hence is just going to display all the html as plaintext, rather than rendering it
Therefore I switched over my hosting and tried to do the same but this time I found that the headers are causing the problem. The email is not sent if the header variable is passed in the mail() function. I tried to concatenate the headers which didn't worked. Then I made an array of headers and joined them with php implode which too didn't worked. On a similar question on stackoverflow I found that webmails mess up if html, head, body tags are used as they use xhtml. I removed them and still no success.
I tried error reporting too and it showed module sqlite3 already loaded which I think is not related to mail.
Below is my code
php
<?php
$email_template = file_get_contents("path/to/my/template");
$lucky_number = rand(999999, 111111);
$email_template = str_replace("{{user}}", "User", $email_template);
$email_template = str_replace("{{lucky_number}}", $lucky_number, $email_template);
$sender = "from:iusername#host.com"; // I found that if I dont use from, my mail ends up in spam folder
$receiver = "username#host.com";
$subject = "Random Subject Name";
$headers =
"MIME-Version: 1.0\r\n" .
"Content-Type: text/html; charset=UTF-8";
if(mail($receiver, $subject, $email_template, $sender, $headers))
{
echo "Email Sent Successfully";
}
else
{
echo "Email Sending Failed";
}
P.S I can't use PHPMailer or other similar libraries
The sender information should be inside the headers
Hence, please change the following lines:
$headers =
"MIME-Version: 1.0\r\n" .
"Content-Type: text/html; charset=UTF-8";
if(mail($receiver, $subject, $email_template, $sender, $headers))
to
$sender = "iusername#host.com";
$headers = "From: $sender <$sender>\r\nReply-To: $sender\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=utf-8\r\n";
if(mail($receiver, $subject, $email_template, $headers))

Sending email from html form using php [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 6 years ago.
Ive been trying this out the whole day but I cant figure out how to send an email from my html contact form containing the information from the form to my email address. Im new to php.
Ive tried running this by uploading it to free web hosting. I get the message "success!" when I press the submit button on my html form but no email is actually sent.
Any help is appreciated.
PHP script:
<?php
//Subject
$subject ="Contact Form Submission";
// Name
$name =$_POST['InputName'];
// Message
$message =$_POST['InputMessage'];
//Mail of Sender
$email =$_POST['InputEmail'];
//From
$header = "From:$name<$email>";
$send_contact=mail("myemail#gmail.com",$subject,$message,$header);
//Check if mail was sent
if($send_contact){
echo "Success!";
}
else {
echo "Error!";
}
?>
EDIT: Figured it out after one whole day of trial and error. The problem was with the free web host I was using. Changed hosts and the code started working fine. Hope this helps someone in the future. Thanks all for the help.
I have a pretty good idea why your code is not working. It happened to me a long time ago. The reason why your code is not working is because :
When you pass "from" in headers, php expects an existing email account of your
server. For example : $headers = 'From: emailacc#yourserver.com';
So first thing you gotta do is create an email account on your server. And then put the From in header to the email address that you've just created.
The From field in the $headers is not the From as you think.
<?php
$email = $_POST["InputEmail"];
$subject = $_POST["InputSubject"];
$message = "From: ".$email.", ".$_POST["InputMessage"]; // you put the email address from the input form here
$headers = 'From: emailacc#yourserver.com'; // here is the email address specified from which u want to send the email.(i.e. your server email address)
mail($to, $subject, $message, $headers)
?>
I'm sure this will do the job :)
Have a shot at this.
I changed you're $header variable around a little bit, so that rather than setting the email as "$email", It'll actually pass through the posted email entered in the form. This apply's to the name too.
I also made it so that you pass the mail function through the parameters of the if statement, rather than setting a new variable.
$headers = "From: " . $name . "<" . $email . ">"; // notice new concatenation
if(mail("myemail#gmail.com", "Contact Form Submission", $message, $headers)){
// success message
} else {
// error message
}
Really hope this helps! :)
Try adding spaces after the "=" that might be the problem,
If that doesn't work you could try to use this
<?php
$emailvariable = $_POST['InputEmail']
$to = 'example#gmail.com';
$subject = "Form"
$message = $_POST['InputMessage'];
$headers = "From: $emailvariable";
mail($to, $subject, $message, $headers);
?>
Hope this helps

submit form and load new page

I'm trying to get a JQM page to send an email via a form and then redirect to a new page. I have the mail part working fine, but the redirect not so much! After the form has been submitted and mail sent, the page basically just refreshes instead of loading the new page. I've tried many different things using the code header('Location:myredirectpage'); but nothing works.
The following php code is located below my closing HTML tag at the bottom of my page.
<?php
if(isset($_POST['mr'])) {
$to = "myemailaddress";
$subject = "Subject";
$content = ""
."Survey Details"."\n\n"
."How did you hear about us: ".$_POST['mr']."\n";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: <myemailaddress>\r\n";
mail($to, $subject, $content, $headers);
$sendit= #mail($to, $subject, $content, $headers);
if($sendit){
header('Location:myredirectpage');
}else{ echo "Email failed to send";}
}
?>
To redirect your page after it has been sent, you need something in the following format:
header('Location: index.php');
Change index.php to the FILENAME of what you want to get to.
This can be a full URL such as http://google.com
You are sending the mail twice : mail(...) then #mail(...).
Also using #mail(...) with # is considered bad practice.
Instead of header('Location:myredirectpage'); use
echo '<script>window.location = 'yourpage'</script>'
Try this

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');

Mail sent with mail() function does not get sent [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
php mail function
I am doing this website where I send emails to users when they for example forget their password or similar, but for some reason I can not send emails with the following function:
$email = 'somemail#mail.com';
$subject = 'subject';
$message = 'message blablablablabla';
mail($email, $subject, $message);
Am I doing something wrong or missing something in the code, or is it the hosting company's fault? (I make my website on x10hosting.com). I checked in the manual about mail() but it didn't help me.
Thanks in advance.
Update
Thanks for the help guys, but it turned out to be a problem on the web hosting company I'm on. Everything's working fine now.
try to use with headersenter code here
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=iso-8859-1\r\n";
$headers .= "Content-Transfer-Encoding: 7bit\r\n";
$headers .= "From: <".$frommail.">\r\n";
$headers .= "Reply-To: ".$frommail."\r\n";
$mail_sent=mail($tomail, $msg, $headers);`enter code here`
I would guess either there is no sendmail_from value set in php.ini or your host does not support email or has not set it up correctly.
Try setting a from header, and if that doesn't work, contact your host:
mail($email, $subject, $message,'From: you#example.com');
is this on local host? or is it on a webserver
Also remember that mail($to, $subject, $contents) returns a boolean,
if(mail($to, $subject, $body){
echo "Message has been sent";
}
else{
echo "Error has occurred"
}

Categories