0

您好我正在使用 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.

它在我的服务器上运行良好。

4

1 回答 1

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";
    }
    ?>
于 2012-09-24T17:10:43.790 回答