how to send mail with phpmailer class?

前端 未结 1 1571
逝去的感伤
逝去的感伤 2021-01-16 06:37

I created a phpMailer class and call it into my register.php file all fine. But dont find a way how to send emails from class.

Here is my class:

clas         


        
1条回答
  •  时光说笑
    2021-01-16 07:15

    You need to define variables before you use them!

    require "modules/mailer.php";
    $email_send = new mailSend();
    
    $subject = "Please verify email!";
    $message = "Thanks for signing up!
    Please click on the link below:

    ".$url.""; $email_send->sendMail($email,$message,$subject);

    To get PHPMailer to throw exceptions, you need to ask it, by passing true to the constructor:

    $mail = new PHPMailer\PHPMailer\PHPMailer(true);
    

    Now to get responses out of your class, you need to return something, so change the end of your send function to this:

    $result = [];
    $mail->send();
        $result['success'] = true;
        $result['message'] = "Mail sent.";
    } catch (Exception $e) {
        $result['success'] = false;
        $result['message'] = "Failed. Mailer error: {$mail->ErrorInfo}";
    }
    return $result;
    

    Then when you call your function:

    $result = $email_send->sendMail($email,$message,$subject);
    if ($result['success']) {
        echo $result['message'];
        //Do whatever else you want to do on success
    } else {
        echo $result['message'];
    }
    

    0 讨论(0)
提交回复
热议问题