Sample Php Code for Contact us page with SMTP Authentication.
mail.php
<?
require_once "mail.php";
$from = $_REQUEST['txtMail'];
$to = '[email protected]';
$headers = "[email protected]\r\n" .
$headers .= 'Reply-To: ' . $_REQUEST['txtMail'] . "\n";
$headers = "MIME-Version: 1.0 "."\n";
$headers .= "Content-type: text/html; charset=iso-8859-1 "."\n\n";
$host = "mail.yourdomain.com";
$port = "25";
$username = "[email protected]";
$password = "mailpassword";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,
'password' => $password));
$subject = "Contact Form";
$message .= '<HTML><Body><Table width="100%"><TR><TD colspan="3" style="background-color:#EFEEEE;"><center><b>Contact Us</b></center></td></tr>' .
'<Tr><td width=""30%"">Name</td><td width=""5%"">:</td><td>' . $_REQUEST['txtName'] . '</td></tr> ' .
'<Tr><td width=""30%"">Company Name</td><td width=""5%"">:</td><td>' . $_REQUEST['txtCompany'] . '</td></tr> ' .
'<Tr><td width=""30%"">Email Id</td><td width=""5%"">:</td><td>' . $_REQUEST['txtMail'] . '</td></tr> ' .
'<Tr><td width=""30%"">Telephone Number</td><td width=""5%"">:</td><td>' . $_REQUEST['txtPhone'] . '</td></tr> ' .
'<Tr><td width=""30%"" colspan="3">Message :</td></tr><tr><td colspan="3">' . $_REQUEST['txtMessage'] . '</td></tr> ' ;
$mail = $smtp->send($to, $headers, $message);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message successfully sent!</p>");
}
?>
Contactus.html
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Contact Us</title>
<link rel="stylesheet" href="CSS/General.css" type="text/css" />
<meta http-equiv='imagetoolbar' content='no' />
</head>
<body style="margin:0px; top:0px;">
<form id="frmContact" method="post" action="mail.php" onsubmit="return verify(this.form)">
<table id="tblContact" cellpadding="0" border="0" cellspacing="0" width="100%">
<tr><td colspan="2" class="ContentTitle" style="padding-top:10px;"> Quick Contact</br></td>
</tr>
<tr><td colspan="2" class="More" style="padding-top:7px;"> Give Your Valuable Feedback</td>
</tr>
<tr><td class="Content" style="padding-top:7px;">Name</td>
<td style="padding-top:7px; "><input name="txtName" id="txtName" type="text" style="width:200px; height:19px;" /></td>
</tr>
<tr><td class="Content" style="padding-top:2px;">Company Name</td>
<td style="padding-top:2px;"><input name="txtCompany" id="txtCompany" type="text" style="width:200px; height:19px;" /></td>
</tr>
<tr><td class="Content" style="padding-top:2px;">Email Id</td>
<td style="padding-top:2px;"><input name="txtMail" id="txtMail" type="text" style="width:200px; height:19px;" /></td>
</tr>
<tr><td class="Content" style="padding-top:2px;">Phone Number</td>
<td style="padding-top:2px;"><input name="txtPhone" id="txtPhone" type="text" style="width:200px; height:19px;" /></td>
</tr>
<tr><td class="Content" style="padding-top:2px;">Message</td>
<td style="padding-top:2px;"><textarea name="txtMessage" id="txtMessage" rows="3" style="width:200px;" style="font-family:Verdana; font-size:10px;"></textarea></td>
</tr>
<tr><td colspan="2" align="center" style="padding-bottom:7px; padding-top:2px;"">
<input id="butSend" value="Send" type="submit" />
<input id="butClear" value="Clear" type="reset" />
</td>
</tr>
</table>
</form>
<script language="javascript" type="text/javascript">
function verify(form)
{
if((document.getElementById("txtName").value==null)||(document.getElementById("txtName").value==""))
{
alert("Please enter your Name");
return(false);
}
else if((document.getElementById("txtCompany").value==null)||(document.getElementById("txtCompany").value==""))
{
alert("Please enter your Company Name");
return(false);
}
else if ((document.getElementById("txtMail").value==null)||(document.getElementById("txtMail").value==""))
{
alert("Please Enter your Email ID");
return(false);
}
else if (echeck(document.getElementById("txtMail").value)==false)
{
alert("Invalid Email ID");
return(false);
}
else if((document.getElementById("txtPhone").value==null)||(document.getElementById("txtPhone").value==""))
{
alert("Please enter your Telephone Number");
return(false);
}
else if((document.getElementById("txtMessage").value==null)||(document.getElementById("txtMessage").value==""))
{
alert("Please enter your Message");
return(false);
}
return(true);
}
</script>
</body>
</html>
PHP Contact us Page with SMTP Authentication
September 20, 2022Read time:less than a minute read