PHP邮件程序SMTP错误

时间:2012-09-24 17:04:19

标签: php smtp gmail phpmailer

您好我正在使用phpMailer发送电子邮件。它在服务器上工作正常,但当我尝试从我的本地机器(localhost)发送邮件时,它给了我错误。我正在使用GMAIL smtp

<?
require("lib/class.phpmailer.php"); 
$mail = new PHPMailer();
$mail->IsSMTP(); 
$mail->SMTPAuth = true;
$mail->Mailer = "smtp";
$mail->Host = "ssl://smtp.gmail.com";
$mail->Port = 465; 
$mail->Username = "XXX@XXX.com";  // SMTP username
$mail->Password = "XXXXXXX"; // SMTP password
$mail->From = $email; 
$mail->AddAddress("XXXXXX@XXX.com", "XX XX XX");
$mail->WordWrap = 50;
$mail->IsHTML(true); 
$mail->Subject = "You have received feedback from your website!";
$mail->Body    = $message;
$mail->AltBody = $message; 
if(!$mail->Send())
 {
   echo "Message could not be sent. <p>";
   echo "Mailer Error: " . $mail->ErrorInfo;
    exit;
 }

 }
 ?>

,错误是

SMTP Error: Could not connect to SMTP host. Message could not be sent.

Mailer Error: SMTP Error: Could not connect to SMTP host.

它在我的服务器上工作正常。

1 个答案:

答案 0 :(得分:1)

好的,我在本地获取的是我使用的代码

<?php
    require("phpmailer/class.phpmailer.php");
    $mail = new PHPMailer();
    $mail->IsSMTP(); // send via SMTP
    IsSMTP(); // send via SMTP
    $mail->SMTPAuth = true; // turn on SMTP authentication
    $mail->Username = "username@gmail.com"; // SMTP username
    $mail->Password = "password"; // SMTP password
    $webmaster_email = "username@doamin.com"; //Reply to this email ID
    $email="username@domain.com"; // Recipients email ID
    $name="name"; // Recipient's name
    $mail->From = $webmaster_email;
    $mail->FromName = "Webmaster";
    $mail->AddAddress($email,$name);
    $mail->AddReplyTo($webmaster_email,"Webmaster");
    $mail->WordWrap = 50; // set word wrap
    $mail->IsHTML(true); // send as HTML
    $mail->Subject = "This is the subject";
    $mail->Body = "This is the HTML BODY "; //HTML Body
    $mail->AltBody = "This is the body when user views in plain text format"; //Text Body
    if(!$mail->Send())
    {
    echo "Mailer Error: " . $mail->ErrorInfo;
    }
    else
    {
    echo "Message has been sent";
    }
    ?>