118 lines
3.9 KiB
PHP
118 lines
3.9 KiB
PHP
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<title>AI Email Generator - Result</title>
|
|
<link rel="stylesheet" href="style.css">
|
|
</head>
|
|
<body>
|
|
<div class="container">
|
|
<?php
|
|
// Load API key from config
|
|
$config = include 'config.php';
|
|
$apikey = $config['openai_api_key'];
|
|
|
|
// Sanitize and validate user inputs
|
|
$emailInput = trim($_POST['email_input'] ?? '');
|
|
$tone = $_POST['tone'] ?? '';
|
|
$language = $_POST['language'] ?? 'English'; // Language selector
|
|
|
|
$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',
|
|
];
|
|
|
|
if (empty($emailInput) || !isset($allowedTones[$tone])) {
|
|
die('Invalid Input.');
|
|
}
|
|
|
|
// Improved Prompt with strict language response
|
|
$prompt = $allowedTones[$tone] . ". Transform this rough text into a polished, professional email in " . $language . ". ONLY reply in " . $language . ":\n\n" . $emailInput;
|
|
|
|
// Prepare API request
|
|
$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,
|
|
];
|
|
|
|
// Make API request
|
|
$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);
|
|
|
|
// Handle response
|
|
$result = json_decode($response, true);
|
|
|
|
if (isset($result['choices'][0]['message']['content'])) {
|
|
$aiEmail = htmlspecialchars($result['choices'][0]['message']['content']);
|
|
echo "<h2>Your AI-Generated Email:</h2>";
|
|
echo "<div class='email-output'>";
|
|
echo "<pre>$aiEmail</pre>";
|
|
echo "<button id='copyBtn'>Copy Email</button>";
|
|
echo "<button id='saveBtn'>Save Email as .txt</button>";
|
|
echo "<button id='darkModeBtn'>Toggle Dark Mode</button>";
|
|
echo "<a href='index.php' class='back-link'>← Generate Another Email</a>";
|
|
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');
|
|
const darkBtn = document.getElementById('darkModeBtn');
|
|
|
|
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;
|
|
};
|
|
}
|
|
|
|
if (darkBtn) {
|
|
darkBtn.onclick = function () {
|
|
document.body.classList.toggle('dark-mode');
|
|
};
|
|
}
|
|
</script>
|
|
</body>
|
|
</html>
|