PHP tutorial- Send E-mail from local host


Using PHPMailer to send e-mail from local host in PHP

In PHP, it is not hard to send e-mail message from a server machine that has mail server installed. You can use the mail() function to accomplish the task. However, it is a problem as you send an e-mail message from your local machine that does not have mail server to a recipient.

I found many people on the web asked for a solution to this problem. After reading this tutorial, you can write PHP code to send your e-mail message from your Google account to any recipient. The main library used to send e-mail from PHP code in this tutorial is PHPMailer. You can download PHPMailer zip file from here. After download the file, extract it to a folder. In my machine, it is extracted to mailsender folder.

phpmailer

Below is the content of mymail.php file. This file is stored in the mailsender folder. To send an e-mail message by using the PHPMailer library, you need to provide SMTP mail configuration information such as SMPT server name, port, communication security (tls or ssl) , user name, and password.

<!DOCTYPE HTML>
<html>
<head><title> Send email from localhost</title>
</head>
<body>
<?php
$status="";
if(isset($_REQUEST['btsubmit'])){

require("class.phpmailer.php");
$mail=new PHPMailer(); //creat PHPMailer object
$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPAuth = true; //needs login information
$mail->SMTPSecure = "tls"; //specifies tls security
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 587; //gmail smtp port
$mail->Username = $_REQUEST['txtfrom']; // SMTP account username
$mail->Password = "yourPassword"; // SMTP account password
$mail->From = $_REQUEST['txtfrom']; //sender's e-mail address
$mail->AddAddress($_REQUEST['txtto']);//receiver's e-mail address
$mail->Subject = $_REQUEST['txtsubject'];//e-maul subject
$mail->Body =$_REQUEST['txtmessage'];//e-mail message
//add file attachment
if (isset($_FILES['file']) && $_FILES['file']['error'] == UPLOAD_ERR_OK) {
$mail->AddAttachment($_FILES['file']['tmp_name'],$_FILES['file']['name']);
}
$mail->WordWrap = 50;
if(!$mail->Send()) {
$status='Message was not sent.';

} else {
$status='Message has been sent.';

}

}
?>
<center>
<form action="mymail.php" method="post" enctype="multipart/form-data" >
<table width="433" style="background:#f8f8f8">
<tr>
<th style="background: #CCC" colspan="2">Send E-mail</th>
</tr>
<tr>
<tr>
<td>
<strong>From: </strong></td>
<td>
<input name="txtfrom" id="txtfrom" type="text" style="width: 200px;height:40px; border:none"/>
</td>
</tr>
<tr>
<td>
<strong>To: </strong></td>
<td>
<input name="txtto" id="txtto" type="text" style="width: 200px;height:40px; border:none"/>
</td>
</tr>
<tr>
<td>
<strong>Subject: </strong></td>
<td>
<input name="txtsubject" id="txtsubject" type="text" style="width: 300px;height:40px; border:none"/>
</td>
</tr>
<tr>
<td colspan="2">
<strong>Message: </strong><br />
<textarea name="txtmessage" cols="50" rows="5" style="border:none"></textarea>
</td>
</tr>
<tr>
<td>
<strong>Attachment:</strong></td>
<td>
<input name="file" id="file" type="file" />
</td>
</tr>
<tr>
<td style="text-align:right" colspan="2">
<input type="submit" name="btsubmit" id="btsubmit" value="Send" />
</td>
</tr>
<tr>
<td colspan="2">
<?php echo $status; ?>
</td>
</tr>
</table>
</form>
</center>

</body>
</html>

This is the output from mymail.php file above.

send e-mail from local host

 




Comments





This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.