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.

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

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