122 lines
4.2 KiB
PHP
122 lines
4.2 KiB
PHP
<?php
|
|
require_once($_SERVER['DOCUMENT_ROOT'] . '/config.php');
|
|
include($_SERVER['DOCUMENT_ROOT'] . '/inc/php/header.php');
|
|
require_once($_SERVER['DOCUMENT_ROOT'] . '/db.php');
|
|
require_once($_SERVER['DOCUMENT_ROOT'] . '/inc/php/auth.php');
|
|
$conn = getConnection();
|
|
|
|
$user_id = $_SESSION['user_id'] ?? null;
|
|
?>
|
|
|
|
<body>
|
|
<div class="container">
|
|
<?php
|
|
// Load API key
|
|
$apikey = getApi();
|
|
|
|
// Sanitize inputs
|
|
$emailInput = trim($_POST['email_input'] ?? '');
|
|
$tone = isset($_POST['tone']) ? htmlspecialchars($_POST['tone']) : '';
|
|
$language = isset($_POST['language']) ? htmlspecialchars($_POST['language']) : 'English';
|
|
|
|
|
|
$allowedTones = [
|
|
'formal' => 'Write this email in a formal tone',
|
|
'casual' => 'Write this email in a casual tone',
|
|
'persuasive' => 'Write this email in a persuasive tone',
|
|
'angry' => 'Write this email in a angry but yet formal tone',
|
|
];
|
|
|
|
if (empty($emailInput) || !isset($allowedTones[$tone])) {
|
|
die('Invalid Input.');
|
|
}
|
|
|
|
// Build prompt
|
|
$prompt = $allowedTones[$tone] . ". Transform this rough text into a polished, professional email in " . $language . ". ONLY reply in " . $language . ":\n\n" . $emailInput;
|
|
|
|
// API call
|
|
$apiurl = 'https://api.openai.com/v1/chat/completions';
|
|
$data = [
|
|
'model' => 'gpt-3.5-turbo',
|
|
'messages' => [
|
|
['role' => 'user', 'content' => $prompt],
|
|
],
|
|
'max_tokens' => 500,
|
|
'temperature' => 0.7,
|
|
];
|
|
|
|
$headers = [
|
|
'Content-Type: application/json',
|
|
'Authorization: Bearer ' . $apikey,
|
|
];
|
|
|
|
$ch = curl_init();
|
|
curl_setopt($ch, CURLOPT_URL, $apiurl);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($ch, CURLOPT_POST, true);
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
|
|
|
$response = curl_exec($ch);
|
|
curl_close($ch);
|
|
|
|
$result = json_decode($response, true);
|
|
|
|
if (isset($result['choices'][0]['message']['content'])) {
|
|
$aiEmail = trim($result['choices'][0]['message']['content']);
|
|
$safeEmail = htmlspecialchars($aiEmail);
|
|
|
|
echo "<h2>Your AI-Generated Email:</h2>";
|
|
echo "<div class='email-output'>";
|
|
echo "<pre>$safeEmail</pre>";
|
|
|
|
// Copy / Save / Navigation
|
|
echo "<button id='copyBtn'>Copy Email</button>";
|
|
echo "<button id='saveBtn'>Save Email as .txt</button>";
|
|
echo "<a href='/home.php'><button>🏠 Back to Home</button></a>";
|
|
|
|
// NEW: Save to DB form
|
|
if ($user_id) {
|
|
echo "<form method='POST' action='/inc/php/save_email.php' style='margin-top:15px;'>";
|
|
echo "<input type='hidden' name='email_body' value=\"" . htmlspecialchars($aiEmail, ENT_QUOTES) . "\">";
|
|
echo "</form>";
|
|
}
|
|
|
|
echo "</div>";
|
|
} else {
|
|
echo "Error: Failed to generate email. Try again later.";
|
|
}
|
|
?>
|
|
<div id="toast">Copied to clipboard ✔️</div>
|
|
</div>
|
|
|
|
<script>
|
|
const copyBtn = document.getElementById('copyBtn');
|
|
const saveBtn = document.getElementById('saveBtn');
|
|
|
|
if (copyBtn) {
|
|
copyBtn.onclick = function () {
|
|
const text = document.querySelector('pre').innerText;
|
|
navigator.clipboard.writeText(text).then(() => {
|
|
const toast = document.getElementById('toast');
|
|
toast.classList.add('show');
|
|
setTimeout(() => toast.classList.remove('show'), 2000);
|
|
});
|
|
return false;
|
|
};
|
|
}
|
|
|
|
if (saveBtn) {
|
|
saveBtn.onclick = function () {
|
|
const text = document.querySelector('pre').innerText;
|
|
const blob = new Blob([text], { type: 'text/plain' });
|
|
const link = document.createElement('a');
|
|
link.href = URL.createObjectURL(blob);
|
|
link.download = 'email.txt';
|
|
link.click();
|
|
return false;
|
|
};
|
|
}
|
|
</script>
|
|
<?php include($_SERVER['DOCUMENT_ROOT'] . '/inc/php/footer.php'); ?>
|