PHPMailer 无法在实时服务器上发送电子邮件

我相信以前有人问过这个问题,但我似乎无法找到解决问题的方法。


我正在使用 PHPMailer 处理网站上的电子邮件。在本地主机上一切正常,但在实时服务器上不起作用。这是我的代码:


<?php

require 'PHPMailerAutoload.php';

$mail = new PHPMailer;


$mail->IsSMTP();

$mail->Host='smtp.gmail.com';

$mail->Port=587;

$mail->SMTPAuth=true;

$mail->SMPTSecure='tls';

$mail->SMTPDebug = 3;

$mail->Username='*********';

$mail->Password='*********';


$mail->setFrom('*********', '********* Booking', 0);

$mail->addAddress('*********');


$name = "First Name :" . $_POST['firstname'] . "<br>";

$email = "Email :" . $_POST['email'] . "<br>";

$number = "Contact Number :" . $_POST['number'] . "<br>";

$message = "message :" . $_POST['message'] . "<br>";

$truck = "Truck Selected :" . $_POST['item'] . "<br>";



$mail->isHTML(true);

$mail->Subject='********* Booking';

$mail->Body= "$name $email $number $message $truck";



if(!$mail->send()) {

  echo 'Message was not sent.';

  echo 'Mailer error: ' . $mail->ErrorInfo;

} else {

  header('Location: /index.php');

  echo 'Message was sent.';

  exit;

}

使用上述代码在实时服务器上发送电子邮件会导致以下结果:


2020-01-22 07:51:48 Connection: opening to smtp.gmail.com:587, timeout=300, options=array ()

2020-01-22 07:51:48 Connection failed. Error #2: stream_socket_client() [<a href='function.stream-socket-client'>function.stream-socket-client</a>]: unable to connect to smtp.gmail.com:587 (Connection refused) [/websites/cv/*********/phpmailer/class.smtp.php line 299]

2020-01-22 07:51:48 SMTP ERROR: Failed to connect to server: Connection refused (111)

SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

Message was not sent.Mailer error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

但是,如果我禁用$mail->IsSMTP();某些人建议的 ,则不会显示错误,但不会发送/接收电子邮件。


我尝试过的事情:

  • 使用 ssl,端口 465

  • 在发送电子邮件时打开不太安全的应用程序

  • 联系我的主机询问是否有任何端口被阻止,他们说没有

  • 确保 openssl 处于活动状态

  • 确保登录凭据正确

做了所有这些事情后,它仍然说使用 SMTP 时连接被拒绝。有谁知道这可能是什么原因造成的?


慕虎7371278
浏览 116回答 2
2回答

GCT1015

这听起来像是网络/系统管理员问题,而不是 PHPMailer 问题。试试telnet&nbsp;smtp.gmail.com&nbsp;587&nbsp;&nbsp;//&nbsp;host&nbsp;and&nbsp;port在您的控制台中,用于检查连通性。如果一切正常,它应该写一条消息,如“ESMTP MAIL 服务就绪”。否则,该服务器会阻止您的服务器。

慕的地10843

请在您的文件中添加类 smtp 或使用此代码并相应地更改凭据它将起作用。include_once('class.phpmailer.php');&nbsp;require_once('class.smtp.php');$name = strip_tags($_POST['full_name']);$email = strip_tags ($_POST['email']);$msg = strip_tags ($_POST['description']);$subject = "Contact Form from DigitDevs Website";$mail = new PHPMailer();$mail->IsSMTP();$mail->CharSet = 'UTF-8';$mail->Host&nbsp; &nbsp; &nbsp; &nbsp;= "mail.example.com"; // SMTP server example//$mail->SMTPDebug&nbsp; = 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// enables SMTP debug information (for testing)$mail->SMTPAuth&nbsp; &nbsp;= true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // enable SMTP authentication$mail->Port&nbsp; &nbsp; &nbsp; &nbsp;= 26;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // set the SMTP port for the GMAIL server$mail->Username&nbsp; &nbsp;= "info@example.com"; // SMTP account username example$mail->Password&nbsp; &nbsp;= "password";&nbsp; &nbsp; &nbsp; &nbsp; // SMTP account password example$mail->From = $email;$mail->FromName = $name;$mail->AddAddress('info@example.com', 'Information');&nbsp;$mail->AddReplyTo($email, 'Wale');$mail->IsHTML(true);$mail->Subject = $subject;$mail->Body&nbsp; &nbsp; =&nbsp; $msg;$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';if(!$mail->Send()) {echo 'Message could not be sent.';echo 'Mailer Error: ' . $mail->ErrorInfo;exit;}&nbsp;echo 'Message has been sent';
打开App,查看更多内容
随时随地看视频慕课网APP