barbushin / php-imap

Manage mailboxes, filter/get/delete emails in PHP (supports IMAP/POP3/NNTP)
MIT License
1.67k stars 460 forks source link

Can I get show originals using IMAP ? #664

Open hiralmmavani opened 2 years ago

hiralmmavani commented 2 years ago

How can I get mail's show original using IMAP ?

Is there any function like For from-name : echo 'from-name: '.(string) ($email->fromName ?? $email->fromAddress)."\n"; something like for Originals of email.

Like in gmail On click on show Original message, they show some information. Same Can I get from IMAP ? Gmail Show Original message Details : Delivered-To: Some Details Received: Some Details X-Received: Some Details ARC-Seal:Some Details ARC-Message-Signature:Some Details ARC-Authentication-Results: Some Details and many more

Can you please let me know is it possible using this demo or not ?

hiralmmavani commented 2 years ago

Hello there, Is there any update ?

Sebbo94BY commented 2 years ago

Hey, I've just checked an example email in Google Mail.

To answer your question: Yes, it is possible.

There is no built-in function in this php-imap library to archive this out of the box as this is in Google Mail also only some "Google" magic or better said, a feature of their mail client.

"Show Original" shows in Google Mail the following information:

Box at the top:

All of the above information are only parsed information from the headers. Google Mail is only showing some keywords from the original email headers.

Box at the bottom:

Here, Google Mail is simply showing the full unformatted email incl. all headers.

So in order to build also such a "Show Original" page like Google Mail has it, you need something like this:

<?php

    /**
     * Example: Build your own "Show Original" feature like in Google Mail.
     *
     * This code only shows an example of how you could get the required values in order to display it on the "show original" page.
     *
     * Please keep in mind, that this is not properly tested - this means, the SPF IP address or DKIM domain could be potentially
     * something wrong. I have not enough test emails to test this. Due to this, the code might also not be the best to handle all possible
     * cases such as "SPF not available".
     *
     * @author Sebastian Krätzig <info@ts3-tools.info>
     */
    declare(strict_types=1);

    require_once __DIR__.'/../vendor/autoload.php';

    use PhpImap\Mailbox;

    $mailbox = new Mailbox(
        '{imap.gmail.com:993/imap/ssl}INBOX', // IMAP server and mailbox folder
        'some@gmail.com', // Username for the before configured mailbox
        '*********', // Password for the before configured username
        __DIR__, // Directory, where attachments will be saved (optional)
        'US-ASCII' // Server encoding (optional)
    );

    $mail_id = $_GET['mailid'];

    $email = $mailbox->getMail(
        $mail_id, // ID of the email, you want to get
        false // Do NOT mark emails as seen (optional)
    );

    /**
     * First Box: Important information as overview
     *
     * This should be formatted as nice HTML.
     *
     * You may want to add additional information or documentation references for your end users.
     */

    echo 'Message ID: '.(string) $email->messageId."\n";
    echo 'Created on: '.(string) $email->date."\n";
    echo 'From: '.(string) $email->fromAddress."\n";
    echo 'To: '.(string) $email->toString."\n";
    echo 'Subject: '.(string) $email->subject."\n";

    preg_match("/spf=([a-z]+)/i", $email->headersRaw, $spf_status);
    preg_match("/designates\W+((?:[0-9]{1,3}\.){3}[0-9]{1,3})\W+as\W+permitted\W+sender/i", $email->headersRaw, $spf_ip_address);
    echo 'SPF: '.(string) strtoupper($spf_status[1])." with IP ".(string) $spf_ip_address[1]."\n";

    preg_match("/dkim=([a-z]+)/i", $email->headersRaw, $dkim_status);
    $dkim_status = $dkim_status[1];
    preg_match("/[dkim=|DKIM\-Signature\:].*[header\.i|d]=@?([a-z\.\-0-9]+)/i", $email->headersRaw, $dkim_domain);

    if (strtolower($dkim_status) == "none") {
        echo 'DKIM: '.(string) strtoupper($dkim_status)."\n";
    } else {
        echo 'DKIM: '.(string) strtoupper($dkim_status)." with domain ".(string) $dkim_domain[1]."\n";
    }

    /**
     * Second Box: Raw email incl. all headers
     *
     * This should be formatted as nice HTML.
     */

    echo (string) $email->headersRaw."\n";

    if ($email->textHtml) {
        /**
         * Ensure, that the HTML is not parsed in the browser - it should be shown as it is.
         * Like an code example on a web site.
         */
        echo $email->textHtml;
    } else {
        echo $email->textPlain;
    }

    $mailbox->disconnect();

I hope this answers your question and helps you to build such a feature.

hiralmmavani commented 2 years ago

Yes, But I need header info in proper formatted output.

Actually currently Get result in single string like : Delivered-To: some details X-Google-Smtp-Source: some details X-Received: some details ARC-Seal: some details

But what I need is : Delivered-To: some details X-Google-Smtp-Source: some details X-Received: some details ARC-Seal: some details

every details in new line. Is there any function in IMAP for this type of output ?

Sebbo94BY commented 2 years ago

As mentioned in my above reply and example code: This is something, what you as developer need to define how it looks like at the end.

The php-imap library is only a helper library to make it easier to work with PHP IMAP functions. It doesn't have any HTML code and thus also doesn't have any HTML rendering functions.

How you can get specific information are shown in my above example code. If you need any other information, you need to extract the required information from the headers.