“SMTP错误无法连接到SMTP主机。消息未发送

时间:2013-06-15 07:54:09

标签: smtp phpmailer

我正在使用一个webmail,我正在使用phpmailer类。问题是我收到错误“SMTP错误:无法连接到SMTP主机。消息未发送 PHP邮件程序错误:SMTP错误:无法连接到SMTP主机。“

我的代码是:

 <html>
<head>
<title>PHPMailer - SMTP basic test with authentication</title>
</head>
<body>

<?php

//error_reporting(E_ALL);
error_reporting(E_STRICT);

date_default_timezone_set('America/Toronto');

require_once('../class.phpmailer.php');
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded

$mail             = new PHPMailer();

$body             = file_get_contents('contents.html');
$body             = eregi_replace("[\]",'',$body);

$mail->IsSMTP();
$mail->Port = 465;
$mail->Host = 'smtp.gmail.com'; // "ssl://smtp.gmail.com" didn't worked
$mail->IsHTML(true); // if you are going to send HTML formatted emails
$mail->Mailer = 'smtp';
$mail->SMTPSecure = 'ssl';

$mail->SMTPAuth = true;
$mail->Username = "email@gmail.com";
$mail->Password = "mypass";

$mail->SingleTo = true; // if you want to send mail to the users individually so that no recipients can see that who has got the same email.

$mail->From = "email@gmail.com";
$mail->FromName = "mypass";

$mail->addAddress("livelyphoneix@yahoo.com","User 1");
$mail->addAddress("user.2@gmail.com","User 2");

$mail->addCC("user.3@ymail.com","User 3");
$mail->addBCC("user.4@in.com","User 4");

$mail->Subject = "Testing PHP Mailer with localhost";
$mail->Body = "Hi,<br /><br />This system is working perfectly.";

if(!$mail->Send())
    echo "Message was not sent <br />PHP Mailer Error: " . $mail->ErrorInfo;
else
    echo "Message has been sent";

?>

</body>
</html>

And my php.ini setting is :

    [mail function]
; For Win32 only.
; http://php.net/smtp
;SMTP =
; http://php.net/smtp-port
;smtp_port = 465

; For Win32 only.
; http://php.net/sendmail-from
;sendmail_from = you@domain.com
; For Unix only.  You may supply arguments as well (default: "sendmail -t -i").
; http://php.net/sendmail-path
sendmail_path ="C:\wamp\sendmail\sendmail.exe -t -i"

; Force the addition of the specified parameters to be passed as extra parameters
; to the sendmail binary. These parameters will always replace the value of
; the 5th parameter to mail(), even in safe mode.
;

有人想让我知道我错了,我需要你关注这个问题。

2 个答案:

答案 0 :(得分:2)

尝试替换

 $mail->SMTPSecure = 'ssl';

通过

$mail->SMTPSecure = 'tls';

答案 1 :(得分:0)

你的smtp主机设置正确但是你的$ mail-&gt; Mailer =&#39; smtp&#39 ;; 应该是$ mail-&gt; Mailer =&#39; mail&#39 ;; 除非你有自己的smtp服务器发送直接smtp?.. 我不太确定,但我已经体验过这个......;)