有没有办法通过laravel从Gmail帐户恢复电子邮件?
我想创建一个收件箱邮件,但是直接来自gmail或outlook。
感谢您的帮助。
答案 0 :(得分:0)
大卫,
下面是我用来阅读电子邮件的一些代码。我相信这可以帮助你:
/**
* Get last UID on Emails table
*
* return integer
*/
private function getLastUID()
{
// I have a model Email and I try to get the higher uid on database
return Email::max('uid');
}
/**
* Open Imap instance
*
* @return resource
*/
private function startEmail()
{
return imap_open(env('IMAP'), env('IMAP_EMAIL'), env('IMAP_PASSWORD'), OP_READONLY);
}
/**
* Get Emails from Imap instance
*
* @return array
*/
private function getTodayEmails()
{
$mailbox = $this->startEmail();
$today = Carbon::now()->format('j-M-Y');
//I only search for todays emails, since I have a cron job that runs this task every hour (For my purpose I don't need to check it every minute)
$inbox = imap_search($mailbox,'SINCE '.$today);
/* If there is no email */
if ($inbox === false) return false;
//Sort to insert the new email first
rsort($inbox);
$emails = [];
foreach($inbox as $box) {
/* get information specific to this email */
$overview = imap_fetch_overview($mailbox, $box, 0);
$header = imap_headerinfo($mailbox , $box);
$uid = imap_uid($mailbox , $box);
// Here I check if the email $uid is already on my database, if no, I save it. If yes I break the conditional.
// I highly believe that you have to work on this conditional and in your Model. The rest is working well (at least for me! :) )
if ($uid > $this->getLastUID()) {
$emails[$box]['uid'] = $uid;
$emails[$box]['date'] = (isset($header->udate)) ? date('Y-m-d H:i:s', $header->udate) : null;
$emails[$box]['subject'] = (isset($overview[0]->subject)) ? $overview[0]->subject : null;
$emails[$box]['from'] = (isset($header->from[0])) ? $this->extractEmail($header->from[0]) : null;
$emails[$box]['from_name'] = (isset($header->from[0]->personal)) ? $header->from[0]->personal : null;
$emails[$box]['to'] = (isset($header->to[0])) ? $this->extractEmail($header->to[0]) : null;
$emails[$box]['to_name'] = (isset($header->to[0]->personal)) ? $header->to[0]->personal : null;
$emails[$box]['reply_to'] = (isset($header->reply_to[0])) ? $this->extractEmail($header->reply_to[0]) : null;
$emails[$box]['reply_name'] = (isset($header->reply_to[0]->personal)) ? $header->reply_to[0]->personal : null;
/* output the email body */
$emails[$box]['message'] = $this->getBody($uid, $mailbox);
} else {
break;
}
imap_close($mailbox);
return $emails;
}
/**
* Extract email from Imap Instance
*
* @param object $email
*
* @return bool|string
*/
private function extractEmail($email)
{
if (isset($email->mailbox) && isset($email->host))
return $email->mailbox.'@'.$email->host;
return false;
}
/**
* Get body message
*
* @param integer $uid
* @param Imap Instance $imap
*
* @return bool
*/
private function getBody($uid, $imap)
{
$body = $this->getPart($imap, $uid, "TEXT/HTML");
// if HTML body is empty, try getting text body
if ($body == "") {
$body = $this->getPart($imap, $uid, "TEXT/PLAIN");
}
return $body;
}
/**
* Treat body message of email
*
* @param Imap Instance $imap
* @param integer $uid
* @param string $mimetype
* @param bool $structure
* @param bool $partNumber
*
* @return bool|string
*/
private function getPart($imap, $uid, $mimetype, $structure = false, $partNumber = false)
{
if (!$structure) {
$structure = imap_fetchstructure($imap, $uid, FT_UID);
}
if ($structure) {
if ($mimetype == $this->getMimeType($structure)) {
if (!$partNumber) {
$partNumber = 1;
}
$text = imap_fetchbody($imap, $uid, $partNumber, FT_UID);
switch ($structure->encoding) {
case 3: return imap_base64($text);
case 4: return imap_qprint($text);
default: return $text;
}
}
// multipart
if ($structure->type == 1) {
foreach ($structure->parts as $index => $subStruct) {
$prefix = "";
if ($partNumber) {
$prefix = $partNumber . ".";
}
$data = $this->getPart($imap, $uid, $mimetype, $subStruct, $prefix . ($index + 1));
if ($data) {
return $data;
}
}
}
}
return false;
}
/**
* Get Mimetype of part
*
* @param $structure
*
* @return string
*/
private function getMimeType($structure)
{
$primaryMimetype = array("TEXT", "MULTIPART", "MESSAGE", "APPLICATION", "AUDIO", "IMAGE", "VIDEO", "OTHER");
if ($structure->subtype) {
return $primaryMimetype[(int)$structure->type] . "/" . $structure->subtype;
}
return "TEXT/PLAIN";
}
在.env上你必须插入:
IMAP={imap.gmail.com:993/ssl/novalidate-cert}INBOX
(or you can use {imap.gmail.com:993/imap/ssl}INBOX)
IMAP_EMAIL=<Your GMAIL>
IMAP_PASSWORD=<PASSWORD>
虽然您可以使用某些库,例如https://github.com/barbushin/php-imap。我真的相信用原始的php(http://php.net/manual/en/book.imap.php)很容易直接进行。
您可以获得更多信息:https://davidwalsh.name/gmail-php-imap(其中大部分功能来自btw)
答案 1 :(得分:0)
您可以使用GMAIL API https://developers.google.com/gmail/api/quickstart/php
您可以使用messages.list和threads.list方法https://developers.google.com/gmail/api/guides/filtering
搜索或过滤文件GET https://www.googleapis.com/gmail/v1/users/me/messages?q="in:sent after:2014/01/01 before:2014/01/30"