在php邮件程序中,smtp连接失败

时间:2016-05-06 08:56:07

标签: php smtp phpmailer

我想从本地主机向mail-id发送邮件。我正在使用php-Mailer。但它说SMTP连接失败了。有人可以帮我吗?我的代码如下:

<?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;
    }

    ?>

3 个答案:

答案 0 :(得分:0)

你必须提供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';

         }
         ?>

答案 1 :(得分:-1)

$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);

答案 2 :(得分:-1)

PHP mailer docs example

<?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';
}