2025-07-10 00:21:08 +02:00
< ? php
2025-08-20 12:58:40 +02:00
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 ;
2025-07-10 00:21:08 +02:00
?>
2025-07-10 00:00:10 +02:00
< body >
< div class = " container " >
< ? php
2025-08-20 12:58:40 +02:00
// Load API key
2025-07-10 00:21:08 +02:00
$apikey = getApi ();
2025-07-10 00:00:10 +02:00
2025-08-20 12:58:40 +02:00
// Sanitize inputs
2025-07-10 00:00:10 +02:00
$emailInput = trim ( $_POST [ 'email_input' ] ? ? '' );
2025-08-20 12:58:40 +02:00
$tone = isset ( $_POST [ 'tone' ]) ? htmlspecialchars ( $_POST [ 'tone' ]) : '' ;
$language = isset ( $_POST [ 'language' ]) ? htmlspecialchars ( $_POST [ 'language' ]) : 'English' ;
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.' );
}
2025-08-20 12:58:40 +02:00
// Build prompt
2025-07-10 00:00:10 +02:00
$prompt = $allowedTones [ $tone ] . " . Transform this rough text into a polished, professional email in " . $language . " . ONLY reply in " . $language . " : \n \n " . $emailInput ;
2025-08-20 12:58:40 +02:00
// API call
2025-07-10 00:00:10 +02:00
$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' ])) {
2025-08-20 12:58:40 +02:00
$aiEmail = trim ( $result [ 'choices' ][ 0 ][ 'message' ][ 'content' ]);
$safeEmail = htmlspecialchars ( $aiEmail );
2025-07-10 00:00:10 +02:00
echo " <h2>Your AI-Generated Email:</h2> " ;
echo " <div class='email-output'> " ;
2025-08-20 12:58:40 +02:00
echo " <pre> $safeEmail </pre> " ;
// Copy / Save / Navigation
2025-07-10 00:00:10 +02:00
echo " <button id='copyBtn'>Copy Email</button> " ;
echo " <button id='saveBtn'>Save Email as .txt</button> " ;
2025-08-20 12:58:40 +02:00
echo " <a href='/home.php'><button>🏠 Back to Home</button></a> " ;
echo " <a href='/inc/php/generate.php' class='back-link'>← Generate Another Email</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 " <input type='text' name='subject' placeholder='Subject (optional)'> " ;
echo " <button type='submit'>💾 Save to My Account</button> " ;
echo " </form> " ;
}
2025-07-10 00:00:10 +02:00
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 >
2025-08-20 12:58:40 +02:00
< ? php include ( $_SERVER [ 'DOCUMENT_ROOT' ] . '/inc/php/footer.php' ); ?>