Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP email script 2

Status
Not open for further replies.

HigherHigher

IS-IT--Management
Feb 7, 2005
37
0
0
CA
I build a form for a contact page using PHP script. It works fine. When I use the same script for e-newsletter on home page, it submits name and email address and returns to the home page immedately. One thing I noticed that I didn't return a "Thank you, Mr. Yield" sentence on a PHP script page. Why the contact page is working, not the home page?
 
It is because that little piece of script right there where I am pointing does not output it. In the same manner that you can see my finger pointing at your script, I can see your script and can thus advise you. Post the script that does not do what you want it to do.
 
I hope you could point that script out for me because I really don't know. Thanks!


<?php
#
# NL-PHPMail v1.5
# Copyright 2002-2003 Sasha, All rights reserved.
# First released December 11, 2002
# This version released October 6, 2003
#
# NL-PHPMail v1.5 is linkware and can be used or modified as long as this note remains intact
# and unaltered, and there is a link up back to so people know where you got this script. You are forbidden to sell or distribute the code of NL-PHPMail v1.5, in whole
# or part, without the written consent of Sasha.
#
# By using NL-PHPMail v1.5 you agree to indemnify Sasha from any liability.
#
# Website: # FAQ: # Support: #

// -----------------------
//VARIABLES NEEDED FOR THE SCRIPT TO WORK, PLEASE CHANGE THESE AS SPECIFIED
// -----------------------

//Your email address (where the form will be sent):
$recipient = 'email@address.com';

//The subject of the email:
$subject = 'e-Mewsletter Sign Up Form';

//Change this to the URL to your HTML form.
$form_url = '
//If you're using Headers and Footers with PHP, please fill in the path to them below. This can be a relative path (like, header.inc if it's in the same directory), or an absolute path. If you're NOT using headers, please leave this empty.
$header ='';
$footer ='';

//Below is the content of the actual email sent to you. If you have different or more form fields than specified here, copy and paste the below line (without the // in front of it) into the coding bit below. Beneath the Comments line would be a good place for it:
// $msg .= "Field Description: $_POST[field_name]\n";
// Change the description and field name to match the field in your form. The $_POST[field_name] variable is what takes the contents of the form fields, so change "field_name" to the name of your form field. The . in front of the = sign merely means that line will be added to the variable, along with all previous lines, instead of overwriting it, so don't take it out! Use \t to insert a tab, and \n to insert a new line.

$msg = "---------------\n";
$msg .= "$subject\n";
$msg .= "---------------\n\n";
$msg .= "Name: $_POST[name]\n";
$msg .= "Email: $_POST\n";


// Change the names below to the 3 required fields in your form. If these fields are not filled out, the script will not send an email and display an error message instead, asking the visitor to fill out the form again.
$req1 ="$name";
$req2 ="$email";

//The below error message will be displayed if not all required fields are filled in. Feel free to use normal HTML in this.
$error_required = '<p>It appears you forgot to enter either Your email address<br /> Your full name<br /><br />Please press the BACK button in your browser and try again.</p>';

//The below error message will be displayed if the visitor submitted an invalid email address. Feel free to use normal HTML in this.
$error_email = '<p>Oops, it appears that the email address you filled in is not a valid one. Please press the back button on your browser and try again. Please check carefully that you filled in all information in the right fields!</p>';

//The below success message will be displayed when the form is sent. Feel free to use normal HTML in this.
$success_message = '<p>Your e-newsletter submission has been sent. Thank you and have a great day. </p>';

// -----------------------
//DO NOT EDIT ANYTHING BELOW UNLESS YOU KNOW WHAT YOU'RE DOING!!
// -----------------------

if ($header ==""){
$use_headers ='0';
$header = '<html><head><title>e-Newsletter Sign Up Form</title>
</head><body>';
} else {$use_headers ='1';}
if ($footer ==""){
$use_footers ='0';
$footer = '<div id="content"></div></body></html>';
} else {$use_footers ='1';}

function is_valid_email ($address) {
return (preg_match(
'/^[-!#$%&\'*+\\.\/0-9=?A-Z^_`{|}~]+'.
'@'.
'([-0-9A-Z]+\.)+' .
'([0-9A-Z]){2,4}$/i',
trim($address)));
}
function is_valid_email_eregi ($address) {
return (eregi(
'^[-!#$%&\'*+\\./0-9=?A-Z^_`{|}~]+'.
'@'.
'([-0-9A-Z]+\.)+' .
'([0-9A-Z]){2,4}$',
trim($address)));
}

