phpMailer在托管后不工作
phpMailer脚本在localhost上运行良好,但一旦上载到000webhost.com,它会向我显示以下错误消息:phpMailer在托管后不工作,php,timeout,phpmailer,Php,Timeout,Phpmailer,phpMailer脚本在localhost上运行良好,但一旦上载到000webhost.com,它会向我显示以下错误消息: SMTP -> ERROR: Failed to connect to server: Connection timed out (110) The following From address failed: abc@gmail.com : Called Mail() without being connected Mailer Error: Th
SMTP -> ERROR: Failed to connect to server: Connection timed out (110)
The following From address failed: abc@gmail.com : Called Mail() without being connected Mailer Error: The following From address failed: abc@gmail.com : Called Mail() without being connected
这是我的剧本:
<?php
require_once('class.phpmailer.php');
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
////////extracting form data from POST/////////
$course = $_REQUEST["Course__2"];
$receiver_name = $_REQUEST["Name__1"];
$receiver_email = $_REQUEST["Email__3"];
/////////////////
$mail = new PHPMailer();
$body = "Hi .";
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 587; // set the SMTP port for the GMAIL server
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Username = "abc@gmail.com"; // GMAIL username
$mail->Password = "00000000"; // GMAIL password
$mail->SMTPSecure = "tls"; // sets the prefix to the servier
$mail->SMTPDebug = 1; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->SetFrom('abc@gmail.com', ' Global'); //sender's email address
$mail->AddReplyTo("abc@gmail.com","Global"); //A reply to address
$mail->Subject = "10% Off Voucher";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
$address = $receiver_email; //receiver's address
$mail->AddAddress($address, $receiver_name); //name of receiver
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
可能是由于某种原因,您的主机没有为带有PHP的Apache激活OpenSSL扩展
也就是说,发送邮件并不需要SMTP。事实上,在Unix/Linux主机上发送邮件的典型方式是通过本地sendmail
SMTP。因为您正在使用PHPMailer
查找PHPMailer
存储库中的examples/
文件夹。我建议您在远程主机上使用此选项而不是SMTP:
require '../PHPMailerAutoload.php';
//Create a new PHPMailer instance
$mail = new PHPMailer();
// Set PHPMailer to use the sendmail transport
$mail->isSendmail();
//Set who the message is to be sent from
$mail->setFrom('from@example.com', 'First Last');
//Set an alternative reply-to address
$mail->addReplyTo('replyto@example.com', 'First Last');
//Set who the message is to be sent to
$mail->addAddress('whoto@example.com', 'John Doe');
//Set the subject line
$mail->Subject = 'PHPMailer sendmail test';
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__));
//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';
//Attach an image file
$mail->addAttachment('images/phpmailer_mini.png');
//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
另外,检查000WebHost
上的远程服务器是否允许访问Google的TLS端口的另一种方法是使用此脚本。把它放在主机上的PHP文件中,然后将其加载到web浏览器中
<?php
$fp = fsockopen('tls://smtp.gmail.com:465');
if(!$fp)
{
echo 'Unable to connect';
}
else
{
$response = fgets($fp, 256);
echo 'Response: ' . $response;
}
?>
我得到的成功信息是:
回复:220 mx.google.com ESMTP r14sm2233165qga.4-gsmtp
如果失败,响应将如下所示:
警告:fsockopen():php\u network\u getaddresses:getaddrinfo失败:
提供了nodename或servname,或中未知
/第3行的Applications/MAMP/htdocs/test.php
如果失败了,那么您就知道000WebHost
和输出到Gmail的TLS连接有问题。在我的服务器上工作正常。您使用的是000webhost免费托管还是付费托管?同样的代码,我已经在我的XAMPP上进行了测试。很好,抱歉,各位。错误的盒子。下面是评论:000webhost正在阻止Gmail。你使用的是免费帐户吗?你需要和他们确认一下,但我确信这就是问题所在。@JakeGould实际上是这样的。免费主机并没有付费主机提供的功能多。甚至资源和其他东西都是有限的。我使用的是免费版本的00webhost。是的,它在我的本地主机(XAMPP)上可以正常工作,但一旦托管就无法工作。所以你们认为问题在于免费版本阻止了它?@Zoubeir好吧,如果你得到一个错误,这意味着000WebHost
正在阻止SMTP端口。我建议您使用我的示例中所示的sendmail
。是的,这一定是因为我在使用sendmail时也会遇到以下错误:Mailer错误:无法执行:/usr/sbin/sendmail谢谢!对不起,我不能投票支持你的答案,因为我显然缺乏声誉,但感谢你的帮助。我需要找到另一个主机(免费)可能至少支持sendmail。