Tag Archives: SMTP Mail

Implement PHP mailer with smtp authentication

smtpmailer

This tutorial for implement the PHP mailer with smtp authentication. you can set smtp authentication mail to your website using this. so simply follow the setup and you can get working smtp authentication mail.

Step 1)

Download the php mailer library from here. click here for download

Step 2)

Make a function for send a mail as below.

function phpMailerMailNew($FromDisplay, $FromEmail, $ReplyTo, $ToName, $ToEmail, $myCCList, $myBCCList, $Subject, $HTMLMsg, $TxtMsg, $AttFile, $AttFileName, $AttFileType){
       if(isset($ToEmail) && validateEmail($ToEmail))
       {
        require_once('phpMailer/PHPMailerAutoload.php');

        $mail = new PHPMailer();
        # -- prepare mail body
        $message = strip_tags($HTMLMsg);
        $message = str_replace("\t","",$message);
        $message = str_replace(" ","",$message);
        # --      
        if(!isset($FromDisplay) || strlen(trim($FromDisplay))==0)
            $FromDisplay = $FromEmail;  
        if(!isset($ToName) || strlen(trim($ToName))==0)
            $ToName = $ToEmail;
        # -- add ccs
        if(isset($myCCList) && strlen(trim($myCCList)) > 0)
        {
            $tempCCs = explode(",", $myCCList);            
            for($c = 0;$c<count($tempCCs);$c++)
                if(validateEmail($tempCCs[$c]))
                    $mail->AddCC($tempCCs[$c]);
        }       
        # ---
        # -- add bccs
        if(isset($myBCCList) && strlen(trim($myBCCList)) > 0)
        {
            $tempBCCs = explode(",", $myBCCList);
            for($c = 0;$c<count($tempBCCs);$c++)
                if(validateEmail($tempBCCs[$c]))
                    $mail->AddBCC($tempBCCs[$c]);
        }       
        # --
        $mail->SMTPOptions = array(
            'ssl' => array(
                'verify_peer' => false,
                'verify_peer_name' => false,
                'allow_self_signed' => true
            )
        );

        $mail->IsSMTP(); // set mailer to use SMTP
        $mail->SMTPDebug = 0;
        
        $mail->Timeout  =   60;

        $mail->Port = 465;
        $mail->SMTPSecure = 'ssl'; 

        $mail->Host = "";  // mail.example.com
        $mail->SMTPAuth = true; // turn on SMTP authentication
        $mail->Username = ""; // SMTP username like example@domain
        $mail->Password = ""; // SMTP password-

             
        $mail->FromName = $FromDisplay;
        $mail->From = $FromEmail;
        $mail->AddAddress($ToEmail,$ToName);

        # -- if a reply to is set, add it.

        if(validateEmail($ReplyTo))
            $mail->AddReplyTo($ReplyTo);

        if(strlen(trim($HTMLMsg)) > 0)
        {

            $mail->IsHTML(true); // set email format to HTML

            $mail->Body = $HTMLMsg;
            if(strlen(trim($TxtMsg)) >0)
            {
                $mail->AltBody = $TxtMsg;
            }
            else
            {
                $message = strip_tags($HTMLMsg);
                $message = str_replace("\t","",$message);
                $message = str_replace("&nbsp;","",$message);
                $mail->AltBody =    $message;
            }
        }
        else
        {
            $mail->IsHTML(false);
            $mail->Body = $TxtMsg;
        }
        $mail->Subject = $Subject;

        if(strlen(trim($AttFile))> 0 && file_exists($AttFile))
        {

            $mail -> AddAttachment($AttFile,$AttFileName);

        }
        $mail->Send();
    }

}

Step 3)

Now use above function for sent the smtp authentication mail via smtp.