简体   繁体   中英

I could not connect to SMTP host

I am using this code to try to send email using SMTP, but I have an error

<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 在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

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. 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. 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. phpmailer is simple to setup - just a few PHP files to copy to your server. See https://github.com/PHPMailer/PHPMailer . Then, you can start sending mail using the simple example at the github page above as a boilerplate. phpmailer will also handle all of your MIME encoding, so you don't have to do it from scratch like you're doing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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