'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 "
Your AI-Generated Email:
";
echo "
";
echo "
$aiEmail
";
echo "
";
echo "
";
echo "
";
echo "
← Generate Another Email";
echo "
";
} else {
echo "Error: Failed to generate email. Try again later.";
}
?>
Copied to clipboard ✔️