RSS

How to Send Mail From Localhost-PHP

08 Oct

If  you are using wamp in a windows machine,its quite hard to send mail through native php mail function or sendmail or SMTP.But you can use PHP Mailer program to send mail from localhost.It bypasses native mail,sendmail methods.It works well on windows as well as *nix machines.

Download PHP Mailer from http://sourceforge.net/projects/phpmailer/files/phpmailer%20for%20php5_6/ or from http://phpmailer.worxware.com/

Sample code

<?php
require($_SERVER['DOCUMENT_ROOT'].'/phpmailer/class.phpmailer.php');
require($_SERVER['DOCUMENT_ROOT'].'./phpmailer/class.smtp.php');
$mail  = new PHPMailer();
$body="<b>This mail is sent using PHP Mailer</b>";#HTML tags can be included
$mail->IsSMTP();
$mail->SMTPAuth  = true;                 #enable SMTP authentication
$mail->SMTPSecure = "ssl";               #sets the prefix to the server
$mail->Host  = "smtp.gmail.com";         #sets GMAIL as the SMTP server
$mail->Port       = 465;                 #set the SMTP port
$mail->Username   = "";                  #your gmail username
$mail->Password   = "";                  #Your gmail password
$mail->From       = "";                  #your gmail id
$mail->FromName   = "";                  #your name
$mail->Subject    = "Subject of the mail";
$mail->WordWrap   = 50;
$mail->AddAddress("recipient mail id","recipient name");
$mail->IsHTML(true); // send as HTML
if(!$mail->Send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent";
}

?>

You can also send mail with attachments.For more info refer the documentation of PHP Mailer.

Caution:

Change SMTP Host,port,authentication only if sure of what you are doing.

 
3 Comments

Posted by on October 8, 2010 in PHP, Web Development

 

Tags: , , , , , , , , , , , ,

3 responses to “How to Send Mail From Localhost-PHP

  1. Rajasekar

    October 16, 2010 at 11:22 PM

    Thanks man, i got the solution for my 3 years search… i need a contact with u to learn more… mail me… again thanks bro

     
    • Srivats

      October 16, 2010 at 11:28 PM

      Thank you for your comments.Its always great to share knowledge.Feel free to contact me!

       
  2. hirenbhut

    January 11, 2014 at 2:48 PM

    this is properly working very very than you

     

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: