]> jfr.im git - irc/unrealircd/unrealircd-webpanel.git/blame - plugins/php_mailer/php_mailer.php
Add email support
[irc/unrealircd/unrealircd-webpanel.git] / plugins / php_mailer / php_mailer.php
CommitLineData
c44f6efa
VP
1<?php
2
3use PHPMailer\PHPMailer\PHPMailer;
4use PHPMailer\PHPMailer\SMTP;
5
6class php_mailer
7{
8 public $name = "PHPMailer()";
9 public $author = "Valware";
10 public $version = "1.0";
11 public $description = "Send mail using PHPMailer()";
12
13 function __construct()
14 {
15 Hook::func(HOOKTYPE_USER_LOGIN, 'php_mailer::user_login_notif');
16 Hook::func(HOOKTYPE_USER_LOGIN_FAIL, 'php_mailer::user_login_fail_notif');
17 }
18
19 public static function send_mail($to, $subject, $body)
20 {
21 $mail = new PHPMailer(true);
22 try {
23 //Server settings
24 //$mail->SMTPDebug = 2;
25 $mail->isSMTP(); //Send using SMTP
26 $mail->Host = EMAIL_SETTINGS['host']; //Set the SMTP server to send through
27 $mail->SMTPAuth = true; //Enable SMTP authentication
28 $mail->Username = EMAIL_SETTINGS['username']; //SMTP username
29 $mail->Password = EMAIL_SETTINGS['password']; //SMTP password
30 $mail->SMTPSecure = EMAIL_SETTINGS['encryption']; //Enable implicit TLS encryption
31 $mail->Port = EMAIL_SETTINGS['port']; //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`
32
33 //Recipients
34 $mail->setFrom(EMAIL_SETTINGS['username'], EMAIL_SETTINGS['from_name']);
35 $mail->addAddress($to['email'], $to['name']); //Add a recipient
36
37 //Content
38 $mail->isHTML(true); //Set email format to HTML
39 $mail->Subject = $subject;
40 $mail->Body = $body . "<br><br>Thank you for using UnrealIRCd!";
41 $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
42
43 $mail->send();
44 echo 'Message has been sent';
45 } catch (Exception $e) {
46 echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
47 }
48 }
49
50 /**
51 * Send a login notification to the admin (note-to-self)
52 * @param mixed $user
53 * @return void
54 */
55 public static function user_login_notif($user)
56 {
57 self::send_mail(
58 ["email" => EMAIL_SETTINGS['username'], "name" => EMAIL_SETTINGS['from_name']],
59 "New login to Unreal Admin Panel",
60 "There was a new login to the admin panel.<br>User: \"$user->username\"<br>IP: \"".$_SERVER['REMOTE_ADDR']."\""
61 );
62 }
63 public static function user_login_fail_notif($fail)
64 {
65 self::send_mail(
66 ["email" => EMAIL_SETTINGS['username'], "name" => EMAIL_SETTINGS['from_name']],
67 "Failed login attempt - Unreal Admin Panel",
68 "There was a failed login attempt to the admin panel.<br>User: \"".$fail['login']."\"<br>IP: \"".$fail['IP']."\""
69 );
70 }
71}