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

吃可爱长大的小学妹 提交于 2019-12-19 04:38:09

问题


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 = 'xyz@gmail.com';
    $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!


回答1:


I did some changes and it worked!

public function getMail()
{
    /* connect to gmail */
    $hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
    $username = 'xyz@gmail.com';
    $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>


来源:https://stackoverflow.com/questions/34041340/retrieving-emails-using-laravel-and-imap-failed-to-pass-variable-to-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!