Send an email through HTML Form using PHPMailer in PHP

Fist of All we will create a HTML Form

<form role="form" action="sendmail.php" method="post">
<input name="name" required="required" type="text" placeholder="Enter Your Name" />
<input name="email" required="required" type="email" placeholder="Email ID"/>
<input name="mobile" required="required" type="text" placeholder="Enter Mobile No"/>
<textarea name="message" placeholder="Enter Any Message"></textarea>
<input name="submit" type="submit" value="Send a Mail" /></form>


Preview will be look like this
html form for send a mail

Now we will create a file sendmail.php for send a mail in PHP

<?php

	require ("class.phpmailer.php");

	if(isset($_POST['submit'])){
		$name=$_POST['name']; // Get Name value from HTML Form
		$mobile=$_POST['mobile'];  // Get Mobile No
		$email=$_POST['email'];  // Get Email Value
		$message=$_POST['message']; // Get Message Value
		
		
		$mail = new PHPMailer();
		
		$mail->IsSMTP();
		$mail->Host = "websapex.com"; // Your Domain Name
		
		$mail->SMTPAuth = true;
		$mail->Port = 587;
		$mail->Username = "info@websapex.com"; // Your Email ID
		$mail->Password = "DT~-RQyJlaFV"; // Password of your email id
		
		$mail->From = "info@websapex.com";
		$mail->FromName = "WebsApex Team";
		$mail->AddAddress ("info@websapex.com"); // On which email id you want to get the message
		$mail->AddCC ($email);
		
		$mail->IsHTML(true);
		
		$mail->Subject = "Enquiry from Website submitted by $name"; // This is your subject
		
		// HTML Message Starts here
		
		$mail->Body = "
		<html>
			<body>
				<table style='width:600px;'>
					<tbody>
						<tr>
							<td style='width:150px'><strong>Name: </strong></td>
							<td style='width:400px'>$name</td>
						</tr>
						<tr>
							<td style='width:150px'><strong>Email ID: </strong></td>
							<td style='width:400px'>$email</td>
						</tr>
						<tr>
							<td style='width:150px'><strong>Mobile No: </strong></td>
							<td style='width:400px'>$mobile</td>
						</tr>
						<tr>
							<td style='width:150px'><strong>Message: </strong></td>
							<td style='width:400px'>$message</td>
						</tr>
					</tbody>
				</table>
			</body>
		</html>
		";
		// HTML Message Ends here
		
			
		if(!$mail->Send()) {
			// Message if mail has been sent
			echo "<script>
				alert('Submission failed.');
			</script>";
		}
		else {
			// Message if mail has been not sent
			echo "<script>
				alert('Email has been sent successfully.');
			</script>";
		}

	}
?>



Now you are ready to send a mail through HTML form in PHP by using PHPMailer.


For Download the PHPMailer Function Click here and extract the files in root folder.

 

3791

You may also like

6 comments on “Send an email through HTML Form using PHPMailer in PHP

    admin Post author

    Hello Sandeep,

    This code is for live site. You will have to just check the port and SMTP enable with your hosting provider. And change the value on Line: 17 and 18.
    $mail->SMTPAuth = true;
    $mail->Port = 587;

    Please Enter your Username and Password on Line: 19 and 20
    $mail->Username = “info@websapex.com”; // Your Email ID
    $mail->Password = “DT~-RQyJlaFV”;

    admin Post author

    Hello Suman,

    Please check the port and SMTP enable with your hosting provider. And change the value on Line: 17 and 18.
    $mail->SMTPAuth = true;
    $mail->Port = 587;

qchecksupport

Works great but I need two additional functions:
1. How do you do a redirect to another web page in the same domain upon submit
2. How to upload data from the form in csv format to folder on the server

    admin Post author

    Hello QcheckSupport,

    1. Please check the Line No. 62 – if(!$mail->Send()) { Here You can write the code for redirecting either through JavaScript or PHP using header
    2. I will publish the tutorial For uploading the file through php form soon. Please Subscribe our YouTube Channel. So, when I will update you will get noified.

Leave a Reply

Your email address will not be published. Required fields are marked *