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
Try PHP_Mailer library.
Please refer this http://www.codesanitize.com/2021/04/send-html-email-with-attachment-using.html
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.
.html
or.txt
. So you need to install php and also ensure all files with php codes have the extension.php
.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 hereOnce 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.