Phpmailer在本地主机中工作正常,但在服务器中无法正常工作


Phpmailer working fine in localhost But not in server

date_default_timezone_set('Asia/Dubai');
include("classes/class.phpmailer.php");
$mail = new PHPMailer();
$body = "this is <strong>testing</strong> mail ". date('Y-m-d H:i:s');
$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPDebug  = 1;

$mail->SMTPAuth   = true;
$mail->SMTPSecure = "ssl";               
$mail->Host       = "smtp.gmail.com";      
$mail->Port       = 465;              
$mail->Username   = 'my@email.com';
$mail->Password   = '*******';
$mail->SetFrom('my@email.com', 'First Last');
$mail->AddReplyTo('my@email.com','First Last');
$mail->Subject    = "PHPMailer Test Subject via smtp (Gmail), basic";
$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
$address = "to@email.com"; // add your address here 
$mail->AddAddress($address, "Gmail Test");
if(!$mail->Send()) {
  echo "Mailer Error: " . $mail->ErrorInfo;
} else {
  echo "Message sent!";
}

我有这样的脚本。它在本地主机上工作正常,但是当我移动到Windows或Linux服务器时,它将无法正常工作。我想在Windows和Linux服务器上工作。我该怎么办?

像这样的错误:SMTP -> 错误: 无法连接到服务器: 连接超时 (110(SMTP 错误: 无法连接到 SMTP 主机。邮件程序错误: SMTP 错误: 无法连接到 SMTP 主机。

尝试使用这个:

require_once ( 'class.phpmailer.php' ); // Add the path as appropriate
$Mail = new PHPMailer();
$Mail->IsSMTP(); // Use SMTP
$Mail->Host        = "smtp.gmail.com"; // Sets SMTP server
 $Mail->SMTPDebug   = 2; // 2 to enable SMTP debug information
$Mail->SMTPAuth    = TRUE; // enable SMTP authentication
$Mail->SMTPSecure  = "tls"; //Secure conection
$Mail->Port        = 587; // set the SMTP port
$Mail->Username    = 'MyGmail@gmail.com'; // SMTP account username
$Mail->Password    = 'MyGmailPassword'; // SMTP account password
$Mail->Priority    = 1; // Highest priority - Email priority (1 = High, 3 = Normal, 5 = low)
$Mail->CharSet     = 'UTF-8';
$Mail->Encoding    = '8bit';
$Mail->Subject     = 'Test Email Using Gmail';
$Mail->ContentType = 'text/html; charset=utf-8'r'n';
$Mail->From        = 'MyGmail@gmail.com';
$Mail->FromName    = 'GMail Test';
$Mail->WordWrap    = 900; // RFC 2822 Compliant for Max 998 characters per  line
$Mail->AddAddress( $ToEmail ); // To:
$Mail->isHTML( TRUE );
$Mail->Body    = $MessageHTML;
$Mail->AltBody = $MessageTEXT;
$Mail->Send();
$Mail->SmtpClose();
 if ( $Mail->IsError() ) { 
  echo "ERROR<br /><br />";
 }
 else {
  echo "OK<br /><br />";
  }

您使用的是哪个版本的 PHPMailer? 如果您使用的是旧版本,请尝试使用

$mail->Port = 587;    

尝试更新PHPMailer版本,这将解决我希望!!