if (!isset($req1) || !isset($req2) || !isset($req3)) {
header( "Location: $form_url" ); }

elseif (empty($req1) || empty($req2) || empty($req3)) {

if ($use_headers =="0"){
echo "$header";
} else { include ("$header"); }
?>
<h2>Error</h2>
<?=$error_required?>


<?
if ($use_footers =="0"){
echo "$footer";
} else { include ("$footer"); }
}
else {

$f_email = $_POST[email];
$f_email = strtolower(trim($f_email));
if (is_valid_email($f_email)) {
$msg .= "\n\n---------------\n";
$msg .= "Life is good./\n";
$msg .= "---------------\n\n";

$mailheaders = "MIME-Version: 1.0 \n";
$mailheaders .= "Content-type: text/plain; charset=iso-8859-1 \n";
$mailheaders .= "From: \"$_POST[email]\" <$_POST[email]> \n";
$mailheaders .= "Reply-To: \"$_POST[email]\" <$_POST[email]> \n";
$mailheaders .= "X-Priority: 3 \n";
$mailheaders .= "X-MSMail-Priority: High \n";
$mailheaders .= "X-Mailer: PHP4";

mail($recipient, $subject, $msg, $mailheaders);

if ($use_headers =="0"){
echo "$header";
} else { include ("$header"); }
?>
<h2>Thank You, <?=$name?>. </h2>
<?=$success_message?>

<?
if ($use_footers =="0"){
echo "$footer";
} else { include ("$footer"); }
} else {
if ($use_headers =="0"){
echo "$header";
} else { include ("$header"); }
?>
<h2>Error</h2>
<?=$error_email?>
<?
if ($use_footers =="0"){
echo "$footer";
} else { include ("$footer"); }
} } ?>
 
HigherHigher said:
When I use the same script for e-newsletter on home page, it submits name and email address and returns to the home page immedately. One thing I noticed that I didn't return a "Thank you, Mr. Yield" sentence on a PHP script page. Why the contact page is working, not the home page?
From the code provided and the description given by you, I can deduce that the script for e-newsletter is not working. You say that it returns to the homepage immediately. If that happens, the email has not been sent but you were rather directed back to the home page because of an error. If the script sent the email correctly you should stay on the script page and see 'Thank you, ' 'Your e-newsletter submission has been sent. Thank you and have a great day.'. There should also be a name following Thank you, however I did not find where this name would be instantiated in the script. If you are redirected to home page immediately, check if the email is sent. It probably shouldn't and you will have to make sure that in the top part of the script you correctly fill out $req1, $req2 and $req3 variables.
 
I think this might be your problem:

SNIPPLET of your code:
Code:
// Change the names below to the [b]3 required fields[/b] in your form. If these fields are not filled out, the script will not send an email and display an error message instead, asking the visitor to fill out the form again.
$req1 ="$name";
$req2 ="$email";
// [b]did you delete one here?!??!?!?![/b]

WHen your script reaches this part:
Code:
if (!isset($req1) || !isset($req2) || [b]!isset($req3)[/b]) {
    header( "Location: $form_url" ); }

  elseif (empty($req1) || empty($req2) || empty($req3)) {

There is no $req3, since you delete it from the script.
That is what I think is happening.

How do you fix it?

Dirty fix:
* Set $req3 to something..
Clean fix:
* Clean up the code, remove un-needed comments, remove the validation of the unwanted variable, etc.

Also, in the future..
Remember that we can not guess, unless we see your code.
You apparently have some logical issues here, due to modifications of the script.

You may want to replace:
Code:
if (!isset($req1) || !isset($req2) || !isset($req3)) {
    header( "Location: $form_url" ); }

  elseif (empty($req1) || empty($req2) || empty($req3)) {

with:
Code:
if (!isset($req1) || !isset($req2)) {
    header( "Location: $form_url" ); }

  elseif (empty($req1) || empty($req2)) {

good luck!

Olav Alexander Mjelde
Admin & Webmaster
 
btw. did you read the comment:
Code:
// -----------------------
//[b]DO NOT EDIT ANYTHING BELOW UNLESS YOU KNOW WHAT YOU'RE DOING!![/b]
// -----------------------

lol..

Olav Alexander Mjelde
Admin & Webmaster
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top