Jump to content

[SOLVED] why won't this mail () redirect?


heathergem

Recommended Posts

When I added bcc in the headers, the .php quit redirecting after the form was sent. Before that it worked just fine. How am I messing up this code?

 

<?php
$email = $_REQUEST['email'] ;
$name = $_REQUEST['name'] ;
$to = $_REQUEST['to'] ;
$message = $_REQUEST['message'] ;
$subject = $_REQUEST['subject'] ;
$headers = "Location: http://www.example.com/thankyou.html \n" ;
$headers .= "Bcc: user@example.com \n" ;

if (!isset($_REQUEST['email'])) {
    header( "Location: http://www.example.com" ); 
  }
  elseif (empty($email) || empty($to)) {
    ?>

    <html>
    <head><title>Error</title></head>
    <body>
    <h1>Error</h1>
    <p>
    Oops, it appears you forgot to enter either your
    email address or your recipient's address. Please press the BACK
    button in your browser and try again.
    </p>
    </body>
    </html>

    <?
  }
  else {

mail( $to, $subject, $message, "From: $name <$email>", $headers);
}
?>

Link to comment
Share on other sites

To keep it simple and error free (since your relateively new to php) I would just add a

, emailhere to the current email address.

It'll send a copy to both listed email addresses, better than trying to add new variables when you are just starting.

If not then put here exactly what is happening when it runs.

Link to comment
Share on other sites

Not quite. I did just as you said and then deleted the headers and changed the last part of the script to:

 

mail( $to, $subject, $message, "From: $name <$email>" );
header ("Location: http://www.heathergemmen.com/thankyou.html" );
} 

 

Correct?

 

Otherwise, if I leave it as above, it does send the message to $to and to the BCC email, but then it stays on the php page. I'd like it to redirect to http://www.example.com/thankyou.html ... or perhaps I could add an echo to let them know the action has been successful and to give them a link back to my site.

Link to comment
Share on other sites

I meant leave everything you ahd but change that part.  Try this

<?php
$email = $_REQUEST['email'] . ', user@example.com' ;
$name = $_REQUEST['name'] ;
$to = $_REQUEST['to'] ;
$message = $_REQUEST['message'] ;
$subject = $_REQUEST['subject'] ;
$headers = "Location: http://www.example.com/thankyou.html \n" ;

if (!isset($_REQUEST['email'])) {
    header( "Location: http://www.example.com" ); 
  }
  elseif (empty($email) || empty($to)) {
    ?>

    <html>
    <head><title>Error</title></head>
    <body>
    <h1>Error</h1>
    <p>
    Oops, it appears you forgot to enter either your
    email address or your recipient's address. Please press the BACK
    button in your browser and try again.
    </p>
    </body>
    </html>

    <?
  }
  else {

mail( $to, $subject, $message, "From: $name <$email>", $headers);
}
?>

Link to comment
Share on other sites

I appreciate your help so much. I wish I could deliver a plate of warm cookies and a carton of milk to you.

 

Sadly, it's still not working. I checked the code five times to be sure I had it exactly right. I even tried it with and without the \n at the end of the header line. It's doing the same thing as before: staying on the php page without any error messages.

Link to comment
Share on other sites

** This was a quick write up and is just a quick attempt to get you something up and running.

<?php
if (isset($_POST['submit'])) { // make sure submit is set

$errorhandler = '';
if ($_REQUEST['email'] == "") {
$errorhandler .= 'email is required.<br />';
}
if ($_REQUEST['name'] == "") {
$errorhandler .= 'Name is required.<br />';
}
if ($_REQUEST['to'] == "") {
$errorhandler .= 'To field is required.<br />';
}
if ($_REQUEST['message'] == "") {
$errorhandler .= "message is required.<br />";
}
if ($_REQUEST['subject'] == "") {
$errorhandler .= "Subject is required.<br />";
}
if ($errorhandler != "") {
echo '<span style="color:red;">';
echo $errorhandler;
echo '</span>';
}
if ($errorhandler == "") {
$email = $_REQUEST['email'] . ', user@example.com';
$name = $_REQUEST['name'] ;
$to = $_REQUEST['to'] ;
$message = $_REQUEST['message'] ;

$subject = $_REQUEST['subject'] ;

mail( $to, $subject, $message, "From: $name <$email>", $headers);
header("Location: http://www.example.com/thankyou.html");
}

}
?>

If this code gives you too many problems then we'll right up something OO style using a class so it'll be easier for you to keep up with and make changes too.

Link to comment
Share on other sites

I copied and pasted your code, changed only the addresses to my email/site ... and it did the same thing: stayed on the php page. This time the messages did not go through either. Could it be an external problem?

 

I think maybe i'll go back to the way it was without the bcc. I don't need to be copied on the message anyway; that was simply to alleviate my curiosity. The main thing is that viewers can direct others to my site.

 

Thanks!

Link to comment
Share on other sites

Revert back to your original code.  Did you do the , emailaddresshere in your original code as I asked in the beginning.  If not then try that.  It's suppose to be able to take as many email addresses (separeted by comma's).  If this doesn't work, then tell me what is your level of php experience.  Perhaps I can help you put together something new that'll do what you are wanting.

Link to comment
Share on other sites

Okay, here's what I ended up with (see below). It copies me and assures the sender that the action has been completed. I believe it is also secure from spammers. (Correct?)

 

As for my level, I'm very much a beginner—but I'm a motivated learner. You have helped me tremendously.

 

 

<?php
$email = $_REQUEST['email'] ;
$name = $_REQUEST['name'] ;
$to = $_REQUEST['to'] ;
$message = $_REQUEST['message'] ;
$subject = $_REQUEST['subject'] ;
$header = "bcc: user@mail.com" ;

if (!isset($_REQUEST['email'])) {
    header( "Location: http://www.example.com" ); 
  }
  elseif (empty($email) || empty($to)) {
    ?>

    <html>
    <head><title>Error</title></head>
    <body>
    <h1>Error</h1>
    <p>
    Oops. It appears you forgot to enter either your
    email address or your recipient's address. Please press the BACK
    button in your browser and try again.
    </p>
    </body>
    </html>

    <?
  }
  else {

mail( $to, $subject, $message, "From: $name <$email>", $header);
echo ("Your message has been sent.") ;
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.