dongwu8064 2013-07-23 10:17
浏览 250
已采纳

我无法连接到SMTP主机

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 enter image description here

enter image description here

enter image description here

  • 写回答

1条回答 默认 最新

  • dongranding3909 2013-07-24 16:03
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效