Results 1 to 5 of 5

Thread: PHP Help

  1. #1
    SG Stud ColdFusion's Avatar
    Join Date
    Oct 2000
    Location
    Vancouver, BC
    Posts
    3,542

    PHP Help

    I have no idea what i'm doing. Just found some code around and put it together. I get to the success page but don't end up getting an email. Any ideas?

    Code:
    <?php
    $yName = Trim(stripslashes($_POST['yName']));
    $LastName = Trim(stripslashes($_POST['LastName']));
    $yEmail = Trim(stripslashes($_POST['yEmail']));
    $EmailFrom = "me@me.com";
    $EmailTo = "me@me.com";
    $Subject = "Email Unsubscription";
    
    // validation
    $validationOK=true;
    if (Trim($yEmail)=="") $validationOK=false;
    if (Trim($yName)=="") $validationOK=false;
    if (!$validationOK) {
      echo "Error! Please enter your name and email address to unsubscribe.";
    //print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
      exit;
    }
    
    $message = '
    
    <html>
    <body>
      Name: <$yName>
      Email: <$yEmail>
    </body>
    </html>
    ';
    
    // To send HTML mail, the Content-type header must be set
    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    
    // Additional headers
    $headers .= 'To: Me <me@me.com>, Test <test@test.com>' . "\r\n";
    $headers .= 'From: SC Unsubcribe <unsub@test.com>' . "\r\n";
    
    // send email 
    $success = mail($EmailTo, $Subject, $message, $headers);
    
    
    
    // redirect to success page 
    if ($success){
      echo "Thank You. Your request has been submitted.";
    //  print "<meta http-equiv=\"refresh\" content=\"0;URL=ok.htm\">";
    }
    else{
      echo "Sorry. We are currently experiencing technical difficulties. You may try later or manually send your request to: me@me.com";
    //print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
    }
    ?>
    Thanks!!
    Edit: I know I need to change the fields for my email address
    Last edited by ColdFusion; 10-16-09 at 11:19 PM.

  2. #2
    Elite Member TonyT's Avatar
    Join Date
    Jan 2000
    Location
    Fairfax, VA
    Posts
    10,336
    You don't need headers declared, assuming you are using a html form try this:
    Code:
    <?php
    if ($_POST['submit'] != TRUE) { //if submit not pressed bounce to home page
    echo "<meta http-equiv='refresh' content='0;url=./'>";
    exit;
    }
    
    elseif ($_POST['submit'] == TRUE) { //if submit button pressed process form
    
    $yName = Trim(stripslashes($_POST['yName']));
    $LastName = Trim(stripslashes($_POST['LastName']));
    $yEmail = Trim(stripslashes($_POST['yEmail']));
    $EmailFrom = "me@me.com";
    $EmailTo = "me@me.com";
    $Subject = "Email Unsubscription";
    // format message
    $message = 'Name: <$yName>\nEmail: <$yEmail>';
    
    //prevent header injection, bounce to google if fields contain carriage returns
    if (ereg("[\r\n]", $yName) || ereg("[\r\n]", $LastName) || ereg("[\r\n]", $yEmail)) {
    echo "<meta http-equiv='refresh' content='0;url=http://www.google.com'>";
    exit;
    }
    
    // validation
    $validationOK=true;
    if (Trim($yEmail)=="") {
    	$validationOK=false;
    	}
    if (Trim($yName)=="") {
    	{ $validationOK=false;
    	}	
    if (!$validationOK) {
    	echo "Error! Please enter your name and email address to unsubscribe.";
    	echo "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
    	}
    elseif (!$validationOK) { // send mail, inform user mail was sent, redirect to success page after 3 seconds
    	mail($EmailTo, $Subject, $message);
    	echo "Thank You. Your request has been submitted.";
    	echo "<meta http-equiv=\"refresh\" content=\"3;URL=ok.htm\">";
    	}
    else { // if there's a problem with the mail server
      	echo "Sorry. We are currently experiencing technical difficulties. You may try later or manually send your request to: me@me.com";
    	}
    } // end if submit button pressed
    ?>
    BTW, better to validate the fields using javascript and then post the form.
    No one has any right to force data on you
    and command you to believe it or else.
    If it is not true for you, it isn't true.

    LRH

  3. #3
    SG Stud ColdFusion's Avatar
    Join Date
    Oct 2000
    Location
    Vancouver, BC
    Posts
    3,542
    Tony! You freaking rock ... thanks so much! I really appreciate the reply.

    Getting a little error message:

    Parse error: syntax error, unexpected $end in /home/mcmatt/mattkurtenbach.net/j/unsubscribe_email.php on line 45

    I checked all the brackets (the only thing that came to mind) but they all seem to match up.

  4. #4
    Elite Member TonyT's Avatar
    Join Date
    Jan 2000
    Location
    Fairfax, VA
    Posts
    10,336
    Try this. If OK, we will implement validation using javascript.

    Code:
    <?php
    // prevent page from being cached
    header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); 
    header('Cache-Control: no-store, no-cache, must-revalidate'); 
    header('Cache-Control: post-check=0, pre-check=0', FALSE); 
    header('Pragma: no-cache');
    
    if ($_POST['submit'] != TRUE) { //if submit not pressed bounce to home page
    echo "<meta http-equiv='refresh' content='0;url=./'>";
    exit;
    }
    
    elseif ($_POST['submit'] == TRUE) { //if submit button pressed process form
    
    $yName = Trim(stripslashes($_POST['yName']));
    $LastName = Trim(stripslashes($_POST['LastName']));
    $yEmail = Trim(stripslashes($_POST['yEmail']));
    $EmailFrom = "me@me.com";
    $EmailTo = "me@me.com";
    $Subject = "Email Unsubscription";
    // format message
    $message = 'Name: <$yName>\nEmail: <$yEmail>';
    
    //prevent header injection, bounce to google if fields contain carriage returns
    if (ereg("[\r\n]", $yName) || ereg("[\r\n]", $LastName) || ereg("[\r\n]", $yEmail)) {
    echo "<meta http-equiv='refresh' content='0;url=http://www.google.com'>";
    exit;
    }
    
    mail($EmailTo, $Subject, $message);
    echo "Thank You. Your request has been submitted.";
    echo "<meta http-equiv=\"refresh\" content=\"3;URL=ok.htm\">";
    } // end if submit button pressed
    ?>
    Last edited by TonyT; 10-18-09 at 08:37 AM.
    No one has any right to force data on you
    and command you to believe it or else.
    If it is not true for you, it isn't true.

    LRH

  5. #5
    Elite Member TonyT's Avatar
    Join Date
    Jan 2000
    Location
    Fairfax, VA
    Posts
    10,336
    Assuming your form inputs are 3:
    first name
    last name
    user's email address

    Put the php in a file named send_mail.php.

    Put the html code in a page called anything you want, probably unsubscribe.html. The javascript checks for empty form fields and also checks if email address is valid and forces lower case for email address. Errors are displayed in red text next to the form field.

    Code:
    <?php // name the php page: send_mail.php
    header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); 
    header('Cache-Control: no-store, no-cache, must-revalidate'); 
    header('Cache-Control: post-check=0, pre-check=0', FALSE); 
    header('Pragma: no-cache');
    
    if ($_POST['submit'] != TRUE) { //if submit not pressed bounce to home page
    echo "<meta http-equiv='refresh' content='0;url=./'>";
    exit;
    }
    
    elseif ($_POST['submit'] == TRUE) { //if submit button pressed process form
    $recipient	= "me@me.com";
    $first_name	= Trim(stripslashes($_POST['first_name']));
    $last_name	= Trim(stripslashes($_POST['last_name']));
    $your_email	= Trim(stripslashes($_POST['your_email']));
    $subject	= "Email Unsubscription";
    $ip		= $_SERVER['REMOTE_ADDR']; // grab sender's ip address
    $sender_name	= $first_name . " " . $last_name; // join first & last into one variable
    // format message
    $msgformat	= 'Sender IP Address: $ip\nName: $sender_name\nEmail: $your_email';
    
    //prevent header injection, bounce to google if fields contain carriage returns
    if (ereg("[\r\n]", $yName) || ereg("[\r\n]", $LastName) || ereg("[\r\n]", $yEmail)) {
    echo "<meta http-equiv='refresh' content='0;url=http://www.google.com'>";
    exit;
    }
    
    // send the message
    mail($recipient, $subject, $msgformat, "From: $sender_name <$your_email>");
    echo "Thank You. Your request has been submitted.";
    echo "<meta http-equiv='refresh' content='0;url=./'>"; // redirect to home page
    } // end if submit button pressed
    ?>
    
    
    ----------------------------------------------------------------------------------
    
    
    <!-- all below goes on the html page -->
    
    <!-- initially hides error messages, display them in red when needed -->
    <style type="text/css">
    .error {
    vertical-align:top;
    color:#ff0000;
    display:none;}
    </style>
    
    <!-- begin form validation -->
    <script type='text/javascript'>
    function validate() {
    first_name = document.getElementById("first_name").value;
    last_name = document.getElementById("last_name").value;
    your_email = document.getElementById("your_email").value;
    filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
      if (first_name == "") {
      hideAllErrors();
      document.getElementById("first_nameError").style.display = "inline";
      document.getElementById("first_name").focus();
      return false;
      }
      else if (last_name == "") {
      hideAllErrors();
      document.getElementById("last_nameError").style.display = "inline";
      document.getElementById("last_name").focus();
      return false;
      }
      else if (your_email == "") {
      hideAllErrors();
      document.getElementById("your_emailError").style.display = "inline";
      document.getElementById("your_email").focus();
      return false;
      }
      else if (!filter.test(your_email)) {
      hideAllErrors();
      document.getElementById("invalid_emailError").style.display = "inline";
      document.getElementById("your_email").select();
      document.getElementById("your_email").focus();
      return false;
      }
    return true;
    }
    function hideAllErrors() {
    document.getElementById("first_nameError").style.display = "none"
    document.getElementById("last_nameError").style.display = "none"
    document.getElementById("your_emailError").style.display = "none"
    document.getElementById("invalid_emailError").style.display = "none"
    }
    </script><!-- end form validation -->
    
    <form onsubmit="return validate();" name="contact_form" method="post" action="send_mail.php">
    <table>
    <tr><td style="text-align:left;"><p>First Name:</p></td><td style="text-align:left;"><input type="text" id="first_name" name="first_name" size="30" maxlength="48" value="">&nbsp;<span class="error" id="first_nameError">Please enter your first name.</span></td></tr>
    <tr><td style="text-align:left;"><p>Last Name:</p></td><td style="text-align:left;"><input type="text" id="last_name" name="last_name" size="30" maxlength="48" value="">&nbsp;<span class="error" id="last_nameError">Please enter your last name.</span></td></tr>
    <tr><td style="text-align:left;"><p>Your Email:</p></td><td style="text-align:left;"><input type="text" id="your_email" name="your_email" size="30" maxlength="48" value="">&nbsp;<span class="error" id="your_emailError">Please enter your email address.</span><span class="error" id="invalid_emailError">Error! Invalid email address.</span></td></tr>
    <tr><td style="text-align:left;" colspan="2"><input type="submit" name="submit" value="Send"></td></tr>
    </table>
    </form>
    Last edited by TonyT; 10-18-09 at 09:56 AM.
    No one has any right to force data on you
    and command you to believe it or else.
    If it is not true for you, it isn't true.

    LRH

Similar Threads

  1. Need feedback on PHP programmer tool...
    By jezeus in forum Programming Forum
    Replies: 0
    Last Post: 01-23-09, 12:22 AM
  2. I am looking for a PHP algorithm/script
    By Tech Manager in forum Programming Forum
    Replies: 0
    Last Post: 09-30-08, 01:37 PM
  3. PHP MySQL dates
    By jorefice in forum Programming Forum
    Replies: 2
    Last Post: 05-07-07, 08:20 AM
  4. PHP on IIS 6 on Win 2003 server
    By amhood in forum General Broadband Forum
    Replies: 0
    Last Post: 12-13-06, 03:46 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •