Ejemplo n.º 1
0
 public function getAttachment($id)
 {
     $attachment = EmailAttachment::findOrFail($id);
     return redirect()->to(url($attachment->file_path));
 }
Ejemplo n.º 2
0
 public function processEmail($message)
 {
     if (Email::where('gmail_uid', $message->id)->count() == 0) {
         $mail = new Email();
         $service = Mailbox::getGmailService();
         $user = '******';
         try {
             $message_data = $service->users_messages->get($user, $message->id);
         } catch (Exception $e) {
             print 'An error occurred: ' . $e->getMessage();
         }
         foreach ($message_data->getPayload()->headers as $header) {
             if ($header['name'] == 'Subject') {
                 $mail->subject = $header['value'];
             }
             if ($header['name'] == 'Cc') {
                 $mail->cc = $header['value'];
             }
             if ($header['name'] == 'From') {
                 $from_pos = strpos($header['value'], "<");
                 $from_name = substr($header['value'], 0, $from_pos);
                 $from_address = substr($header['value'], $from_pos + 1, -1);
                 $mail->sender_name = $from_name;
                 $mail->sender_address = $from_address;
             }
             if ($header['name'] == 'Date') {
                 $mail->received_at = Carbon::createFromFormat('D, d M Y H:i:s T', $header['value']);
             }
         }
         $mail->mailbox_id = $this->id;
         $attachments = new Collection();
         if ($message_data->getPayload()->body['size'] == 0) {
             foreach ($message_data->getPayload()->parts as $part) {
                 if (count($part->parts) > 0) {
                     foreach ($part->parts as $subpart) {
                         if ($subpart->mimeType == 'text/html') {
                             $mail->mimetype = 'text/html';
                             $mail->content = base64_decode(strtr($subpart->getBody()->data, '-_,', '+/='));
                         }
                     }
                 }
                 if ($part->mimeType == 'text/html') {
                     $mail->mimetype = 'text/html';
                     $mail->content = base64_decode(strtr($part->getBody()->data, '-_,', '+/='));
                 }
                 if ($part['filename'] != '') {
                     $attachment_data = $service->users_messages_attachments->get("me", $message->id, $part->getBody()->attachmentId);
                     $file_raw_data = base64_decode(strtr($attachment_data->data, array('-' => '+', '_' => '/')));
                     $stored_filename = $message->id . '-' . time() . '-' . $part['filename'];
                     $fh = fopen(public_path() . "/email_attachments/" . $stored_filename, "w+");
                     fwrite($fh, $file_raw_data);
                     fclose($fh);
                     $attachment = new EmailAttachment();
                     $attachment->file_path = "email_attachments/" . $stored_filename;
                     $attachment->file_name = $part['filename'];
                     $attachment->save();
                     $attachments->push($attachment);
                 }
             }
             if (!$mail->content) {
                 foreach ($message_data->getPayload()->parts as $part) {
                     if ($part->mimeType == 'text/plain') {
                         $mail->mimetype = 'text/plain';
                         $mail->content = base64_decode(strtr($part->getBody()->data, '-_,', '+/='));
                     }
                 }
             }
         } else {
             $mail->mimetype = 'text/plain';
             $mail->content = base64_decode(strtr($message_data->getPayload()->body['data'], '-_,', '+/='));
         }
         $mail->gmail_uid = $message->id;
         $mail->save();
         if ($attachments->count() > 0) {
             foreach ($attachments as $a) {
                 $a->email_id = $mail->id;
                 $a->save();
             }
         }
         $mail->matchToPatient();
         $mail->addProcessedLabel();
     }
 }