Send Email From Localhost PHP

send mail from your localhost php : Almost every web project contains email functionality and PHP mailer class provide a functionality of sending emails through local machine. Emails must be tested properly before the project go live. you can send text emails and html emails through this class. click here to download 

After downloading just perform following steps:

  • Place ‘PHPMailer’ in your project.
  • Include(‘PHPMailer/class.phpmailer.php’) in your file where you want to send email
  • I create a sendemail() function here is the code

function sendmail($sTo,$sFrom,$sSubject,$sMsg)
$logo = “”; //logo path
$html_msg= <<<END
<table width=’640′ cellpadding=’3′ cellspacing=’0′ align=”center” >
<td><img src=’$logo’ width=’150′></td>
<td style=’padding:2px;padding-top:5px’>

$mail = new PHPMailer; //instance created
$mail->Host = ‘’; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = ‘*******’; // SMTP username
$mail->Password = ‘*******’; // SMTP password
$mail->SMTPSecure = ‘tls’; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;

$mail->setFrom($sFrom, “The Technologies Axis”);

$mail->addAddress($sTo); // Add a recipient

$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $sSubject;
$mail->Body = $html_msg;

if(!$mail->send()) {
echo ‘Message could not be sent.’;
echo ‘Mailer Error: ‘ . $mail->ErrorInfo;exit;
} else {
echo ‘Message has been sent’;exit;


you just need to call function wherever you want like this :
$to = “”;
$from = “”;
$subject = “Technologies Axis Email Testing”;
$msg = “this is message”;

So here we are sending 4 parameters to function $to,$from,$subject,$msg . I guess should be clear what these parameters are all about. If you face any difficulty in understanding it you can comment on this post

On the above function i am using Gmail SMTP. So you have  change account access for less secure apps. You need to follow the following steps as well :

This is enough now you can send emails through local machine

Sending Attachment in Email

You can also send attachments through this class you just need to add this code


you just need to give path and that file will be attached to the email.
Send Email From Localhost PHP

You May Also Like

Leave a Reply

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