如何通过laravel接收电子邮件?

如何通过laravel接收电子邮件?,laravel,email,laravel-4,Laravel,Email,Laravel 4,有没有办法通过laravel从gmail帐户中恢复电子邮件 我想创建一个收件箱邮件,但直接从gmail或outlook帐户 谢谢你的帮助。大卫 下面是一些我用来阅读电子邮件的代码。我相信这可以帮助您: /** * Get last UID on Emails table * * return integer */ private function getLastUID() { // I have a model Email and I try to get the higher

有没有办法通过laravel从gmail帐户中恢复电子邮件

我想创建一个收件箱邮件,但直接从gmail或outlook帐户

谢谢你的帮助。

大卫

下面是一些我用来阅读电子邮件的代码。我相信这可以帮助您:

/**
 * 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>
IMAP={IMAP.gmail.com:993/ssl/novalidate cert}收件箱
(或者您可以使用{imap.gmail.com:993/imap/ssl}收件箱)
IMAP_电子邮件=
IMAP_密码=
尽管您可以使用一些库,例如。我真的相信直接使用原始php()是很容易的

您可以获得更多信息:(顺便说一句,大多数函数来自哪里)

您可以使用GMAIL API

您可以使用messages.list和threads.list方法搜索或筛选文件

GET https://www.googleapis.com/gmail/v1/users/me/messages?q="in:sent after:2014/01/01 before:2014/01/30"