【问题标题】:PHPMailer is not sending mailPHPMailer 不发送邮件
【发布时间】:2018-08-24 02:23:44
【问题描述】:

在数据库中保存数据后发送确认邮件。我用过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 中没有这样的文件或目录

致命错误:require_once():在 C:\wamp64\www\jobLister 中打开所需的 'lib/PHPMailer\PHPMailer\Exception.php' (include_path='.;C:\php\pear') 失败\config\init.php 第 10 行

我的配置\init.php

<?php 


require_once 'config.php';

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

我无法真正理解这个问题,如果您能解释发生这种情况的原因,我将不胜感激。干杯

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

【问题讨论】:

  • 你为什么要写你自己的自动加载器?基本上它需要更聪明,并将完全限定的类名拆分为对应于目录。但实际上,为什么不直接使用 composer?
  • 我正在使用自动加载器,因此我不必为 lib 中的每个文件都使用 require_once。
  • 你收到了吗。问题?出了什么问题
  • 您可以在require_once 之前echo $class_name; 获取在文件中找不到的类名。
  • 对不起,我不知道这对我有什么帮助

标签: php


【解决方案1】:

我会推荐使用 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 发送邮件。没有它,它就行不通。如果此代码有效,则在此基础上构建您的程序。

【讨论】:

    猜你喜欢
    • 2012-09-15
    • 1970-01-01
    • 2013-10-05
    • 2013-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-24
    • 2016-06-06
    相关资源
    最近更新 更多