我无法连接到SMTP主机

时间:2013-07-23 10:17:24

标签: php email ssl smtp

我正在使用此代码尝试使用SMTP发送电子邮件,但我有错误

<html>
<head>
<title>PHPMailer - SMTP basic test with authentication</title>
</head>
<body>

<?php

//error_reporting(E_ALL);

phpinfo();

require("../class.phpmailer.php");
require("../class.smtp.php");

define("PHPMAILERHOST",'smtp.gmail.com');
date_default_timezone_set('Asia/Tehran');
$mail = new PHPMailer();
ini_set('display_errors', 1);

$mail->IsSMTP(); // set mailer to use SMTP
$mail->Host = "smtp.gmail.com"; // specify main and backup server
$mail->Port = "465"; // SMTP Port
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->SMTPSecure= "ssl"; // SMTP connection type
/************************************************** *********************************/
/************************************************** *********************************/
$mail->Username = "XXXXXXXXXX@gmail.com"; // SMTP username
$mail->Password = "XXXXXX"; // SMTP password
// Send email to :
$mail->AddAddress("masoudy.maryam@gmail.com"); // will receive the test email
/************************************************** *********************************/
/************************************************** *********************************/
//$mail->AddAddress("second-receiver@gmail.com", "Josh Adams");
//$mail->AddReplyTo("example@gmail.com", "Information");
$mail->WordWrap = 50; // set word wrap to 50 characters
//$mail->AddAttachment("/var/tmp/file.tar.gz"); // add attachments
//$mail->AddAttachment("/tmp/image.jpg", "new.jpg"); // optional name
$mail->IsHTML(true); // set email format to HTML
$mail->Subject = "hahahahahahahahahahhahhahahahhahahha";
$mail->Body = '<html><meta http-equiv="content-type" content="text/php; charset=utf-8"/><body>
layay layayya رسید بگو
</body></html>

';
$mail->AltBody = "This is the body in plain text for non-HTML mail clients";
if(!$mail->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
echo "Message has been sent";

?>

</body>
</html>

此代码在本地没有任何问题我可以在本地(wamp服务器)发送电子邮件但在服务器中我有错误:

SMTP Error: Could not connect to SMTP host. Message could not be sent.

Mailer Error: SMTP Error: Could not connect to SMTP host.

我确定ssl端口启用我调用phpinfo()我有这个结果 请帮我 enter image description here

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:1)

根据上述所有评论,听起来运行PHP的计算机上的本地SMTP服务器可能存在问题。您可以从命令行(如上面提到的那些)尝试一些测试来进行故障排除。或者,您可能希望通过使用phpmailer通过远程SMTP中继服务器发送外发邮件,简单地在本机上使用本地SMTP服务器。如果您有Gmail帐户,则可以使用smtp.gmail.com,也可以使用您有权访问的任何其他SMTP服务器。 phpmailer设置简单 - 只需几个PHP文件即可复制到您的服务器。见https://github.com/PHPMailer/PHPMailer。然后,您可以使用上面github页面上的简单示例作为样板开始发送邮件。 phpmailer也会处理你所有的MIME编码,所以你不必像现在这样从头开始。