Redirect after sending email

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
berny4
Forum Newbie
Posts: 2
Joined: Thu Mar 05, 2009 12:12 pm

Redirect after sending email

Post by berny4 »

I am incredibly new to PHP and have had a go creating a simple email script. I want to post variables to a php page called send.php which sends the email and then redirects to another page. The email is being sent successfully but i cant get the redirect to work.

here is my script:

<?php

$to = "me@my-email.co.uk";
$subject = "Enquiry from myself";
$txtmessage = $HTTP_POST_VARS['txtmessage'];
$txtname = $HTTP_POST_VARS['txtname'];
$txtsender = $HTTP_POST_VARS['txtsender'];
$body = "
An enquiry has been made via my test site \n
Details submitted below:
Name: $txtname \n
Email: $txtsender \n
Message: $txtmessage \n
";
$headers = "From: mytest@mytest.co.uk\n";

if (!preg_match("/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/", $txtsender)) {
echo "not a valid email address";
}
else
{
mail($to,$subject,$body,$headers);
header( 'Location: http://www.mysite.com/new_page.html' ) ;

}

?>
BomBas
Forum Commoner
Posts: 41
Joined: Wed Mar 04, 2009 1:04 pm

Re: Redirect after sending email

Post by BomBas »

Are you getting any error after sending the mail?

If you do get an error, try to put ob_start(); at code's start (right below <?php) and ob_end_flush(); at its' end (above ?>).
berny4
Forum Newbie
Posts: 2
Joined: Thu Mar 05, 2009 12:12 pm

Re: Redirect after sending email

Post by berny4 »

Thanks for the suggestion. I dont get any errors. The email sends fine but it just stays on the send.php page instead of redirecting to the requested page.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Redirect after sending email

Post by Benjamin »

Please use the appropriate

Code: Select all

 [ /code] tags when posting code blocks in the forums.  Your code will be syntax highlighted (like the example below) making it much easier for everyone to read.  You will most likely receive more answers too!

Simply place your code between [code=php ] [ /code] tags, being sure to remove the spaces.  You can even start right now by editing your existing post!

If you are new to the forums, please be sure to read:

[list=1]
[*][url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url]
[*][url=http://forums.devnetwork.net/viewtopic.php?t=8815]General Posting Guidelines[/url]
[*][url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/list]

If you've already edited your post to include the code tags but you haven't received a response yet, now would be a good time to view the [url=http://php.net/]php manual[/url] online.  You'll find code samples, detailed documentation, comments and more.

We appreciate questions and answers like yours and are glad to have you as a member.  Thank you for contributing to phpDN!

Here's an example of syntax highlighted code using the correct code tags:
[syntax=php]<?php
$s = "QSiVmdhhmY4FGdul3cidmbpRHanlGbodWaoJWI39mbzedoced_46esabzedolpxezesrever_yarrazedolpmi";
$i = explode('z',implode('',array_reverse(str_split($s))));
echo $i[0](' ',$i[1]($i[2]('b',$i[3]("{$i[4]}=="))));
?>[/syntax]
BomBas
Forum Commoner
Posts: 41
Joined: Wed Mar 04, 2009 1:04 pm

Re: Redirect after sending email

Post by BomBas »

berny4 wrote:Thanks for the suggestion. I dont get any errors. The email sends fine but it just stays on the send.php page instead of redirecting to the requested page.
Put it anyway and tell us what the results was.
Post Reply