Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
994 views
in Technique[技术] by (71.8m points)

php - how to send mail with phpmailer class?

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 PHPMailerPHPMailerPHPMailer();
        try {
        $mail->SMTPDebug = 0;
        $mail->isSMTP();
        $mail->Host = "smtp.gmail.com";
        $mail->SMTPAuth = true;
        $mail->Username = "[email protected]";
        $mail->Password = "password";
        $mail->Port = 587;
        $mail->setFrom('[email protected]', 'Mailer');
        $mail->addAddress($email);
        $mail->addReplyTo("[email protected]", "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:Wampwwwhtmlmodules egister.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>";
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 PHPMailerPHPMailerPHPMailer(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'];
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...