'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 "
Your AI-Generated Email:
";
echo "
";
echo "
$safeEmail
";
// Copy / Save / Navigation
echo "
";
echo "
";
echo "
";
// NEW: Save to DB form
if ($user_id) {
echo "
";
}
echo "
";
} else {
echo "Error: Failed to generate email. Try again later.";
}
?>
Copied to clipboard ✔️