Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Services;
- use PHPMailer\PHPMailer\Exception;
- use PHPMailer\PHPMailer\PHPMailer;
- /**
- * Email Service
- *
- * Handles sending emails using PHPMailer
- */
- class EmailService
- {
- /**
- * SMTP Configuration
- */
- private $host;
- private $port;
- private $username;
- private $password;
- private $encryption;
- private $fromEmail;
- private $fromName;
- /**
- * Debug mode
- * @var bool
- */
- private $debug;
- /**
- * Initialize email service
- */
- public function __construct()
- {
- // Load configuration
- $this->host = MAIL_HOST;
- $this->port = MAIL_PORT;
- $this->username = MAIL_USERNAME;
- $this->password = MAIL_PASSWORD;
- $this->encryption = MAIL_ENCRYPTION;
- $this->fromEmail = MAIL_FROM_ADDRESS;
- $this->fromName = MAIL_FROM_NAME;
- // Set debug mode based on config
- $this->debug = defined('MAIL_DEBUG') ? MAIL_DEBUG : APP_DEBUG;
- }
- /**
- * Send an email
- *
- * @param string $to
- * @param string $subject
- * @param string $body
- * @param bool $isHtml
- * @return bool
- */
- public function send($to, $subject, $body, $isHtml = true)
- {
- // Create a new PHPMailer instance
- $mail = new PHPMailer();
- try {
- // Server settings
- $mail->isSMTP();
- $mail->Host = $this->host;
- $mail->SMTPAuth = true;
- $mail->Username = $this->username;
- $mail->Password = $this->password;
- $mail->SMTPSecure = $this->encryption;
- $mail->Port = $this->port;
- $mail->SMTPDebug = $this->debug;
- // Recipients
- $mail->setFrom($this->fromEmail, $this->fromName);
- $mail->addAddress($to);
- // Content
- $mail->isHTML($isHtml);
- $mail->Subject = $subject;
- $mail->Body = $body;
- // Send the email
- $response = $mail->send();
- if ($this->debug) {
- $this->logEmail($to, $subject, $body, $response);
- }
- return true;
- } catch (\Exception $e) {
- // Log the error
- error_log('Email sending failed: ' . $mail->ErrorInfo);
- return false;
- }
- }
- /**
- * Send a password reset email
- *
- * @param string $to
- * @param string $resetLink
- * @return bool
- */
- public function sendPasswordReset($to, $resetLink)
- {
- $subject = APP_NAME . ' - Password Reset Request';
- $body = '
- <html>
- <head>
- <title>Password Reset</title>
- </head>
- <body>
- <div style="max-width: 600px; margin: 0 auto; padding: 20px; font-family: Arial, sans-serif;">
- <h2 style="color: #3498db;">Password Reset Request</h2>
- <p>You recently requested to reset your password for your ' . APP_NAME . ' account. Click the button below to reset it.</p>
- <p style="margin: 30px 0;">
- <a href="' . $resetLink . '" style="background-color: #3498db; color: #ffffff; text-decoration: none; padding: 10px 20px; border-radius: 5px; display: inline-block;">Reset Your Password</a>
- </p>
- <p>If you did not request a password reset, please ignore this email or contact support if you have questions.</p>
- <p>This password reset link is only valid for one hour.</p>
- <hr style="border: 1px solid #f2f2f2; margin: 20px 0;">
- <p style="font-size: 12px; color: #777777;">If you\'re having trouble clicking the password reset button, copy and paste the URL below into your web browser:</p>
- <p style="font-size: 12px; color: #777777;">' . $resetLink . '</p>
- </div>
- </body>
- </html>';
- return $this->send($to, $subject, $body);
- }
- /**
- * Log email details instead of sending
- *
- * @param string $to
- * @param string $subject
- * @param string $body
- */
- private function logEmail($to, $subject, $body, $response)
- {
- $logDir = BASE_PATH . '/logs';
- // Create logs directory if it doesn't exist
- if (!is_dir($logDir)) {
- mkdir($logDir, 0755, true);
- }
- $logFile = $logDir . '/email_debug.log';
- // Format the log entry
- $logEntry = "==========================================================\n";
- $logEntry .= "Date: " . date('Y-m-d H:i:s') . "\n";
- $logEntry .= "To: $to\n";
- $logEntry .= "Subject: $subject\n";
- $logEntry .= "Body:\n$body\n\n";
- $logEntry .= "Response Server:\n$response\n\n";
- // Write to log file
- file_put_contents($logFile, $logEntry, FILE_APPEND);
- // Also output to error log for easy debugging
- error_log("DEBUG MODE - Email would be sent to: $to with subject: $subject");
- }
- /**
- * Send an account verification email
- *
- * @param string $to
- * @param string $verificationLink
- * @return bool
- */
- public function sendAccountVerification($to, $verificationLink)
- {
- $subject = APP_NAME . ' - Verify Your Email Address';
- $body = '
- <html>
- <head>
- <title>Verify Your Email</title>
- </head>
- <body>
- <div style="max-width: 600px; margin: 0 auto; padding: 20px; font-family: Arial, sans-serif;">
- <h2 style="color: #3498db;">Verify Your Email Address</h2>
- <p>Thank you for registering with ' . APP_NAME . '. Please click the button below to verify your email address.</p>
- <p style="margin: 30px 0;">
- <a href="' . $verificationLink . '" style="background-color: #3498db; color: #ffffff; text-decoration: none; padding: 10px 20px; border-radius: 5px; display: inline-block;">Verify Email Address</a>
- </p>
- <p>If you did not create an account, no further action is required.</p>
- <p>This verification link is valid for 48 hours.</p>
- <hr style="border: 1px solid #f2f2f2; margin: 20px 0;">
- <p style="font-size: 12px; color: #777777;">If you\'re having trouble clicking the verification button, copy and paste the URL below into your web browser:</p>
- <p style="font-size: 12px; color: #777777;">' . $verificationLink . '</p>
- </div>
- </body>
- </html>';
- return $this->send($to, $subject, $body);
- }
- /**
- * Send a notification about suspicious login
- *
- * @param string $to User's email
- * @param array $loginDetails Details about the suspicious login
- * @return bool
- */
- public function sendSuspiciousLoginAlert($to, $loginDetails)
- {
- $subject = APP_NAME . ' - Suspicious Login Detected';
- $ipAddress = $loginDetails['ip_address'] ?? 'Unknown';
- $location = $loginDetails['location'] ?? 'Unknown';
- $browser = $loginDetails['browser'] ?? 'Unknown';
- $os = $loginDetails['os'] ?? 'Unknown';
- $deviceType = $loginDetails['device_type'] ?? 'Unknown';
- $time = $loginDetails['created_at'] ?? 'Unknown';
- $body = '
- <html>
- <head>
- <title>Suspicious Login Alert</title>
- </head>
- <body>
- <div style="max-width: 600px; margin: 0 auto; padding: 20px; font-family: Arial, sans-serif;">
- <h2 style="color: #d9534f;">Suspicious Login Detected</h2>
- <p>We detected a login to your ' . APP_NAME . ' account from a new location or device.</p>
- <div style="background-color: #f8f9fa; border-left: 4px solid #d9534f; padding: 15px; margin: 20px 0;">
- <h3 style="margin-top: 0;">Login Details:</h3>
- <p><strong>Time:</strong> ' . $time . '</p>
- <p><strong>IP Address:</strong> ' . $ipAddress . '</p>
- <p><strong>Location:</strong> ' . $location . '</p>
- <p><strong>Browser:</strong> ' . $browser . '</p>
- <p><strong>Operating System:</strong> ' . $os . '</p>
- <p><strong>Device:</strong> ' . $deviceType . '</p>
- </div>
- <p>If this was you, you can ignore this message.</p>
- <p>If you did not log in at this time, please change your password immediately and enable two-factor authentication for additional security.</p>
- <p style="margin: 30px 0;">
- <a href="' . APP_URL . '/account/security" style="background-color: #d9534f; color: #ffffff; text-decoration: none; padding: 10px 20px; border-radius: 5px; display: inline-block;">Review Account Security</a>
- </p>
- <p>If you need assistance, please contact our support team.</p>
- </div>
- </body>
- </html>';
- return $this->send($to, $subject, $body);
- }
- /**
- * Set debug mode
- *
- * @param bool $debug
- */
- public function setDebug($debug)
- {
- $this->debug = (bool)$debug;
- }
- /**
- * Get current debug status
- *
- * @return bool
- */
- public function isDebug()
- {
- return $this->debug;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement