Saya akan membantu membuat script yang mirip dengan owlmailer tetapi menggunakan PHPMailer resmi dari Composer. Berikut adalah versi yang lebih bersih dan menggunakan library PHPMailer resmi:
Struktur File
/your-project/
├── composer.json
├── index.php
└── vendor/ (akan dibuat otomatis oleh composer)
1. composer.json
{
"require": {
"phpmailer/phpmailer": "^6.8"
}
}
Jalankan composer install untuk menginstall PHPMailer.
2. index.php (Script Utama)
<?php
/**
* PHP Mailer Script menggunakan PHPMailer Resmi
* @version : 1.0
*/
session_start();
set_time_limit(0);
ini_set("memory_limit", -1);
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header("Access-Control-Allow-Origin: *");
// Load PHPMailer dari Composer
require 'vendor/autoload.php';
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
$password = "123"; // Password untuk akses
$appInfo = [
'version' => '1.0',
'name' => 'PHP Mailer'
];
// Handle password authentication
$sessioncode = md5(__FILE__);
if (!isset($_SESSION[$sessioncode])) {
$_SESSION[$sessioncode] = '';
}
if (!empty($password) && $_SESSION[$sessioncode] != $password) {
if (isset($_REQUEST['pass']) && $_REQUEST['pass'] == $password) {
$_SESSION[$sessioncode] = $password;
} else {
echo '<pre align=center><form method=post>Password: <input type="password" name="pass"><input type="submit" value=">>"></form></pre>';
exit;
}
}
// Handle send request
if (isset($_POST['action']) && $_POST['action'] == "send") {
$result = processEmailSendingRequest($_POST, $_FILES);
exit($result);
}
/**
* Process email sending request
*/
function processEmailSendingRequest($postData, $files) {
$recipient = cleanInput($postData['recipient'] ?? '');
$smtpAcct = cleanInput($postData['smtpAcct'] ?? '');
$senderName = cleanInput($postData['senderName'] ?? '');
$sendingMethod = cleanInput($postData['sendingMethod'] ?? 'builtin');
$senderEmail = cleanInput($postData['senderEmail'] ?? '');
$replyTo = cleanInput($postData['replyTo'] ?? '');
$messageSubject = processMessageContent($postData['messageSubject'] ?? '', $recipient);
$messageLetter = processMessageContent($postData['messageLetter'] ?? '', $recipient);
$altMessageLetter = processMessageContent($postData['altMessageLetter'] ?? '', $recipient);
$messageType = cleanInput($postData['messageType'] ?? 'html');
$encodingType = cleanInput($postData['encodingType'] ?? 'UTF-8');
$emailPriority = cleanInput($postData['emailPriority'] ?? '');
// Validate email
if (!filter_var($recipient, FILTER_VALIDATE_EMAIL)) {
return "Incorrect Email";
}
try {
$mail = new PHPMailer(true);
// Configure sending method
if ($sendingMethod == "smtp" && !empty($smtpAcct)) {
configureSMTP($mail, $smtpAcct);
}
// Set sender
$mail->setFrom($senderEmail, $senderName);
// Set reply-to
if (!empty($replyTo)) {
$mail->addReplyTo($replyTo);
}
// Add recipient
$mail->addAddress($recipient);
// Set subject
if (!empty($messageSubject)) {
$mail->Subject = $messageSubject;
}
// Set body
if (!empty($messageLetter)) {
$mail->Body = $messageLetter;
}
// Set charset
if (!empty($encodingType)) {
$mail->CharSet = $encodingType;
}
// Set priority
if (!empty($emailPriority)) {
$mail->Priority = (int)$emailPriority;
}
// Set alternative body
if (!empty($altMessageLetter)) {
$mail->AltBody = $altMessageLetter;
}
// Handle attachments
if (isset($files['attachment'])) {
for ($i = 0; $i < count($files['attachment']['name']); $i++) {
if ($files['attachment']['tmp_name'][$i] != "") {
$mail->addAttachment(
$files['attachment']['tmp_name'][$i],
$files['attachment']['name'][$i]
);
}
}
}
// Set message type
$mail->isHTML($messageType == "html");
// Send email
$mail->send();
return "OK";
} catch (Exception $e) {
return $mail->ErrorInfo;
}
}
/**
* Configure SMTP settings
*/
function configureSMTP($mail, $smtpAcct) {
$mail->isSMTP();
$mail->SMTPDebug = SMTP::DEBUG_OFF;
$parts = explode(':', $smtpAcct);
$mail->Host = trim($parts[0] ?? 'localhost');
$mail->Port = (int)trim($parts[1] ?? 25);
// SSL/TLS configuration
$security = strtolower(trim($parts[2] ?? ''));
if ($security == 'ssl') {
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
} elseif ($security == 'tls') {
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
} else {
$mail->SMTPSecure = '';
$mail->SMTPAutoTLS = false;
}
// Authentication
if (isset($parts[3]) && isset($parts[4])) {
$mail->SMTPAuth = true;
$mail->Username = trim($parts[3]);
$mail->Password = trim($parts[4]);
}
}
/**
* Clean and trim input
*/
function cleanInput($string) {
return stripslashes(trim($string));
}
/**
* Process message content with placeholders
*/
function processMessageContent($text, $email) {
$text = stripslashes($text);
$emailuser = preg_replace('/([^@]*).*/', '$1', $email);
$replacements = [
'[-time-]' => date("m/d/Y h:i:s a", time()),
'[-email-]' => $email,
'[-emailuser-]' => $emailuser,
'[-randomletters-]' => generateRandomString('abcdefghijklmnopqrstuvwxyz', 8, 15),
'[-randomstring-]' => generateRandomString('abcdefghijklmnopqrstuvwxyz0123456789', 8, 15),
'[-randomnumber-]' => generateRandomString('0123456789', 7, 15),
'[-randommd5-]' => md5(random_bytes(16)),
];
return str_replace(array_keys($replacements), array_values($replacements), $text);
}
/**
* Generate random string
*/
function generateRandomString($characters, $minLength, $maxLength) {
$length = rand($minLength, $maxLength);
$result = '';
$charLength = strlen($characters);
for ($i = 0; $i < $length; $i++) {
$result .= $characters[random_int(0, $charLength - 1)];
}
return $result;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title><?php echo $appInfo['name'] . ' ' . $appInfo['version']; ?></title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
body { padding: 20px; background-color: #f5f5f5; }
.card { margin-bottom: 20px; }
.progress-log { max-height: 400px; overflow-y: auto; }
.log-entry { padding: 5px 10px; border-bottom: 1px solid #eee; }
.log-entry:last-child { border-bottom: none; }
</style>
</head>
<body>
<div class="container-fluid">
<div class="row">
<div class="col-lg-6">
<div class="card">
<div class="card-header">
<h4>📧 <?php echo $appInfo['name']; ?> <small class="text-muted">v<?php echo $appInfo['version']; ?></small></h4>
</div>
<div class="card-body">
<form id="mailerForm">
<div class="row mb-3">
<div class="col-md-6">
<label class="form-label">Sender Email</label>
<input type="email" class="form-control form-control-sm" id="senderEmail" name="senderEmail">
</div>
<div class="col-md-6">
<label class="form-label">Sender Name</label>
<input type="text" class="form-control form-control-sm" id="senderName" name="senderName">
</div>
</div>
<div class="row mb-3">
<div class="col-md-6">
<label class="form-label">Attachment <small>(Multiple)</small></label>
<input type="file" class="form-control form-control-sm" id="attachment" name="attachment[]" multiple>
</div>
<div class="col-md-6">
<label class="form-label">Reply-to</label>
<input type="text" class="form-control form-control-sm" id="replyTo" name="replyTo">
</div>
</div>
<div class="mb-3">
<label class="form-label">Subject</label>
<input type="text" class="form-control form-control-sm" id="subject" name="subject">
</div>
<div class="row mb-3">
<div class="col-md-6">
<label class="form-label">Message Letter</label>
<textarea class="form-control" id="messageLetter" name="messageLetter" rows="8" placeholder="Message Letter"></textarea>
</div>
<div class="col-md-6">
<label class="form-label">Alternative Message Letter</label>
<textarea class="form-control" id="altMessageLetter" name="altMessageLetter" rows="8" placeholder="Plain text version for non-HTML clients"></textarea>
</div>
</div>
<div class="row mb-3">
<div class="col-md-6">
<label class="form-label">Email List</label>
<textarea class="form-control" id="emailList" name="emailList" rows="6" placeholder="One email per line"></textarea>
</div>
<div class="col-md-6">
<label class="form-label">SMTP Accounts</label>
<textarea class="form-control" id="smtpAccounts" name="smtpAccounts" rows="6" placeholder="Format: HOST:PORT:SSL:Username:Password Example: smtp.gmail.com:587:tls:user@gmail.com:password"></textarea>
</div>
</div>
<div class="row mb-3">
<div class="col-md-3">
<label class="form-label">Message Type</label>
<div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="messageType" id="typeHtml" value="html" checked>
<label class="form-check-label" for="typeHtml">HTML</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="messageType" id="typePlain" value="plain">
<label class="form-check-label" for="typePlain">Plain</label>
</div>
</div>
</div>
<div class="col-md-3">
<label class="form-label">Sending Method</label>
<div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="sendingMethod" id="methodBuiltin" value="builtin" checked>
<label class="form-check-label" for="methodBuiltin">Builtin</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="sendingMethod" id="methodSmtp" value="smtp">
<label class="form-check-label" for="methodSmtp">SMTP</label>
</div>
</div>
</div>
<div class="col-md-3">
<label class="form-label">Encoding</label>
<select class="form-select form-select-sm" id="encoding" name="encoding">
<option value="UTF-8" selected>UTF-8</option>
<option value="ISO-8859-1">ISO-8859-1</option>
<option value="ISO-8859-2">ISO-8859-2</option>
</select>
</div>
<div class="col-md-3">
<label class="form-label">Priority</label>
<select class="form-select form-select-sm" id="priority" name="priority">
<option value="" selected>Default</option>
<option value="1">High</option>
<option value="3">Normal</option>
<option value="5">Low</option>
</select>
</div>
</div>
<div class="d-flex gap-2">
<button type="button" id="btnStart" class="btn btn-primary" onclick="startSending()">Start</button>
<button type="button" id="btnStop" class="btn btn-secondary" onclick="stopSending()" disabled>Stop</button>
</div>