2015-11-19 39 views
0

我想使用SMTP服務器(gmail)從localhost XAMPP使用phpmailer發送郵件。但我不斷收到此錯誤:PHPmailer:SMTP連接()失敗(不工作)

郵件無法發送。郵件錯誤:SMTP連接()失敗。 https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

我嘗試了許多解決方案,取消註釋php.ini文件中的openSSL,更改端口465(「ssl」)和587(「tls」),但它不起作用。

我的代碼:

<?php 
date_default_timezone_set('Etc/UTC'); 
'PHPMailerAutoLoad.php'; 
class.phpmailer.php if not already loaded 
$port =465; 
$securetype = 'ssl'; 
$from = '[email protected]'; 
$name = 'User'; 
$toemail= "[email protected]"; 

$mail = new PHPMailer; 
$mail->isSMTP(); 
$mail->isSMTPDebug = 1; 
$mail->Host = 'smtp.gmail.com'; 

$mail->SMTPAuth = true; 
$mail->Username = '[email protected]'; 
$mail->Password = 'password'; 
$mail->SMTPSecure = $securetype; 
$mail->Port = $port; 

$mail->From = $from; 
$mail->FromName = $name; 
$mail->addAddress($toemail); 

$mail->isHTML(true); 

$mail->Subject = 'Test Mail Subject!'; 
$mail->Body = 'This is SMTP Email Test'; 

if(!$mail->send()) { 
    echo 'Message could not be sent.'; 
    echo 'Mailer Error: ' . $mail->ErrorInfo; 
} else { 
    echo 'Message has been sent'; 
} 
?> 
+0

您使用的是一箇舊的gmail示例,這表明您沒有閱讀或完成您鏈接的文檔告訴您的內容。 – Synchro

回答

0

您需要class.phpmailer.php。如果您使用SMTP,則需要class.smtp.php。 試試這個演示代碼:

<?php 
     require 'class.phpmailer.php'; 
      require 'class.smtp.php'; 
    //SMTP needs accurate times, and the PHP time zone MUST be set 
    //This should be done in your php.ini, but this is how to do it if you don't have access to that 
    #require '../PHPMailerAutoload.php'; 
    //Create a new PHPMailer instance 
    $mail = new PHPMailer; 
    //Tell PHPMailer to use SMTP 
    $mail->isSMTP(); 
    //Enable SMTP debugging 
    // 0 = off (for production use) 
    // 1 = client messages 
    // 2 = client and server messages 
    $mail->SMTPDebug = 0; 
    //Ask for HTML-friendly debug output 
    $mail->Debugoutput = 'html'; 
    //Set the hostname of the mail server 
    $mail->Host = 'mail.domain.com'; 
    // use 
    // $mail->Host = gethostbyname('smtp.gmail.com'); 
    // if your network does not support SMTP over IPv6 
    //Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission 
    $mail->Port = 25; 
    //Set the encryption system to use - ssl (deprecated) or tls 
    //$mail->SMTPSecure = 'tls'; 
    //Whether to use SMTP authentication 
    $mail->SMTPAuth = true; 
    //Username to use for SMTP authentication - use full email address for gmail 
    $mail->Username = "[email protected]"; 
    //Password to use for SMTP authentication 
    $mail->Password = "password"; 
    //Set who the message is to be sent from 
    $mail->setFrom('[email protected]', 'subillion'); 
    //Set an alternative reply-to address 
    #$mail->addReplyTo('[email protected]', 'First Last'); 
    //Set who the message is to be sent to 
    $mail->addAddress("[email protected]"); 

    //Set the subject line 
    $mail->Subject = 'Demo !!'; 
    //Read an HTML message body from an external file, convert referenced images to embedded, 
    $mail->isHTML(true); 

    $msgbody = "This is a demo test !"; 
    $mail->Body = $msgbody; 
    //send the message, check for errors 
    if (!$mail->send()) { 
    // echo "Mailer Error: " . $mail->ErrorInfo; 
    } else { 
     echo "Message sent!"; 
    } 
?>