Phpmailer在localhost中工作正常但不在服务器中

时间:2015-03-21 05:43:35

标签: php smtp phpmailer

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!";
}

我有这样的脚本。它与localhost工作正常,但是当我移动到Windows或Linux服务器时,它不会工作。我想在Windows和Linux服务器上工作。我该怎么办?

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

2 个答案:

答案 0 :(得分:0)

尝试使用:

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 />";
  }

答案 1 :(得分:-3)

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

$mail->Port = 587;    

尝试更新我希望解决的PHPMailer版本!!!