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>";
You need to define variables before you use them!
To get PHPMailer to throw exceptions, you need to ask it, by passing
true
to the constructor:Now to get responses out of your class, you need to return something, so change the end of your send function to this:
Then when you call your function: