在php-mailer中,smtp连接失败


in php mailer the smtp connection is failed

我想从本地主机向mail-id发送一封邮件。我正在使用php-Mailer。但它表示CCD_ 2连接失败。有人能帮我吗?我的代码如下:

<?php
    require_once('class.phpmailer.php');

    $mail = new PHPMailer();
    $body='hai';
    $address='stin@f12technologies.com';
    $name='hey';
    $mail->IsSMTP();
    $mail->SMTPAuth = true;
    $mail->Host = "localhost";
    $mail->Port = 25;
    $mail->Username = "#@#@#@#@-####-@@@@-#####-@#@#@#@#@#@#";
    $mail->Password = "#@#@#@#@-####-@@@@-#####-@#@#@#@#@#@#";
    $mail->SetFrom('stinjohnece@gmail.com','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;
    }
    ?>

您必须提供smtp详细信息

在反弹(重新启动)您的apache一次之后。

<?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 = 'admin@XXXX.com';               
  $mail->Password = 'XXXXX';                         
 //$mail->SMTPSecure = 'None';                      
 $mail->Port = 25;                                
 $mail->setFrom('admin@XXXX.com', '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';
         }
         ?>
$mail = new PHPMailer;
$mail->IsSMTP();  // telling the class to use SMTP
$mail->SMTPAuth=true;
//$mail->SMTPDebug=2;
$mail->Host='smtp.gmail.com'; // SMTP server
$mail->Username ='...@gmail.com';
$mail->Password ='...';
$mail->SMTPSecure='ssl';
$mail->Port=465;
$mail->isHTML(true);

PHP邮件文档示例

<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3;                               // Enable verbose debug output
$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'user@example.com';                 // SMTP username
$mail->Password = 'secret';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;                                    // TCP port to connect to
$mail->setFrom('from@example.com', 'Mailer');
$mail->addAddress('joe@example.net', 'Joe User');     // Add a recipient
$mail->addAddress('ellen@example.com');               // Name is optional
$mail->addReplyTo('info@example.com', 'Information');
$mail->addCC('cc@example.com');
$mail->addBCC('bcc@example.com');
$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
$mail->isHTML(true);                                  // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}