PHPMailer没有发送邮件

时间:2018-08-23 16:49:10

标签: php

在将数据保存到数据库之后,尝试发送确认邮件。我用过PHPMailer。

我的代码

<?php include_once 'config/init.php'; ?>
<?php
use PHPMailer\PHPMailer\PHPMailer;

$user = new user();

if(isset($_POST['submitsignup'])){

$date = array();

if($_POST['email']=='' || $_POST['password']=='' || $_POST['first_name']=='' || $_POST['last_name']==''){
    echo "Fill all the stuff";
}
else{ 
    $result=$user->checkemails($_POST['email']);
        if(!$result){
            $data["email"] = $_POST['email'];
            $data['password'] = $_POST['password'];
            $data['first_name'] =$_POST['first_name'];
            $data['last_name'] = $_POST['last_name'];

            $token = 'qwertyuiopasdfghjklzxcvbnm!$#()/1234567890';
            $token = str_shuffle($token);
            $token = substr($token,0,10);

            $user->signup($data,$token);

            include_once "PHPMailer/PHPMailer.php";

            $mail = new PHPMailer();
            $mail->setFrom("ahsan44411@gmail.com");
            $mail->addAddress($data["email"],$data["first_name"]);
            $mail->Subject = "Please verify name";
            $mail->isHTML(true);
            $mail->Body = "Please click on the Link below <br><br> 
            <a href='localhost/index.php'></a>";
            if($mail->send()){
                echo "Message sent";
            }else{
                echo "Something went wrong";
            }
        }
    else{
            echo "Email aleay present";
        }
    }   

}


$template = new Template('templates/signuptemp.php');

但是我遇到这样的错误

警告:require_once(lib / PHPMailer \ PHPMailer \ Exception.php):无法打开流:在第10行的C:\ wamp64 \ www \ jobLister \ config \ init.php中没有这样的文件或目录< / strong>

严重错误:require_once():无法在C:\ wamp64 \ www \ jobLister中打开所需的'lib / PHPMailer \ PHPMailer \ Exception.php'(include_path ='.; C:\ php \ pear')第10行上的\ config \ init.php

我的config \ init.php

<?php 


require_once 'config.php';

//Autoloader
function __autoload($class_name){
    require_once 'lib/'.$class_name.'.php';
}
?>

我不太了解这个问题,如果您能解释一下发生这种情况的原因,我将不胜感激。

编辑:它不起作用,因为我已经在使用自动加载器来加载我的文件,在这种情况下,我不应该使用include_once函数

1 个答案:

答案 0 :(得分:0)

我建议使用Composer。但是,如果您不熟悉它,则只需下载zip文件并将其解压缩到您的网站目录中即可。创建一些测试文件,例如test.php或其他内容,然后尝试使用此代码。

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require '<use correct path to PHPMailer here>/src/Exception.php';
require '<use correct path to PHPMailer here>/src/PHPMailer.php';

$body = "Hi! It works!";

$fromEmail = "<from address>"; //fill this
$password="<your secret password to gmail>"; //fill this
$toEmail = "<to address>"; //fill this

$mail = new PHPMailer(true);
try {
    //Server settings
    $mail->SMTPDebug = 2;
    $mail->isSMTP();    
    $mail->Host = 'smtp.gmail.com';  
    $mail->SMTPAuth = true;           
    $mail->Username = $fromEmail;        
    $mail->Password = $password;                
    $mail->SMTPSecure = 'tls';                      
    $mail->Port = 587;                           
    //Recipients
    $mail->setFrom($fromEmail, 'EMAIL FROM PHP TEST CODE');
    $mail->addAddress($toEmail);  

    //Content
    $mail->isHTML(true);                         
    $mail->Subject = 'TEST EMAIL';
    $mail->Body = $body;
    $mail->AltBody = 'IT WORKS!!!';
    $mail->send();
    echo 'mail sent!';
} catch (Exception $e) {
    echo $e;
}

确保您在gmail帐户中进行了必要的设置,以允许PHP发送邮件。没有它,它将无法工作。如果此代码有效,则在此代码之上构建程序。