简体   繁体   English

我无法连接到SMTP主机

[英]I could not connect to SMTP host

I am using this code to try to send email using SMTP, but I have an error 我正在使用此代码尝试使用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>

this code have not any problem in local i could send email in local (wamp server) but in server i have errors: 此代码在本地没有任何问题,我可以在本地(垃圾服务器)发送电子邮件,但在服务器中我有错误:

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

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

I'm sure that ssl port is enable i call phpinfo() i had this result please help me 我确定ssl端口已启用,我致电phpinfo()我得到了这个结果,请帮帮我 在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

In light of all of the comments above, it sounds like there may be some problem with the local SMTP server on the machine that your PHP is running on. 根据以上所有评论,听起来您的PHP运行所在的计算机上的本地SMTP服务器可能存在一些问题。 You can try a few tests from the command line (like the ones mentioned above) to troubleshoot. 您可以从命令行尝试一些测试(例如上述测试)以进行故障排除。 Or, you might want to simply work around the local SMTP server on this machine, by using phpmailer to send outgoing mail by way of a remote SMTP relaying server. 或者,您可能只想通过使用phpmailer通过远程SMTP中继服务器发送传出邮件,来解决本机上的本地SMTP服务器。 If you have a gmail account, you can use smtp.gmail.com, or you can use any other SMTP server that you have access to. 如果您拥有Gmail帐户,则可以使用smtp.gmail.com,也可以使用有权访问的任何其他SMTP服务器。 phpmailer is simple to setup - just a few PHP files to copy to your server. phpmailer易于安装-仅需复制几个PHP文件到您的服务器即可。 See https://github.com/PHPMailer/PHPMailer . 参见https://github.com/PHPMailer/PHPMailer Then, you can start sending mail using the simple example at the github page above as a boilerplate. 然后,您可以使用上面github页上的简单示例作为样板开始发送邮件。 phpmailer will also handle all of your MIME encoding, so you don't have to do it from scratch like you're doing. phpmailer还将处理您所有的MIME编码,因此您不必像从前那样从头开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM