使用 Laravel 和 IMAP 检索电子邮件,无法将变量传递给查看
Retrieving Emails Using Laravel and IMAP,failed to pass variable to view
我写了一个代码来显示来自 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”、“$from”进行查看时,它只显示第一个电子邮件 ID。我必须添加一个查看按钮,单击该按钮后应该会出现相应电子邮件的正文并转到不同的方法,我将 para $message 传递给该方法,
$this->showMail($message);
方法是:
public function showMail($message){
return view('emails.showmail',compact('message'));
}
但是点击按钮时出现缺少参数错误。如果有人知道帮助我解决这个问题!
我做了一些修改,结果成功了!
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'));
}
我把所有的邮件都存储在自己的数据库中,然后显示在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>
我写了一个代码来显示来自 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”、“$from”进行查看时,它只显示第一个电子邮件 ID。我必须添加一个查看按钮,单击该按钮后应该会出现相应电子邮件的正文并转到不同的方法,我将 para $message 传递给该方法,
$this->showMail($message);
方法是:
public function showMail($message){
return view('emails.showmail',compact('message'));
}
但是点击按钮时出现缺少参数错误。如果有人知道帮助我解决这个问题!
我做了一些修改,结果成功了!
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'));
}
我把所有的邮件都存储在自己的数据库中,然后显示在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>