What role does urlencode function play in resolving the issue of %20 appearing in email links?

When constructing email links in PHP, spaces in the URL can appear as "%20" which can make the link look messy and unprofessional. To resolve this issue, we can use the urlencode function to encode the URL and replace spaces with the proper "%20" encoding. This ensures that the email link appears correctly in the email client.

$email = 'example@example.com';
$subject = 'Hello';
$body = 'Check out this link: www.example.com/page with spaces';

$emailLink = 'mailto:' . $email . '?subject=' . urlencode($subject) . '&body=' . urlencode($body);

echo '<a href="' . $emailLink . '">Send Email</a>';