How can you retrieve the actual email address of the sender using IMAP in PHP?
When using IMAP in PHP to retrieve emails, the "From" header often contains the sender's name and email address in a format like "Sender Name <sender@example.com>". To extract just the email address, you can use PHP's built-in functions like `imap_headerinfo()` to get the header information and then parse out the email address using regular expressions or string manipulation.
<?php
// Connect to the IMAP server
$mailbox = imap_open('{mail.example.com:993/ssl}INBOX', 'username', 'password');
// Get the email headers
$headers = imap_headerinfo($mailbox, $email_number);
// Extract the email address from the "From" header
$sender_email = preg_match('/[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/', $headers->fromaddress, $matches);
// Output the sender's email address
echo $matches[0];
// Close the connection
imap_close($mailbox);
?>