2015-12-02 38 views
1

我已经写了代码来显示电子邮件形成的Gmail:检索电子邮件使用Laravel和IMAP,未能通过变量来查看

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); 
} 

这工作得很好,但是当我传递变量“$的toAddress”,“$从'来查看它只显示第一个电子邮件ID。我不得不添加视图按钮,在那点击那里要来的是相应的电子邮件的身体进入不同的方法,我通过对$消息该方法,

$this->showMail($message); 

和方法是:

public function showMail($message){ 

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

但点击按钮存在缺失的参数错误。如果有人知道通过这个帮助我!

+0

您有什么问题是什么呢?这与只传送单个电子邮件数据以查看或如何显示电子邮件正文有关吗? –

+0

我必须显示发送电子邮件的人员列表,但点击某个按钮后应该有相应的邮件正文 – Shweta

回答

0

我做了一些改变,它的工作!

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')); 
} 

我储存在我所有的电子邮件在我自己的数据库,然后在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> 
相关问题