How to Send Mail Using Php Mailer.

In this post we will see that how we can send mail using Phpmailer. Before Start Tutorial we should know about what is Phpmailer , benefits of Phpmailer than we learn how to implement code for Php mailer.

what is Phpmailer

PHP Mailer is a code library to send emails safely and easily via PHP code from a web server.
phpmailer is a collection of classes which allows user to send mail via smtp seetings.
A user can easily configure php mailer according to his/her needs.

Benefits of Phpmailer over mail() function

php provides us mail() function allows us to send mails to user but it has certain limitations, mail() function send mail but it has very slow response ,mail() function does not assure the sending of mail.


On the other hand phpmailer assures the mail will be send to the user.
In this tutorial we will make 2 pages. first page is contact-us.php. in contact-us.php page we make a contact us form, where users query will be send via mail to admin.
Contact-us.php

Also Read :
PHP Login Script With Remember me.
Change password using javascript, php and mysqli.
Password and Confirm Password Validation Using JavaScript
Check Email is Already Registered in Database using Ajax and JavaScript.
How to hide extension of html and php file.?

<?php
//index.php
$error = '';
$name = '';
$email = '';
$subject = '';
$message = '';
function clean_text($string)
{
	$string = trim($string);
	$string = stripslashes($string);
	$string = htmlspecialchars($string);
	return $string;
}
if(isset($_POST["submit"]))
{
	if(empty($_POST["name"]))
	{
		$error .= '<p><label>Please Enter your Name</label></p>';
	}
	else
	{
		$name = clean_text($_POST["name"]);
		if(!preg_match("/^[a-zA-Z ]*$/",$name))
		{
			$error .= '<p><label>Only letters and white space allowed</label></p>';
		}
	}
	if(empty($_POST["email"]))
	{
		$error .= '<p><label>Please Enter your Email</label></p>';
	}
	else
	{
		$email = clean_text($_POST["email"]);
		if(!filter_var($email, FILTER_VALIDATE_EMAIL))
		{
			$error .= '<p><label>Invalid email format</label></p>';
		}
	}
	if(empty($_POST["subject"]))
	{
		$error .= '<p><label>Subject is required</label></p>';
	}
	else
	{
		$subject = clean_text($_POST["subject"]);
	}
	if(empty($_POST["message"]))
	{
		$error .= '<p><label>Message is required</label></p>';
	}
	else
	{
		$message = clean_text($_POST["message"]);
	}
	if($error == '')
	{
		require 'class/class.phpmailer.php';
		$mail = new PHPMailer;
		$mail->IsSMTP();								//Sets Mailer to send message using SMTP
		$mail->Host = 'localhost';		//Sets the SMTP hosts of your Email hosting, this for Godaddy
		$mail->Port = '25';								//Sets the default SMTP server port
		$mail->SMTPAuth = true;							//Sets SMTP authentication. Utilizes the Username and Password variables
		$mail->Username = 'your-email here';					//Sets SMTP username
		$mail->Password = 'password';					//Sets SMTP password
		$mail->SMTPSecure = '';							//Sets connection prefix. Options are "", "ssl" or "tls"
		$mail->From = $_POST["email"];					//Sets the From email address for the message
		$mail->FromName = $_POST["name"];				//Sets the From name of the message
		$mail->AddAddress('your email', 'Mainvps');		//Adds a "To" address
		//$mail->AddCC($_POST["email"], $_POST["name"]);	//Adds a "Cc" address
		$mail->WordWrap = 50;							//Sets word wrapping on the body of the message to a given number of characters
		$mail->IsHTML(true);							//Sets message type to HTML				
		$mail->Subject = $_POST["subject"];				//Sets the Subject of the message
		$mail->Body = $_POST["message"];				//An HTML or plain text message body
		if($mail->Send())								//Send an Email. Return true on success or false on error
		{
			$error = '<label>Thank you for contacting us</label>';
		}
		else
		{
			$error = '<label>There is an Error</label>';
		}
		$name = '';
		$email = '';
		$subject = '';
		$message = '';
	}
}
?>
<!DOCTYPE html>
<html>
	<head>
		<title>Send an Email on Form Submission using PHP with PHPMailer</title>
		<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
		<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
		<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
	</head>
	<body>
		<br />
		<div>
			<div>
				<div style="margin:0 auto; float:none;">
					<h3 align="center">Send an Email on Form Submission using PHP with PHPMailer</h3>
					<br />
					<?php echo $error; ?>
					<form method="post">
						<div>
							<label>Enter Name</label>
							<input type="text" name="name" placeholder="Enter Name" value="<?php echo $name; ?>" />
						</div>
						<div>
							<label>Enter Email</label>
							<input type="text" name="email" placeholder="Enter Email" value="<?php echo $email; ?>" />
						</div>
						<div>
							<label>Enter Subject</label>
							<input type="text" name="subject" placeholder="Enter Subject" value="<?php echo $subject; ?>" />
						</div>
						<div>
							<label>Enter Message</label>
							<textarea name="message" placeholder="Enter Message"><?php echo $message; ?></textarea>
						</div>
						<div align="center">
							<input type="submit" name="submit" value="Submit" />
						</div>
					</form>
				</div>
			</div>
		</div>
	</body>
</html>