Sending mail using SMTP from localhost using PHPMailer


When you are tesPHPMailerdeveloped by the PHPMailer team. It makes the job very easy and you can send SMTP mail easily from your local server. It is very easy to integrate the library to your web applications.
ting your Web application in localhost or if you are using W‌indows Operating System it is difficult to setup mail server to send email. Recently I came across a useful PHP library called

Download

Download PHPMailer library from https://github.com/PHPMailer/PHPMailer.

Usage Instructions

Here I am using my Gmail account to send SMTP mail. You can also configure other services such as Yahoo , Outlook to send mail using SMTP. Create a file named mail.php outside the downloaded PHPMailer directory and use the code similar to the sample given below.
PHP Code
<?php
require 'PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'raj.amalw@gmail.com';
$mail->Password = '******';
$mail->SMTPSecure = 'tls';
$mail->From = 'raj.amalw@gmail.com';
$mail->FromName = 'Raj Amal';
$mail->addAddress('raj.amalw@learn2crack.com', 'Raj Amal W');
$mail->addReplyTo('raj.amalw@gmail.com', 'Raj Amal W');
$mail->WordWrap = 50;
$mail->isHTML(true);
$mail->Subject = 'Using PHPMailer';
$mail->Body    = 'Hi Iam using PHPMailer library to sent SMTP mail from localhost';
if(!$mail->send()) {
   echo 'Message could not be sent.';
   echo 'Mailer Error: ' . $mail->ErrorInfo;
   exit;
}
echo 'Message has been sent';
The SMTP host of Gmail is smtp.gmail.com. It will vary for Outlook and Yahoo mail. The to Address should be set in addAddress(). I think it will be really helpful for you guys.
SHARE

Bilal Kashif

Hi.I am admin of Insectofcomputers and also Web Designer, Industrial Designer, Web Developer, Business Enthusiast, StartUp Enthusiast, Speaker, Writer and Photographer.

  • Image
  • Image
  • Image
  • Image
    Blogger Comment
    Facebook Comment

0 comments:

Ads