PHP – Send Email From Localhost XAMPP

php send email using xampp localhost

Hello friends, today i am going to share with you an amazing tutorial that people always in search for. Yes, Today you will learn how to send email from localhost xampp using PHP. PHP is most famous server side scripting language. It has its own PHP mailer class, but here we use my favorite swift mailer library for sending emails using PHP. Swift mailer is feature rich powerful component that you don’t need to configure more. It has its own libraries and class that will help you to send html or text mail using php from localhost xampp. So perform this tutorial please follow the below steps. Let’s start…

First download the swift mailer package from here Swift mailer package. Extract the package, rename the package folder as swift-mailer and place it in your project’s root directory.

Now create a html form for write email address and message. See below html form code which i used for my html-form.

index.php

<!DOCTYPE html>
<html>
<head>
	<title>Send Email Using PHP</title>
<link rel="stylesheet" type="text/css" href="style.css">
<script type="text/javascript" src="jquery-1.11.1.js"></script>
</head>
<body>

<?php 
session_start();
	
$error = [
"email_error" => '',
"message_error" => ''
];

$form_data = [
"email" => '',
"message" => '',
];

if(!empty($_SESSION['error']))
{
	$error = $_SESSION['error'];
}

if(!empty($_SESSION['form_data']))
{
	$form_data = $_SESSION['form_data'];
}

?>

	<h1><center>Send Email Using PHP Swiftmailer library</center></h1>
	<form action="send-mail.php" method="post" onsubmit="return validate();" id="form_submission_ajax">
		<table class="form-table">
			
			<tr>
				<td><label>Email Address:</label></td>
				<td><input type="email" name="email" id="email" value="<?php echo $form_data['email']; ?>"></td>
			</tr>
			<tr>
				<td></td>
				<td id="email_error" class="error"><?php echo $error['email_error']; ?></td>
			</tr>

			<tr>
				<td><label>Message:</label></td>
				<td><textarea name="message" id="message"><?php echo $form_data['message']; ?></textarea></td>
			</tr>
			<tr>
				<td></td>
				<td id="message_error" class="error"><?php echo $error['message_error']; ?></td>
			</tr>

			<tr>
				<td></td>
				<td>
					<input type="submit" name="submit" value="Send">
				</td>
			</tr>
		</table>
	</form>
</body>

<script type="text/javascript">
function validate()
{
	var valid = true;
	var email = $('#email').val();
	var message = $('#message').val();

	if(email=='' || email==null)
	{
		valid=false;
		$('#email_error').html("* This field is required.");
	}
	else
	{
		$('#email_error').html("");	
	}

	if(message=='' || message==null)
	{
		valid=false;
		$('#message_error').html("* This field is required.");
	}
	else
	{
		$('#message_error').html("");
	}

	if(valid==true)
	{
		return true;
	}
	else
	{
		return false;
	}
}
</script>

</html>	

<?php 
$_SESSION['error'] = "";
$_SESSION['form_data'] = "";
?>

Above html form is secure with both ways server side and client side. Check it. If you fill up the sender’s email, message fields and click on the send button message will automatically send to that email address. It’s pretty awesome. You can send your gmail message from here. Let’s see example using gmail account.

Related:  Install Laravel 5.8 On Windows XAMPP Using Composer

See the below file which is accessed while user click on the send button on the index.php file.

send-mail.php

<?php 
session_start();
$valid = true;
$error = [];
$form_data = [];

if(!empty($_POST['email']))
{
	$email = $_POST['email'];
	$email_data = array("email" => $email);
	$form_data = array_merge($form_data, $email_data);
	$email_error = array("email_error" => "");
	$error = array_merge($error, $email_error);
}
else
{
	$valid = false;
	$email = "";
	$email_data = array("email" => $email);
	$form_data = array_merge($form_data, $email_data);
	$email_error = array("email_error" => "* Email is required.");
	$error = array_merge($error, $email_error);
}

if(!empty($_POST['message']))
{
	$message = $_POST['message'];
	$message_data = array("message" => $message);
	$form_data = array_merge($form_data, $message_data);
	$message_error = array("message_error" => "");
	$error = array_merge($error, $message_error);
}
else
{
	$valid = false;
	$message = "";
	$message_data = array("message" => $message);
	$form_data = array_merge($form_data, $message_data);
	$message_error = array("message_error" => "* Message is required.");
	$error = array_merge($error, $message_error);
}

if($valid==true)
{
	require_once 'swiftmailer/lib/swift_required.php';

	// Create the Transport the call setUsername() and setPassword()
	$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'ssl')
  	->setUsername('Your email address')
  	->setPassword('Your Password')
  	;

  	$mailer = Swift_Mailer::newInstance($transport);
	// Create the message
	$message = Swift_Message::newInstance()
	// Give the message a subject
	->setSubject('Your subject')
	// Set the From address with an associative array
	->setFrom(array('Your email address' => 'Your Name'))
	// Set the To addresses with an associative array
	->setTo(array($email))  //Receiver's email address
	// Give it a body
	->setBody('Body Message')
	// And optionally an alternative body
	->addPart($message, 'text/html');
	// Optionally add any attachments
	$result = $mailer->send($message);

	if(!$result)
	{
		echo "Mail successfully not sent.";
	}
	else
	{
		echo "Mail sent successfully.";
	}

}
else
{
	$_SESSION['error'] = $error;
	$_SESSION['form_data'] = $form_data;
	header('Location:index.php');
}

You can customize subject field also. Its is very simple. Try it yourself, if you are facing any problem then let me know i will help you.

Remember that, please remove “your email address” and “your password” with your email and password. This is a email and password of your gmail account by which you will send the emails.

I hope that you may like this send email using php in localhost xampp tutorial. Please share it with your friends. If you have problem while performing this tutorial, feel free to ask me in the comment box. I will definitely give you answer. Thank you very much. 🙂

Related:  How To Run PHP File From Windows Command Line

About Chintan Panchal

I am web developer having 6+ year of experience of web development. Currently working on laravel and have worked on codeigniter, cakephp, symfony. Thank you :)

View all posts by Chintan Panchal →

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.