0

I just tried to send email using html form and php, but not able to send email to the respective mail id provided in textbox as input.

I searched and got this example code, can anyone suggest me that where I made mistake.

html

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8" />
        <title>Contact Form</title>
        <script  src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js"></script>
        <link rel="stylesheet" type="text/css" href="./styles.css" />

        <script type="text/javascript">                                         
        /* <![CDATA[ */
            $(document).ready(function(){ // sends the data filled in the contact form to the php file and shows a message
                $("#contact-form").submit(function(){
                    var str = $(this).serialize();
                    $.ajax({
                       type: "POST",
                       url: "send.php",
                       data: str,
                       success: function(msg)
                       {
                            $("#formstatus").ajaxComplete(function(event, request, settings){
                                if(msg == 'OK'){ // Message Sent? Show the 'Thank You' message and hide the form
                                    result = '<div class="formstatusok">Your message has been sent. Thank you!</div>';
                                    $("#fields").hide();
                                }
                                else{
                                    result = msg;
                                }
                                $(this).html(result);
                            });
                        }

                     });
                    return false;
                });
            });
        /* ]]> */   
        </script>  
    </head>

    <body>
        <div id="wrap">
            <h1>Drop me a Message!</h1>
            <div id='form_wrap'>
                <form id="contact-form" action="javascript:alert('success!');">

            <p id="formstatus"></p>
                    <p>Hello Joe,</p>
                    <label for="email">Your Message : </label>
                    <textarea  name="message" value="Your Message" id="message" ></textarea>
                    <p>Best,</p>    
                    <label for="name">Name: </label>
                    <input type="text" name="name" value="" id="name" />
                    <label for="email">Email: </label>
                    <input type="text" name="email" value="" id="email" />
                    <input type="submit" name ="submit" value="OK I want to send it now, thanks!" />
                </form>
            </div>
        </div>
    </body>
    </html>

send.php

    <?php
        define("WEBMASTER_EMAIL", 'myid@company.com');

        error_reporting (E_ALL ^ E_NOTICE);

        function ValidateEmail($email)
        {
            $regex = '/([a-z0-9_.-]+)'. # name
            '@'. # at
            '([a-z0-9.-]+){2,255}'. # domain & possibly subdomains
            '.'. # period
            '([a-z]+){2,10}/i'; # domain extension 

            if($email == '') 
                return false;
            else
                $eregi = preg_replace($regex, '', $email);
            return empty($eregi) ? true : false;
        }

    //////////////////////////////////////////////////////

        $post = (!empty($_POST)) ? true : false;

        if($post)
        {
            $name    = stripslashes($_POST['name']);
            $email   = trim($_POST['email']);
            $subject = stripslashes($_POST['subject']);
            $message = stripslashes($_POST['message']);

            $error = '';

            // Check name
            if(!$name || $name == "Name*")
                $error .= 'Please enter your name.<br />';

            // Check email
            if(!$email || $email == "Email*")
                $error .= 'Please enter an e-mail address.<br />';

            if($email && !ValidateEmail($email))
                $error .= 'Please enter a valid e-mail address.<br />';

            // Check message
            if(!$message)
                $error .= "Please enter your message. <br />";

            if(!$error)
            {
                $mail = mail(WEBMASTER_EMAIL, $subject, $message,
                     "From: ".$name." <".$email.">\r\n"
                    ."Reply-To: ".$email."\r\n"
                    ."X-Mailer: PHP/" . phpversion());

                if($mail)
                    echo 'OK';
            }
            else
                echo '<div class="formstatuserror">'.$error.'</div>';
        }

    ?>

Any ideas ?

amdixon
  • 3,814
  • 8
  • 25
  • 34
DevGo
  • 1,045
  • 1
  • 16
  • 42
  • Did you tried changing define("WEBMASTER_EMAIL", 'your_email@mail.com'); with your email address where you want to receive emails? – happyhardik May 22 '15 at 09:08
  • 1
    What is the message that you get? or any error messages? – happyhardik May 22 '15 at 09:09
  • could you provide some more information? Most of all: have you ever managed to send an email from this webserver? If no, try that first. If yes: Are you getting any error messages? Have you tried anything to debug? – Burki May 22 '15 at 09:12
  • http://54.169.114.129:8181/web/index1.html this is my sample page... I am getting error. This is first time i am using php+html – DevGo May 22 '15 at 09:15
  • 1
    Your server is not running php, its just returning it back. Check your server config to configure php or contact your hosting company. – happyhardik May 22 '15 at 09:20
  • 1
    Here's how to check the config: http://stackoverflow.com/questions/5121495/php-code-is-not-being-executed-i-can-see-it-on-source-code-of-page – happyhardik May 22 '15 at 09:29

4 Answers4

0

try configuring this in your php.ini

SMTP = smtp.exmaple.com -> smtp host
smtp_port = 25 -> port usually 25
username = usename@user.com
password = password
sendmail_from = sourceofmail@example.com
0

Firstly, due to the problems caused by spam mails, many servers have limited the mails outgoing unless its authenticated by some authority like Gmail, Yahoo etc.

Secondly, You should check out your php version and see if you are using the right formats rather than copy pasting a sample code.

Finally, if you are doing this from you local machine, your ISP might have implemented some regulations for sending emails.

NikzJon
  • 912
  • 7
  • 25
0

If you are getting "OK" message from ajax and mail sent message is displayed and still not receiving the emails, then you need to check your php.ini's smtp server settings.

Open your php.ini file and look for the following settings:

SMTP = smtp.gmail.com 
smtp_port = 25
username = youremail@gmail.com
password = PASSWORD
sendmail_from = webmaster@yourdomain.com

You can use gmail's smtp server settings to send an email for testing, if you are not sure where to get those.
Check this out: https://www.digitalocean.com/community/tutorials/how-to-use-google-s-smtp-server

happyhardik
  • 24,987
  • 7
  • 47
  • 60
0

I tried your code on my server and the first thing I noticed: "ReferenceError: $ is not defined"

So the linked jquery doesnt work and wont execute the ajax request.

Change it to more recent version on googleapis (googlecode will be shut down January 25, 2016)

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

(Assuming your php.ini is configured to send mails: see other answers)

Naga
  • 118
  • 10
  • I changed it. pls test it now console is clear without errors – DevGo May 22 '15 at 09:24
  • It works on my Server with the new jQuery lib, thats why I posted it as an answer. You should test it on your side. If it wont work try to debug if the PHP Script reaches the part where it want to send the mail ( mail() ) - if it does and still wont send mail its probably one of the other answers here – Naga May 22 '15 at 09:26