Thread: PHP Mail Form

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    1
    Rep Power
    0

    PHP Mail Form


    I'm new to PHP and I found this snippet of code online
    PHP Code:
     <?php
        
    // YOUR EMAIL ADDRESS
        
    define ("MYEMAIL","email@email.com"); // YOU MUST ENTER YOUR EMAIL HERE

    function show_form()
    {
        
    $txt  "<p class=\"title\" align=\"center\"><b></b></p>";
        
    $txt .= "<form name=\"f2email\" action=\"".$_SERVER['PHP_SELF']."\" method=\"POST\">";
        
    $txt .= "<table align=\"center\">";
        
    $txt .= "<tr><td>First Name:</td><td><input type=\"text\" name=\"firstname\" /></td></tr>";
        
    $txt .= "<tr><td>Last Name:</td><td><input type=\"text\" name=\"lastname\" /></td></tr>";
        
    $txt .= "<tr><td>Your Email:</td><td><input type=\"text\" name=\"email\" /></td></tr>";
        
    $txt .= "<tr><td>Your Product:</td><td><input type=\"text\" name=\"product\" size=\"40\" /></td></tr>";
        
    $txt .= "<tr><td colspan=\"2\">Your Comment:</td></tr>";
        
    $txt .= "<tr><td colspan=\"2\"><textarea name=\"comment\" rows=\"5\" cols=\"60\"></textarea></td></tr>";
        
    $txt .= "<tr><td colspan=\"2\" align=\"right\">";
        
    $txt .= "<input type=\"button\" name=\"send\" value=\"Send\" 
                    onclick=\"js_validate(document.forms['f2email']); return false;\" /></td></tr>"
    ;
        

        
    $txt .= "</table>";
        
    $txt .= "<input type=\"hidden\" name=\"submitted\" value=\"submitted\" />";
        
    $txt .= "</form>";
        return(
    $txt);
    }
    function 
    treat_submission()
    {
        
    $firstname $_POST['firstname'];    // VISITOR'S NAME
        
    $lastname $_POST['lastname'];    // VISITOR'S NAME
        
    $email $_POST['email']; // VISITOR'S EMAIL
        
    $product $_POST['product'];
        
    $comment $_POST['comment'];
        
    $fullname $firstname ' ' $lastname;
        if (
    MYEMAIL == "")
            
    $my_email $_POST['toemail']; // use this line if MYEMAIL is not defined
        
    else
            
    $my_email MYEMAIL;    // Use this one if MYEMAIL is defined
        // =========================================================
        // THE MESSAGE
        
    $headers "To: ".$my_email"\r\n";
        
    $headers .= "From: ".$email"\r\n";
        
    $subject .= $product"\r\n";
        
    $msg $fullname" has submitted a comment:\r\n\r\n";
        
    $msg .= $comment."\r\n\r\n";
        
    mail($my_email$subject$msg$headers);
        
    // MESSAGE TO VISITOR:
        
    $headers "To: ".$email"\r\n"// this is the email given by the visitor
        
    $headers .= "From: ".$my_email"\r\n";
        
    $subject "Your message to ".$my_email;
        
    $msg "Thank you for contacting us.\r\n\r\n";
        
    $msg .= "We will reply to your message shortly.\r\n\r\n";
        
    mail($email$subject$msg$headers);
        
    $txt "<p align=\"center\">Your email has been sent</p>";
        return(
    $txt);
    }
    ?>
    <script type='text/javascript' language='JavaScript'>
    function showform(theForm)
    {
        var txt = "Form name: " + theForm.name + "\r\n";
        txt += "theForm.elements.length = "+theForm.elements.length+"\r\n";
        for (var i=0; i<theForm.elements.length; i++)
            txt += theForm.elements[i].name + " = " + theForm.elements[i].value + "\r\n";
        alert(txt);
    }
    function js_validate(theForm)
    {
        showform(theForm); // remove when you don't need it
        // Check for name
        var name = theForm.elements['firstname'].value;
        if (name == "")
        {
            alert ("You must enter your first name");
            theForm.elements['subject'].focus();
            return true;
        // Check if subject is not blank
        var sbj = theForm.elements['lastname'].value;
        if (sbj == "")
        {
            alert ("You must enter a Last Name");
            theForm.elements['subject'].focus();
            return true;
        }
        }
        // Check email address
        var email = theForm.elements['email'].value;
        if (echeck(email))
        {
            alert ("You must enter a valid email address");
            theForm.elements['email'].focus();
            return true;
        }
        // Check if subject is not blank
        var sbj = theForm.elements['product'].value;
        if (sbj == "")
        {
            alert ("You must enter a product");
            theForm.elements['subject'].focus();
            return true;
        }
        // Check if comment is not blank
        var comt = theForm.elements['comment'].value;
        if (comt == "")
        {
            alert ("You must enter a comment");
            theForm.elements['comment'].focus();
            return true;
        }
        theForm.submit();    // form is ok, we can submit it!
    }
    // === EMAIL VALIDATION =================================================================================================================================
    function echeck(str) 
    {
        var at="@"
        var dot="."
        var lat=str.indexOf(at)
        var lstr=str.length
        var ldot=str.indexOf(dot)
        if (str.indexOf(at)==-1)
           return(true);
        if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
            return(true);
        if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
            return(true);
        if (str.indexOf(at,(lat+1))!=-1)
            return(true);
        if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
            return(true);
        if (str.indexOf(dot,(lat+2))==-1)
            return(true);
        if (str.indexOf(" ")!=-1)
            return(true);
        return (false);                    
    }
    </script>
    </head>
    <body>
    <?php
        
    if (isset($_POST['submitted']))
        {
            echo (
    treat_submission());
            unset (
    $_POST['submitted']);
        }
        else
            echo (
    show_form());
    ?>
    It works great however it pops up the annoying javascript alert box. When I try to remove the lines with the alert the code breaks. Anyone have any ideas?
  2. #2
  3. Why so angry?
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jan 2004
    Posts
    1,945
    Rep Power
    898
    Code:
        showform(theForm); // remove when you don't need it
    Verify and sanitize ALL USER DATA.

    And, to steal a quote from jeremy, "Explain your problem instead of asking how to do what you decided was the solution." Chances are someone on the forums will know a better or more efficient way to do what you're trying to accomplish.

    Avatar: Stolen by me, shown to me by patrick.

  4. #3
  5. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,911
    Rep Power
    1045
    Hi,

    to be honest, I wouldn't use that code. It's rather second rate and doesn't offer anything but the most basic functionalities.

    Unless you urgently need a working script and have no interest at all in PHP, I'd write it myself. Scripts on the internet are rarely good and sometimes even dangerous (because many people have no idea about security).

IMN logo majestic logo threadwatch logo seochat tools logo