Wednesday, March 18, 2015

How to send an email from localhost in PHP

Sometimes we may need to send emails from localhost for the purpose of testing, and as you know that localhost doesn't support sending email function. Recently I came across a useful and effective PHP library called PHPMailer developed by PHPMailer team, the PHPMailer Library is very flexible and easy and you can simply send SMTP mail easily from your local server to the specified email
  • in the beginning  you will need to download the PHPMailer library from PHPMailer Library.
  • in this example i chose to send STMP mail using Gmail Account, as you can configure other services such as Yahoo or Hotmail according to your desire.
  • create a file named "test.php".
paste the below code in "test.php".
the PHP code:
<?php
require 'PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'yourgmailaccount@gmail.com';
$mail->Password = 'yourpassword';
$mail->SMTPSecure = 'tls';
$mail->From = 'yourgmailaccount@gmail.com';
$mail->FromName = 'the Recipient Name ';
$mail->addAddress('additionaladdress@gmail.com', 'add Title');
$mail->addReplyTo('yourgmailaccount@gmail.com', 'add title');
$mail->WordWrap = 50;
$mail->isHTML(true);
$mail->Subject = 'PHPMailer';
$mail->Body    = 'Hi, this is a test to send a SMTP mail from localhost using PHPMailer library ';
if(!$mail->send()) {
   echo 'Email could not be sent.';
   echo 'Mailer Error: ' . $mail->ErrorInfo;
   exit;
}
echo 'Email has been sent';
?> 

that's all, I hope this example was helpful for you guys.

How to Send an Email in PHP

we will learn in this lesson the way of sending an email through getting the textbox value and send it automatically to a specific email once the user clicks the submit button, as you know that this thing is mostly required by the websites' owners' to create a contact us form for the purpose of feedbacks or complaints that are provided by the website visitors or also can be used for any other purpose such as tracking your website.

in this lesson we will perform a (simple contact us form) and then will send the content of the form to a specific email.

let's create a php page with a name of "contact.php", and paste the code below into "contact.php"

<?php 
if(isset($_POST['submit'])){
$msg= 'Name: ' .$_POST['name']."\n".'E-mail: '.$_POST['email']."\n".'message: '.$_POST['message'];
mail('youremail@gmail.com','Sample Contact Form',$msg);
echo"your email has been sent to us, thank you";
}
?>
<html>
<body>

<form action="contact.php" method="post">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
Message: <input type="text" name="message"><br>
<input type="submit" name="submit">
</form>

</body>
</html>


Notice: in order to execute the process of sending an email, you will need to upload this web page to a web host, the function of sending email doesn't work with localhost server. you can use a free web hosting such as 000webhost.com to test your work.

if you want to send an email from localhost in PHP, then you will need to go through send an email from localhost in php.