how to send mail with phpmailer class?

2020-03-30 04:21发布

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:

class mailSend {
    public function sendMail($email, $message, $subject)
    {
        require_once('PHPMailer/src/Exception.php');
        require_once('PHPMailer/src/PHPMailer.php');
        require_once('PHPMailer/src/SMTP.php');
        $mail = new PHPMailer\PHPMailer\PHPMailer();
        try {
        $mail->SMTPDebug = 0;
        $mail->isSMTP();
        $mail->Host = "smtp.gmail.com";
        $mail->SMTPAuth = true;
        $mail->Username = "email@gmail.com";
        $mail->Password = "password";
        $mail->Port = 587;
        $mail->setFrom('admin@example.com', 'Mailer');
        $mail->addAddress($email);
        $mail->addReplyTo("user@gmail.com", "Alias");
        $mail->CharSet = "UTF-8";
        $mail->isHTML(true);
        $mail->Subject = $subject;
        $mail->Body = $message;
        $mail->send();
            $success['success'] = "Mail sent.";
        } catch (Exception $e) {
            $errors['mail'] = "Failed. Mailer error: {$mail->ErrorInfo}";
        }
    }
}

This is the usaqe:

require "modules/mailer.php";
$email_send = new mailSend();
$email_send->sendMail($email,$message,$subject);

And Here is my problem part, I am trying to send a confirmation link to user on registration and dont know how to do it, I tried several methods but couldnt make it work:

Here is how I am trying to send and having error :

Notice: Undefined variable: message in D:\Wamp\www\html\modules\register.php on line 115

require "modules/mailer.php";
$email_send = new mailSend();
$email_send->sendMail($email,$message,$subject);

$subject = "Please verify email!";
$message = "Thanks for signing up!<br>
            Please click on the link below:<br><br>
            <a href=".$url.">".$url."</a>";

1条回答
Juvenile、少年°
2楼-- · 2020-03-30 04:28

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!<br>
            Please click on the link below:<br><br>
            <a href=".$url.">".$url."</a>";
$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'];
}
查看更多
登录 后发表回答