简体   繁体   English

PHPMailer没有发送邮件

[英]PHPMailer is not sending mail

Tring to send a confirmation mail after from saving data in the database. 将数据保存到数据库后,尝试发送确认邮件。 I have used PHPMailer. 我用过PHPMailer。

My Code 我的密码

<?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');

But I am getting errors like this 但是我收到这样的错误

Warning: require_once(lib/PHPMailer\\PHPMailer\\Exception.php): failed to open stream: No such file or directory in C:\\wamp64\\www\\jobLister\\config\\init.php on line 10 警告:require_once(lib / PHPMailer \\ PHPMailer \\ Exception.php):无法打开流:第10行的C:\\ wamp64 \\ www \\ jobLister \\ config \\ init.php中没有此类文件或目录

Fatal error: require_once(): Failed opening required 'lib/PHPMailer\\PHPMailer\\Exception.php' (include_path='.;C:\\php\\pear') in C:\\wamp64\\www\\jobLister\\config\\init.php on line 10 致命错误:require_once():无法在C:\\ wamp64 \\ www \\ jobLister \\ config \\ init.php中打开所需的'lib / PHPMailer \\ PHPMailer \\ Exception.php'(include_path ='.; C:\\ php \\ pear')在第10行

My config\\init.php 我的config \\ init.php

<?php 


require_once 'config.php';

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

I can't really understand the problem, I would appreciate if you could explain the reason why this is happening.Cheers 我真的无法理解这个问题,如果您能解释为什么会发生,我将不胜感激。

Edit: Its not working because I am already using an autoloader to load my files and I should not be using include_once function in this case 编辑:它不起作用,因为我已经在使用自动加载器来加载我的文件,并且在这种情况下不应该使用include_once函数

I would recommend using Composer. 我建议使用Composer。 But if you are not familiar with it, then just download the zip file and extract it in your website's directory. 但是,如果您不熟悉它,则只需下载zip文件并将其解压缩到您网站的目录中即可。 Create some test file like test.php or something and try this code. 创建一些测试文件,例如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;
}

Make sure you made the necessary setting in your gmail account to allow PHP to send mails. 确保您在gmail帐户中进行了必要的设置,以允许PHP发送邮件。 Without that, it won't work. 没有它,它将无法工作。 If this code works, then build your program on top of this. 如果此代码有效,则在此基础上构建程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM