Search code examples
phpsmtpphpmailer

in php mailer the smtp connection is failed


I want to send a a mail from local-host to a mail-id. i am using php-Mailer. but its saying that the SMTP connection is failed. can anyone help me please? MY CODE IS BELOW:

<?php

    require_once('class.phpmailer.php');


    $mail = new PHPMailer();

    $body='hai';
    $address='[email protected]';
    $name='hey';

    $mail->IsSMTP();
    $mail->SMTPAuth = true;
    $mail->Host = "localhost";
    $mail->Port = 25;
    $mail->Username = "#@#@#@#@-####-@@@@-#####-@#@#@#@#@#@#";
    $mail->Password = "#@#@#@#@-####-@@@@-#####-@#@#@#@#@#@#";

    $mail->SetFrom('[email protected]','Web App');
    $mail->Subject = "A Transactional Email From Web App";
    $mail->MsgHTML($body);
    $mail->AddAddress($address, $name);



    if($mail->Send()) {
        echo "Message sent!";
    } else {
        echo "Mailer Error: " . $mail->ErrorInfo;
    }

    ?>

Solution

  • you have to give smtp details

    after that bounce(restart) your apache once.

    <?php
    require dirname(__FILE__) .'/library/PHPMailer/PHPMailerAutoload.php';
    
     $mail = new PHPMailer(true);
    
     //$mail->SMTPDebug = 2;   // error mode                         
      //$mail->SMTPDebug = 3;   // error mode                         
    
     $mail->isSMTP();                                      
     $mail->Host = 'mail.xxx.com';  
     $mail->SMTPAuth = true;                              
     $mail->Username = '[email protected]';               
      $mail->Password = 'XXXXX';                         
     //$mail->SMTPSecure = 'None';                      
     $mail->Port = 25;                                
    
     $mail->setFrom('[email protected]', 'XXXXXX');
    
     //for sending mail
     $mail->addAddress($username);     // Add a recipient
    
    $mail->isHTML(true);        // Set email format to     HTML
    
     $mail->Subject = 'HAI';
      $mail->Body    = '<br>  <br>
                <html><body> <div><div><u><h3>HAI </h3></u></div><div><p>This email has been sent for testing</p><p>xxx<b>xx</b></p><p>xx<b>xx</b></p></div>       </body></html>';
      $mail->AltBody = 'Unable to display the mail';
    
      if(!$mail->send()) 
         {
         echo 'Message could not be sent.';
          echo 'Mailer Error: ' . $mail->ErrorInfo;
    
          } 
          else 
           {
          echo 'Message has been sent';
    
             }
             ?>