What best practices should be followed when generating and sending activation emails with PHP code?

When generating and sending activation emails with PHP code, it is important to ensure that the email is formatted correctly, contains necessary information for the user to activate their account, and is sent securely to prevent unauthorized access. Additionally, it is recommended to include a unique activation code or link in the email to verify the user's identity.

// Generate unique activation code
$activationCode = md5(uniqid(rand(), true));

// Send activation email
$to = $userEmail;
$subject = 'Activate your account';
$message = 'Please click the following link to activate your account: http://www.example.com/activate.php?code=' . $activationCode;
$headers = 'From: admin@example.com';

mail($to, $subject, $message, $headers);