skip to Main Content

I am trying to send an email from a webpage but when i click on the submit button php code is opening. I have installed apache server and its up and running. I am not quite sure what i am missing and how to send an email from webpage. do i need to install sendmail and php also to make it work in local using apache server. below is my code. please help

    <form action= "temp.php" method="post" name="sentMessage" id="contact" >
<div class="row">
<div class="col-md-6" style="margin-left:-5px;">
    <div class="input-field">
        <input type="text" name="name" class="form-control" id="name"
            required
            data-validation-required-message="Please enter your name" />
        <label for="name" class=""> Name </label>
        <p class="help-block"></p>

    </div>
    </div>
</div>
<!-- For success/fail messages -->
<button name="submit" type="submit"
    class="btn btn-primary waves-effect waves-dark pull-right">Send</button>
<br />
</form>

and php code is as follows.

<?php 
if(isset($_POST['submit'])){
    $to = "[email protected]"; // this is your Email address
    $from = "[email protected]"; // this is the sender's Email address
    $subject = "Form submission";
    $message = "form submission";
    $headers = "From:" . $from;
    $headers2 = "From:" . $to;
    mail($to,$subject,$message,$headers);
    echo "Mail Sent";
    }
?>

2

Answers


  1. Adding this answer here to list a few things that can be done. This is by no means a proper tutorial on how to set up an smtp server. It just skims through the topics to illustrate the basic requirements to send email with php.

    There are a few things you should do to make the setup work.

    1. You are seeing php code as the output. This can happen if you have no php installed (as mentioned by you) or if you have saved a php file with a different extension like .html or .txt. So you need to install php and also ensure all files with php codes have the extension .php.
    2. If it still does not work take a look at this checklist (as mentioned by El_vanja in the comments). It should help you do basic troubleshooting on what could be wrong.

    Once you have php working and you start seeing php output instead of codes you will need to setup or find an smtp server. Read more about smtp servers here

    You can setup an smtp server on your own or use and existing one (like gmail or yahoo). What steps to follow to install smtp server depends on your OS. For ubuntu you can check here and for windows server you can check here. This list is definitely not exhaustive. These are aimed at pointing you to the right direction.

    For Windows dev environment xampp allows you to use an external smtp server. So you can simply configure it with your existing gmail account and it would start sending emails. You can check the details in their windows faq here

    Once you have an smtp server to send the emails you can use sendmail in php to send emails.

    Also do note managing a public facing smtp server comes with its challenges and one should be careful when setting up a server without proper understanding.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search