0

I am first time working on web development using just html. I have a webiste and i want to have an information submit form which must send an email without opening outlook. I try to do so uing a http POST request and handling it using PHP.

The problem is it do not send the email. I don't know wy.

My SendEmail.php file is:

<?php
 if(isset($_POST['submit']))
{  
    $to = "shekhar.paris@gmail.com"; // this is your Email address
    $from = $_POST['email']; // this is the sender's Email address
    $first_name = $_POST['name'];
    $subject = "Form submission";
    $message = $first_name . " " . " wrote the following:" . "\n\n" . $_POST['message'];

    $headers = "From:" . $from;
    mail($to,$subject,$message,$headers);
    echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.";
    // You can also use header('Location: thank_you.php'); to redirect to another page.
    }
?>

My html is :

  <div class="contact">
            <div class="container">
                <div class="row">
                    <div class="col-sm-12">
                        <h4>Please Contact With Us For Any Kind Of Information></h4>
                        <form id="contactform" action="SendEmail.php" method="post" class="validateform" name="send-contact">
                            <div class="row">
                                <div class="col-lg-4 field">
                                    <input type="text" name="name" placeholder="* Your Name" data-rule="maxlen:4" data-msg="Please enter at least 4 chars" />                                   
                                </div>
                                <div class="col-lg-4 field">
                                    <input type="text" name="email" placeholder="* Your Email" data-rule="email" data-msg="Please enter a valid email" />
                                </div>
                                <div class="col-lg-4 field">
                                    <input type="text" name="subject" placeholder="Subject" data-rule="maxlen:4" data-msg="Please enter at least 4 chars" />
                                </div>
                                <div class="col-lg-4 field">
                                    <input type="text" name="ContactNumber" placeholder="* Contact Number" data-rule="required" data-msg="Please enter your number" />
                                </div>
                                <div class="col-lg-12 margintop10 field">
                                    <textarea rows="12" name="message" class="input-block-level" placeholder="* Your message here..." data-rule="required" data-msg="Please write something"></textarea>
                                    <p>
                                        <button name="submit" value="submit" class="btn btn-theme margintop10 pull-left" type="submit">Submit message</button>
                                    </p>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>

Could some one please help me in sending the email ?

user3085082
  • 133
  • 4
  • 16
  • 2
    mail() function does not work on localhost if you are running it locally – Ropali Munshi Feb 12 '19 at 10:10
  • `C:/Users/sshekhawat/Desktop/Raj........` you need to use a file on the webserver or create an alias to that location but not try to execute files like that – Professor Abronsius Feb 12 '19 at 10:10
  • 2
    All `mail()` does is pass the email to a MailServer, it does not actually send the email. Do you have a mail server on this PC? – RiggsFolly Feb 12 '19 at 10:11
  • Are you actually running a Web Server? Like Apache? – RiggsFolly Feb 12 '19 at 10:13
  • @RopAliMunshi That is not actually true! It can work but only if you have installed a mail server and configured PHP to use it – RiggsFolly Feb 12 '19 at 10:14
  • @RiggsFolly Sorry i have no idea about how to run a webserver, i am able to open gmail and outlook both in PC. What do you suggest me next to do ? – user3085082 Feb 12 '19 at 10:16
  • @RopAliMunshi Yes i am trying to work locally, what should i do it make it work, i mean be able to send email. i have not hosted my htnl website yet, because its the last part to be able to send email. – user3085082 Feb 12 '19 at 10:20
  • Without a Web Server like Apache, PHP will not run. Look at WAMPServer its an out of the box Apache/MySQL/PHP instance – RiggsFolly Feb 12 '19 at 10:20
  • @user3085082 you can either use Gmail's SMTP server or you can go for a SMTP services provider SendGrid which is a good option. – Ropali Munshi Feb 12 '19 at 10:21
  • @RopAliMunshi >>> **NO WEB SERVER** <<< therefore no PHP – RiggsFolly Feb 12 '19 at 10:22

1 Answers1

0

In your HTML, change the form action to SendEmail.php instead of your absolute path.

If you run your Code on a webserver with php it should work now.

Michi Gruber
  • 254
  • 3
  • 18
  • Thanks, Already done this , please see the edit of code in my question. Still do not send email. – user3085082 Feb 12 '19 at 10:17
  • Alright then. But are you able to execute any PHP files? To test this, create a simple test.php file and echo "hello world". – Michi Gruber Feb 12 '19 at 10:21
  • Nothing to do with the actual problem, which is OP has no web server and therefore no way of running PHP – RiggsFolly Feb 12 '19 at 10:21
  • @NicolasHaemmerli No i am not able to execute php file. and print helloworld – user3085082 Feb 12 '19 at 10:22
  • >>> **NO WEB SERVER** <<< therefore no PHP – RiggsFolly Feb 12 '19 at 10:24
  • Then I would recommend to install XAMPP and start to use this for testing. Have a look at some tutorials. – Michi Gruber Feb 12 '19 at 10:25
  • @NicolasHaemmerli OK i can install XAMPP, But while hosting my website, what would i need to host along my html css php files to make the email sending work ? – user3085082 Feb 12 '19 at 10:26
  • Then you could look to use `phpMailer` a php based email helper tool which is very good – RiggsFolly Feb 12 '19 at 10:28
  • 1
    A Webserver. Almost every hosting provider has PHP included in their shared hosting options. – Michi Gruber Feb 12 '19 at 10:28
  • @NicolasHaemmerli You mean if i host my website (html,css and php and some jpg) under shared hosting option, then the mail() ishould work and should send the email with same code with nothing done extra, As the hoster has php included and it will just not work locally until i install a webserver. If yes, Please edit your answer so that i can accept your answer. – user3085082 Feb 12 '19 at 11:12
  • Exactly, that's what I've meant. I edited it. – Michi Gruber Feb 12 '19 at 11:55