2025-07-10 00:21:08 +02:00
< ? php
require_once ( $_SERVER [ 'DOCUMENT_ROOT' ] . '/config.php' );
include ( $_SERVER [ 'DOCUMENT_ROOT' ] . '/inc/php/header.php' );
?>
2025-07-10 00:00:10 +02:00
< body >
< div class = " container " >
< ? php
// Load API key from config
2025-07-10 00:21:08 +02:00
$apikey = getApi ();
2025-07-10 00:00:10 +02:00
// Sanitize and validate user inputs
$emailInput = trim ( $_POST [ 'email_input' ] ? ? '' );
2025-07-10 00:21:08 +02:00
$tone = htmlspecialchars ( $_POST [ 'tone' ]) ? ? '' ;
$language = htmlspecialchars ( $_POST [ 'language' ]) ? ? 'English' ; // Language selector
2025-07-10 00:00:10 +02:00
$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 ;
};
}
</ script >
2025-07-10 00:21:08 +02:00
< ? php include ( $_SERVER [ 'DOCUMENT_ROOT' ] . '/inc/php/footer.php' );