0

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 = "xyz@gmail.com"; // this is your Email address
    $from = "abc@gmail.com"; // 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";
    }
?>
kavya
  • 157
  • 2
  • 10
  • 1
    If your PHP code isn't being executed, libraires won't help you at the moment. You need to determined why it wasn't executed. Go through [this checklist](https://stackoverflow.com/questions/5121495/php-code-is-not-being-executed-code-shows-in-browser) and see if anything helps. – El_Vanja May 24 '21 at 13:48
  • 1
    Yes, you need to install php for php scripts to be executed. Mostly sendmail is installed along with php. If you have php installed then ensure the file with php codes has the extenstion `.php`. The checklist linked by EL_Vanja is a good place to start troubleshooting. If you still cant make it run tell us the OS and the setup. – endeavour May 24 '21 at 14:02
  • Thank you @endeavour. i did install php and configured it(i didnt install sendmail), but when i am trying to run through apache server, after clicking on submit its giving me a msg ' mail sent' but i didnt receive any mail even after providing correct mail id's. can you please help me understand the issue here – kavya May 24 '21 at 14:41
  • And also help me how can i make sure i mail is actually been sent. – kavya May 24 '21 at 14:42
  • Is this a local server? Do you have smtp installed? What is the OS? – endeavour May 24 '21 at 15:04
  • I am running through Apache server in my local. I am using Window 10 and didnt install smtp explicitly – kavya May 24 '21 at 15:28
  • 1
    You need to install a mail server (smtp) for php to send the email. Is apache on IIS or xampp or wsl. Also if you want to notify a commenter about your response you need to mention their name like this `@kavya` – endeavour May 24 '21 at 16:57
  • Hi @endeavour, thank you, i have installed xampp and able to send the mail successfully. – kavya May 24 '21 at 18:42
  • That sounds good. Just keep in mind xampp is a solution for development purpose. Never use it for production – endeavour May 24 '21 at 19:26
  • Added a short explanation of the setup to help you understand the different systems required to send emails. – endeavour May 24 '21 at 20:02
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/232844/discussion-between-kavya-and-endeavour). – kavya May 25 '21 at 07:38

2 Answers2

0

Try PHP_Mailer library. Please refer this http://www.codesanitize.com/2021/04/send-html-email-with-attachment-using.html

Tony Stark
  • 434
  • 4
  • 13
  • 1) The question clearly indicates that PHP isn't even being executed; 2) Link-only answers have very little value in the long run. Links can expire and then your answer will lose a lot of context; 3) The matter of sending an e-mail using PHP has been well covered both on and off the site. – El_Vanja May 24 '21 at 13:51
0

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.

endeavour
  • 576
  • 4
  • 15