Search code examples
emaillaravel-5imap

Retrieving Emails Using Laravel and IMAP,failed to pass variable to view


I have written a code to display emails form gmail:

 public function getMail()
{
    /* connect to gmail */
    $hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
    $username = '[email protected]';
    $password = 'xyz';

    $inbox = imap_open($hostname, $username, $password) or die('Cannot connect: ' . imap_last_error());

    $emails = imap_search($inbox, 'ALL');


    if ($emails) {
        $output = '';

        rsort($emails);

        foreach ($emails as $email_number) {
            $header = imap_headerinfo($inbox, $email_number);

            $from = $header->from[0]->mailbox . "@" . $header->from[0]->host;
            $toaddress = $header->toaddress;
            echo '<strong>To:</strong> ' . $toaddress . '<br>';
            echo '<strong>From:</strong> ' . $from . '<br>';
            $message = quoted_printable_decode(imap_fetchbody($inbox,$email_number,1.1));
            if ($message == '') {
                $message = (imap_fetchbody($inbox, $email_number, 1));
                echo '<strong>Body:</strong> ' . $message . '<br>';
                echo '<br>';
            }
        }
    }
    imap_expunge($inbox);
    imap_close($inbox);
}

This works fine, but when i pass variables '$toaddress', '$from' to view it shows only first email id. And i have to add a view button,upon click of that there should come body of that respective email and goes to different method, i passed para $message to that method,

$this->showMail($message);

and method is:

    public function showMail($message){

    return view('emails.showmail',compact('message'));
}

but on click of button there is missing argument error. If anyone knows help me through this!


Solution

  • I did some changes and it worked!

    public function getMail()
    {
        /* connect to gmail */
        $hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
        $username = '[email protected]';
        $password = 'xyz';
    
        $inbox = imap_open($hostname, $username, $password) or die('Cannot connect: ' . imap_last_error());
    
        $emails = imap_search($inbox, 'ALL');
    
        if ($emails) {
            $output = '';
            $mails = array();
    
            rsort($emails);
    
            foreach ($emails as $email_number) {
                $header = imap_headerinfo($inbox, $email_number);
                $message = quoted_printable_decode (imap_fetchbody($inbox, $email_number, 1));
    
                $from = $header->from[0]->mailbox . "@" . $header->from[0]->host;
                $toaddress = $header->toaddress;
                if(imap_search($inbox, 'UNSEEN')){
                    /*Store from and message body to database*/
                    DB::table('email')->insert(['from'=>$from, 'body'=>$message]);
                    return view('emails.display');
                }
                else{
                    $data = Email::all();
                    return view('emails.display',compact('data'));
    
                }
            }
        }
            imap_close($inbox);
    }
    
    public function showMail($id){
    
        // get the id
        $message = Email::findOrFail($id);
        $m = $message->body;
        // show the view and pass the nerd to it
        return view('emails.showmail',compact('m'));
    }
    

    I stored all my emails in my own database, and then displayed in display.blade.php.

    display.blade.php:

    <div class="page-header">
            <h1>Inbox</h1>
        </div>
        <div class="row">
            <div class="col-sm-4">
                @foreach ( $data as $from )
                        {!! $from->from !!} <br>
                    <a href="/showmail/{{$from ->id}}">View</a>
                    <hr>
                @endforeach
            </div>
        </div>