Skip to content

Instantly share code, notes, and snippets.

@Shaxadhere
Created March 23, 2025 08:28
Show Gist options
  • Save Shaxadhere/ce684f1cdfbe9a90225bcd7bcdf11ecb to your computer and use it in GitHub Desktop.
Save Shaxadhere/ce684f1cdfbe9a90225bcd7bcdf11ecb to your computer and use it in GitHub Desktop.
<?php
// error_reporting(E_ALL);
// ini_set('display_errors', 1);
set_time_limit(30); // Increase timeout to 30 seconds
// Include PHPMailer files manually
// Download these from https://github.com/phpmailer/phpmailer/releases version 6.9.3
require '../assets/phpmailer/src/PHPMailer.php'; // Path to the PHPMailer class
require '../assets/phpmailer/src/Exception.php'; // Path to the Exception class
require '../assets/phpmailer/src/SMTP.php'; // Path to the SMTP class
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
if (isset($_REQUEST['sendEmail'])) {
function sendEmail($recipient, $subject, $message)
{
$mail = new PHPMailer(true); // Set up PHPMailer to use exceptions
try {
// Set mailer to use SMTP
$mail->isSMTP();
$mail->Host = 'your smtp server'; // Your SMTP server (e.g., smtp.gmail.com)
$mail->SMTPAuth = true;
$mail->Username = 'your usernae'; // SMTP username
$mail->Password = 'your password'; // SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // Enable TLS encryption
$mail->Port = 587; // SMTP port for TLS
// Debugging SMTP connection
// $mail->SMTPDebug = 2; // Enables SMTP debug output (1 = errors, 2 = detailed)
// Set sender and recipient
$mail->setFrom('youremail', 'your name'); // Sender's email
$mail->addAddress($recipient); // Recipient's email
// Set email subject and body
$mail->Subject = $subject;
$mail->Body = $message;
// Send email
if ($mail->send()) {
return 'Email sent successfully';
} else {
return 'Mailer Error: ' . $mail->ErrorInfo;
}
} catch (Exception $e) {
return 'Mailer Error: ' . $mail->ErrorInfo;
}
}
// Example usage:
$email = $_REQUEST['email'];
$subject = $_REQUEST['subject'];
$message = $_REQUEST['message'];
$name = $_REQUEST['name'];
$message = "Dear " . $name . ",<br><br>" . $message;
$result = sendEmail($email, $subject, $message);
// echo $result;
echo json_encode(array('status' => 'success', 'message' => $result));
exit;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment