检索电子邮件使用Laravel和IMAP,无法传递变量来查看 [英] Retrieving Emails Using Laravel and IMAP,failed to pass variable to view
问题描述
我已经写了一个代码来显示gmail的电子邮件:
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);
}
这工作正常,但是当我传递变量'$ toaddress','$从'查看它只显示第一个电子邮件ID。而且我必须添加一个视图按钮,点击这个应该来自相应的电子邮件的身体,并转到不同的方法,我通过了 $ message 到该方法,
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);
方法是:
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!
推荐答案
我做了一些变化,它的工作!
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>
这篇关于检索电子邮件使用Laravel和IMAP,无法传递变量来查看的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!