php-imap icon indicating copy to clipboard operation
php-imap copied to clipboard

From email address not shown

Open luke52 opened this issue 2 years ago • 3 comments

Hi all, I'm facing an issue and I'm not sure if I'm missing something. Based on the Getting started example, I created a script, what should also output the sender's email address (from). However, the from output is empty and no from email address is outputted, while all the other information (subject line) is shown, so I assume that the connection and login is working. In the code you see what I already tried and what the result was. Any ideas?

I use the following code

        $cm = new ClientManager($options = []);

        $client = $cm->make([
            'host'          => '*****',
            'port'          => 993,
            'encryption'    => 'ssl',
            'validate_cert' => true,
            'username'      => '******',
            'password'      => '*****',
            'protocol'      => 'imap'
        ]);
        
        //Connect to the IMAP Server
        $client->connect();
        
        //Get all Mailboxes
        /** @var \Webklex\PHPIMAP\Support\FolderCollection $folders */
        $folders = $client->getFolders();
        
        //Loop through every Mailbox
        /** @var \Webklex\PHPIMAP\Folder $folder */
        foreach($folders as $folder){
        
            //Get all Messages of the current Mailbox $folder
            /** @var \Webklex\PHPIMAP\Support\MessageCollection $messages */
            $messages = $folder->messages()->all()->get();
        
            /** @var \Webklex\PHPIMAP\Message $message */
            foreach($messages as $message){
                //echo $message->getFrom().'<br />'; //Outputs Sender name only
                //echo $message->getFrom()->mail.'<br />'; // Outputs Error Undefined property: Webklex\PHPIMAP\Attribute::$mail
                //echo $message->getFrom()[0]->mail.'<br />'; // Outputs Error Undefined array key 0
                //echo $message->from()[0]->mail.'<br />'; // Outputs Error Undefined array key 0
                //echo $message->from().'<br />'; // Outputs Error Method Webklex\PHPIMAP\Message::from() is not supported
                //echo $message->from[0]->mail.'<br />'; // Outputs Error Undefined array key 0
                echo $message->getSubject().'<br />';
                echo "<hr>";
            }
        }

luke52 avatar Mar 01 '23 08:03 luke52

Hi @luke52 , thanks for your report. Please share some additional background information such as:

  • OS: [e.g. Debian 10]
  • PHP: [e.g. 5.5.9]
  • Version [e.g. v2.3.1]
  • Provider [e.g. Gmail, Outlook, Dovecot]

Please provide an anonymised version of the troubling email if this issue exists in >= v5.x, in order to be able to create a test case for it. Or if you already have a test case, feel free to share it :)

Best regards & happy coding,

Webklex avatar Mar 01 '23 20:03 Webklex

@luke52, @Webklex In my case, the problematic header was: From: [email protected] (Mail Delivery System) From values were also unavailable. I traced the problem to Webklex\Header class, decodeAddresses method.

  1. The method checked if php extension mailparse is installed & config has $this->config["rfc822"], if yes: it parses the email
  2. If not, it uses RegExp's to parse the address, which failes to parse it I solved the issue, by installing mailparse extension (apt-get install php8.2-mailparse on debian)

michalkkkd avatar Apr 22 '23 17:04 michalkkkd

Hi @michalkkkd, many thanks for your follow up. I went ahead and created a new test case. While doing so, I've noticed that the from header value [email protected] (Mail Delivery System) is malformed. It should be Mail Delivery System <[email protected]> - However, this isn't an issue in the latest release, but might has been one in a previous version.

If the issue persists even with the latest release, please share an anonymized message or even better a test case in order to have something to replicate the behavior.

Once again, thanks for taking the time and effort to make this library better! If you have any more questions or need further assistance, feel free to let me know.

Best regards and happy coding,

Webklex avatar Jun 23 '23 22:06 Webklex