Announcement

Collapse
No announcement yet.

Html Contact Form And Captcha

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Html Contact Form And Captcha

    I have an contact from and one mail php script to send the mail. It is from one templatemonster.com site. But the form is not sending any mail to my mail box. Also I like to include one captcha in this form to avoid spamming. I include herewith the code so that it will be easy for you to look into. Here is the html form
    Code:
    							<form id="contact-form">
    								<div class="success">
    									Contact form submitted!<br>
    									<strong>We will be in touch soon.</strong>
    								</div>
    								<fieldset>
    									<label class="name">
    										<input type="text" value="Name:">
    										<span class="error">*This is not a valid name.</span> <span class="empty">*This field is required.</span> 
    									</label>
    									<label class="email">
    										<input type="text" value="Email:">
    										<span class="error">*This is not a valid email address.</span> <span class="empty">*This field is required.</span>
    									</label>
    									<label class="phone">
    										<input type="tel" value="Phone:">
    										<span class="error">*This is not a valid phone number.</span> <span class="empty">*This field is required.</span>
    									</label>
    									<label class="message">
    										<textarea>Message:</textarea>
    										<span class="error">*The message is too short.</span> <span class="empty">*This field is required.</span>
    									</label>
    									<div class="buttons-wrapper">
    										<a class="button" data-type="submit">Send Message</a>
    									</div>
    								</fieldset>
    							</form>
    And here is the php code
    Code:
    <?php
    	$owner_email = $_POST[" My email address "];
    	$headers = 'From:' . $_POST["email"];
    	$subject = 'A message from your site visitor ' . $_POST["name"];
    	$messageBody = "";
    	
    	if($_POST['name']!='nope'){
    		$messageBody .= '<p>Visitor: ' . $_POST["name"] . '</p>' . "\n";
    		$messageBody .= '<br>' . "\n";
    	}
    	if($_POST['email']!='nope'){
    		$messageBody .= '<p>Email Address: ' . $_POST['email'] . '</p>' . "\n";
    		$messageBody .= '<br>' . "\n";
    	}else{
    		$headers = '';
    	}
    	if($_POST['state']!='nope'){		
    		$messageBody .= '<p>State: ' . $_POST['state'] . '</p>' . "\n";
    		$messageBody .= '<br>' . "\n";
    	}
    	if($_POST['phone']!='nope'){		
    		$messageBody .= '<p>Phone Number: ' . $_POST['phone'] . '</p>' . "\n";
    		$messageBody .= '<br>' . "\n";
    	}	
    	if($_POST['fax']!='nope'){		
    		$messageBody .= '<p>Fax Number: ' . $_POST['fax'] . '</p>' . "\n";
    		$messageBody .= '<br>' . "\n";
    	}
    	if($_POST['message']!='nope'){
    		$messageBody .= '<p>Message: ' . $_POST['message'] . '</p>' . "\n";
    	}
    	
    	if($_POST["stripHTML"] == 'true'){
    		$messageBody = strip_tags($messageBody);
    	}
    	
    	try{
    		if(!mail($owner_email, $subject, $messageBody, $headers)){
    			throw new Exception('mail failed');
    		}else{
    			echo 'mail sent';
    		}
    	}catch(Exception $e){
    		echo $e->getMessage() ."\n";
    	}
    ?>
    Kindly if you some time , please look into it why the code is not working and request to instruct how to add captcha in this form.

    Sincerely

    trnghosh
    Last edited by trnghosh; 05-13-2016, 02:22 PM.

  • #2
    For a start it isn't formatted correctly. Secondly if you are using windows you need to be running an smtp server. Thirdly if you are hosted by a third party ensure they allow mail to be sent.

    Comment


    • #3
      Originally posted by fiveworlds View Post
      For a start it isn't formatted correctly. Secondly if you are using windows you need to be running an smtp server. Thirdly if you are hosted by a third party ensure they allow mail to be sent.
      I uploaded this one to my server and so far my knowledge the allow mail to be sent. Regarding formatting can u please tell me where is wrong with this one. Little knowledge in conact form, captcha and php so need your assistance.

      Thanks for responding.

      trnghosh

      Comment


      • #4
        <?php
        if($_POST && isset($_FILES['my_file']))
        {
        //where your server is sending from mandatory as of 2014, can also be set in php.ini
        date_default_timezone_set( 'Europe/Dublin' );

        //adding a subject to an email is not mandatory
        if(isset($_POST['subject']))
        {
        $subject = $_POST['subject']; //subject of email
        }
        else
        {
        $subject="";

        }
        //sender email should be @yourdomain if the domain doesn't exist it may fail
        $user_email = filter_var($_POST["email"], FILTER_SANITIZE_EMAIL);
        $yourdomainname = 'example.com';

        if (strpos($user_email, $yourdomainname) !== false) {

        if(isset($_FILES['my_file']))
        {

        //get file details we need
        $file_tmp_name = $_FILES['my_file']['tmp_name'];
        $file_name = $_FILES['my_file']['name'];
        $file_size = $_FILES['my_file']['size'];
        $file_type = $_FILES['my_file']['type'];
        $file_error = $_FILES['my_file']['error'];

        //fail if the attachment upload failure
        if($file_error>0)
        {

        die('upload error');
        }

        //read from the uploaded file & base64_encode content for the mail
        $handle = fopen($file_tmp_name, "r");
        $content = fread($handle, $file_size);
        fclose($handle);
        $encoded_content = chunk_split(base64_encode($content));
        }


        $boundary = md5("sanwebe");

        //headers
        $headers = "MIME-Version: 1.0\r\n";
        $headers .= "From:".$$user_email."\r\n";
        $headers .= "Reply-To: ".$$user_email."" . "\r\n";
        $headers .= "Content-Type: multipart/mixed; boundary = \"{$boundary}\"\r\n\r\n";


        //sending emails in an array
        $recipient_emails = array("frank@example.com","frank livingstone");

        for ($x = 0; $x <= count($recipient_emails)-2; $x=$x+2) {

        $y=$x+1;
        $message = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
        <html xmlns=\"http://www.w3.org/1999/xhtml\">
        <head>
        <meta http-equiv=\"Content-Type\" content=\"text/html; charset=Unicode\" />
        <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">
        <meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge,chrome=1\"\>
        <meta name=\"format-detection\" content=\"telephone=no\" />
        <title>Curriculum Vitae</title>
        <style type=\"text/css\">

        </style>
        </head>
        <body leftmargin=\"0\" marginwidth=\"0\" topmargin=\"0\" marginheight=\"0\" offset=\"0\">
        Dear {$recipient_emails[$y]},
        <p></p>
        <p>This is a test email</p>
        <p></p>
        <p>I am very interested in making sure my email works!</p>
        <p></p>
        <p>Kind Regards,</p>
        <p>{$_POST['name']}</p>
        <p></p>
        <p></p>
        </body>
        </html>
        ";

        //html email
        $body = "--{$boundary}\r\n";
        $body .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
        $body .= "Content-Transfer-Encoding: base64\r\n\r\n";
        $body .= chunk_split(base64_encode($message));

        if(isset($_FILES['my_file']))
        {

        //attachment headers these are required
        $body .= "--{$boundary}\r\n";
        $body .="Content-Type: {$file_type}; name=\"{$file_name}\"\r\n";
        $body .="Content-Disposition: attachment; filename=\"{$file_name}\"\r\n";
        $body .="Content-Transfer-Encoding: base64\r\n";
        $body .="X-Attachment-Id: ".rand(1000,99999)."\r\n\r\n";
        $body .= $encoded_content;
        }

        $sentMail = @mail($recipient_emails[$x], $subject, $body, $headers);
        }
        }


        }

        ?>

        <form name="form1" enctype="multipart/form-data" method="post" action="">

        <input type="hidden" name="MAX_FILE_SIZE" value="300000" />
        <label>Your Name
        <input type="text" name="name" />
        </label>
        <label>Your Email
        <input type="email" name="email" />
        </label>
        <label>Your subject
        <input type="text" name="subject" />
        </label>

        <label>Attachment
        <input type="file" name="my_file" />
        </label>
        <label>
        <input type="submit" name="button" value="Submit" />
        </label>
        </form>
        An imperfect example. Also note that while it is not an absolute requirement some email service providers may require ssl.
        Last edited by fiveworlds; 05-14-2016, 08:53 AM.

        Comment


        • #5
          Originally posted by fiveworlds View Post
          An imperfect example. Also note that while it is not an absolute requirement some email service providers may require ssl.
          Is that works for me sir. I included my email address and tested but my unfortune that I did not get any mail. However thanks for so much help extended so far.

          Thanks.

          trnghosh

          Comment


          • #6
            I may have made a small mistake in the php when I changed my file for you. Here is the original which works in hotmail and gmail. Also if you send to hotmail/gmail with a hotmail/gmail email address it is automatically discarded.

            <?php
            if($_POST && isset($_FILES['my_file']))
            {
            date_default_timezone_set( 'Europe/Dublin' ); //eircom
            $from_email = 'matherdavid2@gmail.com'; //sender email
            $recipient_email = 'matherdavid2@gmail.com'; //recipient email
            $subject = 'Job Application'; //subject of email
            $message = file_get_contents("cv3.html"); //message body

            //get file details we need
            $file_tmp_name = $_FILES['my_file']['tmp_name'];
            $file_name = $_FILES['my_file']['name'];
            $file_size = $_FILES['my_file']['size'];
            $file_type = $_FILES['my_file']['type'];
            $file_error = $_FILES['my_file']['error'];

            $user_email = filter_var($_POST["email"], FILTER_SANITIZE_EMAIL);

            if($file_error>0)
            {
            die('upload error');
            }

            //read from the uploaded file & base64_encode content for the mail
            $handle = fopen($file_tmp_name, "r");
            $content = fread($handle, $file_size);
            fclose($handle);
            $encoded_content = chunk_split(base64_encode($content));


            $boundary = md5("sanwebe");

            //header
            $headers = "MIME-Version: 1.0\r\n";
            $headers .= "From: David <".$from_email.">\r\n";
            $headers .= "Reply-To: ".$user_email."" . "\r\n";
            $headers .= "Content-Type: multipart/mixed; boundary = \"{$boundary}\"\r\n\r\n";



            $recipient_emails = array(

            "davidmather@live.ie","testing"
            );
            for ($x = 0; $x <= count($recipient_emails)-2; $x=$x+2) {
            $y=$x+1;
            echo $recipient_emails[$x].",".$recipient_emails[$y]."</br>";
            $message = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
            <html xmlns=\"http://www.w3.org/1999/xhtml\">
            <head>
            <meta http-equiv=\"Content-Type\" content=\"text/html; charset=Unicode\" />
            <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">
            <meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge,chrome=1\"\>
            <meta name=\"format-detection\" content=\"telephone=no\" />
            <title>Curriculum Vitae</title>
            <style type=\"text/css\">

            </style>

            </head>
            <body leftmargin=\"0\" marginwidth=\"0\" topmargin=\"0\" marginheight=\"0\" offset=\"0\">
            To whom it concerns,
            <p></p>
            <p>I am applying for the role of {$recipient_emails[$y]} advertised on fas.ie.</p>
            <p></p>
            <p>I am very interested in this role and feel I have the required experience and qualifications. I look forward to hearing from you.</p>
            <p></p>
            <p>Kind Regards,</p>
            <p>David Mather</p>
            <p></p>
            <p></p>
            </body>
            </html>";

            //plain text
            $body = "--{$boundary}\r\n";
            $body .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
            $body .= "Content-Transfer-Encoding: base64\r\n\r\n";
            $body .= chunk_split(base64_encode($message));

            //attachment
            $body .= "--{$boundary}\r\n";
            $body .="Content-Type: {$file_type}; name=\"{$file_name}\"\r\n";
            $body .="Content-Disposition: attachment; filename=\"{$file_name}\"\r\n";
            $body .="Content-Transfer-Encoding: base64\r\n";
            $body .="X-Attachment-Id: ".rand(1000,99999)."\r\n\r\n";
            $body .= $encoded_content;

            $sentMail = @mail($recipient_emails[$x], $recipient_emails[$y], $body, $headers);

            }

            }

            ?>

            <form name="form1" enctype="multipart/form-data" method="post" action="">
            <label>Your Name
            <input type="text" name="name" />
            </label>
            <label>Your Email
            <input type="email" name="email" />
            </label>
            <label>Attachment
            <input type="file" name="my_file" />
            </label>
            <label>
            <input type="submit" name="button" value="Submit" />
            </label>
            </form>
            The main problem I saw was the lack of a boundary in your original message. All emails must comply with http://www.faqs.org/rfcs/rfc2822.html . The php manual states this.
            Last edited by fiveworlds; 05-14-2016, 11:24 AM.

            Comment


            • #7
              Originally posted by fiveworlds View Post
              I may have made a small mistake in the php when I changed my file for you. Here is the original which works in hotmail and gmail. Also if you send to hotmail/gmail with a hotmail/gmail email address it is automatically discarded.



              The main problem I saw was the lack of a boundary in your original message. All emails must comply with http://www.faqs.org/rfcs/rfc2822.html . The php manual states this.
              Actually I do not have any knowledge in php just trying to understand how it works and make my form to work. For that reasons my questions sometimes seems meaningless. However will try to implement this one and will inform you about the result. Once again many thanks for your time.

              trnghosh

              Comment


              • #8
                I never use a boundary and mine work. So I don't think it "has" to have it, but it is suggested you should have one. It will work without one. Also, there is a lot of that php code which is not needed for your situation trnghosh. You don't need the attachment part. Plus it doesn't do anything with captcha. You can signup for google's re-captcha, it's free.
                Last edited by scoutt; 05-23-2016, 09:05 AM.
                Have a Script or Snippet you want to share?

                WWW Standards: HTML 4.01,
                HTML 5, CSS2.1, CSS3, XHTML 1.0
                PHP Standards: PHP Standards

                Comment

                Working...
                X