>  기사  >  백엔드 개발  >  SMTP Error: Could not connect to SMTP host.该如何解决

SMTP Error: Could not connect to SMTP host.该如何解决

WBOY
WBOY원래의
2016-06-13 12:49:461955검색

SMTP Error: Could not connect to SMTP host.
我用网页运行时正常,能发送,用命令运行php文件时报错,主要代码经检查无误,如下

function sendmail($setEmail, $setName, $body, $subject) {<br />
	require_once("PHPMail/phpmailer.php");<br />
	$mail = new PHPMailer ( true ); // the true param means it will throw exceptions on errors, which we need to catch<br />
	$mail->IsSMTP (); // telling the class to use SMTP<br />
	try {<br />
		$mail->SMTPAuth = true; // enable SMTP authentication<br />
		$mail->SMTPSecure = SBT_SSL; // sets the prefix to the servier<br />
		$mail->Host = SBT_SMTP_HOST; // sets GMAIL as the SMTP server<br />
		$mail->Port = SBT_SMTP_PORT; // set the SMTP port for the GMAIL server<br />
		$mail->Username = SBT_SMTP_USER; // GMAIL username<br />
		$mail->Password = SBT_SMTP_PASS; // GMAIL password<br />
		$mail->AddReplyTo ( SBT_MAIL_FROM, SBT_MAIL_FROM_NAME );<br />
		$mail->AddAddress ( $setEmail, $setName );<br />
		$mail->SetFrom (SBT_SMTP_USER,SBT_SMTP_USER); //sender name<br />
		$mail->Subject = $subject;		<br />
		$mail->MsgHTML ( $body );		<br />
		if (! $mail->Send ()) {//(这边没报错)<br />
			echo "Message could not be sent. <p>";<br />
			echo "Mailer Error: " . $mail->ErrorInfo;<br />
			exit ();<br />
		} <br />
	} catch ( phpmailerException $e ) {<br />
		echo $e->errorMessage (); //Pretty error messages from PHPMailer(就是这边输出错误的)<br />
	} catch ( Exception $e ) {<br />
		echo $e->getMessage (); //Boring error messages from anything else!<br />
	}<br />
}

问下是什么原因,目的是为了可以用linux计划任务来执行该PHP文件


------解决方案--------------------
错误代码呢?
------解决方案--------------------
php --ini 一下,看使用的ini是否和作为apache模块运行的php一致.
------解决方案--------------------
不可能,$mail->IsSMTP ();这个表明你已经设置了smtp发送了.
你现在的主要问题是找找看为什么socket连接不了smtp服务器.
防火墙也有可能.
------解决方案--------------------
wamp环境,应该是共用一份ini文件的.想再一次确定的话,php网页输出phpinfo(),看使用的ini文件,是否和这个一致.
命令:telnet smtp.gmail.com 465 ,确定能连接成功?
------解决方案--------------------
检查一下防火墙,另外再检查一下发送图片附件的代码
------解决方案--------------------
telnet都不行,肯定是你防火墙禁止了访问 
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.