简体   繁体   English

为什么我为$ mail对象获取空值?

[英]Why am i getting null value for the $mail object?

I am using a code to find the individuals that check in late and send emails to them. 我正在使用代码查找迟到并向他们发送电子邮件的人员。 also I find those that haven't come at all and send emails to them too. 我也发现那些根本没有的东西,也向他们发送电子邮件。 However, it is not working. 但是,它不起作用。 I get the names and emails correctly, but the $mail object is null, and I don't understand why. 我可以正确获取名称和电子邮件,但是$ mail对象为null,我不明白为什么。

This is my code: 这是我的代码:

mail_sender.php (this is what i call to send the message) mail_sender.php(这就是我发送消息所需要的)

<?php

function custom_mail($name, $surname, $email, $message, $subject){
    //$mail->SMTPDebug = 3;                               // Enable verbose debug output
        require './PHPMailer-master/PHPMailerAutoload.php';
        $mail = new PHPMailer();
        global $mail;
        var_dump($mail); 
        $mail->isSMTP();                                      // Set mailer to use SMTP
        $mail->Host = 'smtp.gmail.com;';  // Specify main and backup SMTP servers
        $mail->SMTPAuth = true;                               // Enable SMTP authentication
        $mail->Username = '****';                 // SMTP username
        $mail->Password = '****';                           // SMTP password

        $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
        $mail->Port = ****;                                    // TCP port to connect to        
        $mail->From = '****';
        $mail->FromName = '****';

        $mail->addAddress($email, $name." ".$surname);     // Add a recipient
        $mail->addCC('****');
        $mail->isHTML(true);                                  // Set email format to HTML   
        $mail->Subject = $subject;
        $mail->Body    = ucwords($name).' '.ucwords($surname).'! <br />'.$message;
        $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
        //
        if(!$mail->send()) {
                echo 'email -> '.$email.' name -> '.$name.' surname -> '.$surname.'<br />';
                echo 'Mailer Error: ' . $mail->ErrorInfo;
        } 
        else    {                           
                    $mail->ClearAllRecipients();        //clears the list of recipients to avoid other people from getting this email               
        }   
}   

?>

I think this may be your problem: 我认为这可能是您的问题:

$mail = new PHPMailer();
global $mail;
var_dump($mail);

This just doesn't look like a good idea - if you already have a variable called $mail defined globally, it may overwrite your PHPMailer instance, making it null . 这似乎不是一个好主意-如果您已经有一个全局定义的名为$mail的变量,它可能会覆盖您的PHPMailer实例,使其为null Change the order to this: 将顺序更改为此:

global $mail;
$mail = new PHPMailer();
var_dump($mail);

I don't see that there's much reason to make this available globally at all - if you want to re-use the instance across multiple calls, this won't help - you should be declaring it statically to do that, like this: 我看不出有什么理由可以使该全局全局可用-如果您想在多个调用之间重用该实例,这将无济于事-您应该像这样静态声明它:

static $mail;
if (!isset($mail)) {
    $mail = new PHPMailer();
}
var_dump($mail);

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

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