需要修复PHP邮件程序错误

时间:2015-05-08 13:50:39

标签: php email phpmailer

我正在使用PHP邮件程序实现邮件功能。 代码不是在线工作,而是在本地机器上工作。在本地机器上,代码成功发送邮件,但在在线网站上显示以下错误:

SMTP -> ERROR: Failed to connect to server: ()
SMTP Error: Could not connect to SMTP host. 

代码是:

<?php
    include "classes/class.phpmailer.php"; // include the class name

    $mail1 = new PHPMailer(); // create a new object
    $mail1->IsSMTP(); // enable SMTP
    $mail1->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
    $mail1->SMTPAuth = true; // authentication enabled
    $mail1->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
    $mail1->Host = "smtp.gmail.com";
    $mail1->Port = 465; // or 587
    $mail1->IsHTML(true);
    $mail1->Username = "yourmail@gmail.com";
    $mail1->Password="password";        
    $mail1->SetFrom("yourmail@gmail.com");
    $mail1->Subject = "Working";
    $mail1->Body ="Hi, you got email";
    $mail1->AddAddress("yourmail2@gmail.com"); 
    $mail1->Send(); 
?>

1 个答案:

答案 0 :(得分:-2)

首先,您必须编辑“php.ini”要查找此文件,请使用WAMP服务器中的以下代码显示phpinfo。创建一个php文件并添加此内容。

<?php
     echo phpinfo();
?>

搜索“已加载的配置文件”,这将是您的php.ini的路径。

在此文件中删除给予extension = php_openssl.dll的;(半冒号)。

下载PHPMailer包后     提取将完整文件夹复制到项目文件夹中。     使用doc文件夹获取帮助。在测试文件夹中有一个名为testemail.php的文件。     根据需要更改参数。 (下面给出的例子)。     然后在浏览器中输入127.0.0.1/PHPMailer/test/testemail.php。     如果发送电子邮件,它将显示成功的消息,否则它将给出错误消息。

示例:

//add these codes if not written
$mail->IsSMTP();
$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
$mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
$mail->Port       = 465;

//You have to change these parameters to your requirements.


$mail->Username = “abc@gmail.com”; // GMAIL username
$mail->Password = “abcdefgh”; // GMAIL password
//..code ... There are many other functions to attach file etc.. For that refer doc file.
$mail->AddAddress(“destination@gmail.com”,”Nick name”);