public function addProcessedLabel() { $labels = array(); $service = Mailbox::getGmailService(); try { $labelsResponse = $service->users_labels->listUsersLabels("me"); if ($labelsResponse->getLabels()) { $labels = array_merge($labels, $labelsResponse->getLabels()); } } catch (Excetion $e) { print 'An error occurred: ' . $e->getMessage(); } $processed_label = false; foreach ($labels as $label) { if ($label->getName() == 'PROCESSED') { $processed_label = true; $processed_label_id = $label->getId(); } } if (!$processed_label) { $label = new \Google_Service_Gmail_Label(); $label->setName("PROCESSED"); try { $label = $service->users_labels->create("me", $label); } catch (Exception $e) { print 'An error occurred: ' . $e->getMessage(); } } $mods = new \Google_Service_Gmail_ModifyMessageRequest(); $mods->setAddLabelIds([$processed_label_id]); try { $message = $service->users_messages->modify("me", $this->gmail_uid, $mods); } catch (Exception $e) { print 'An error occurred: ' . $e->getMessage(); } }
public function doSendEmail(Request $request) { $mailbox = Mailbox::findOrFail($request->input('mailbox_id')); $mailbox->sendEmail(["to" => $request->input('to'), "cc" => $request->input('cc'), "bcc" => $request->input('bcc'), "subject" => $request->input('subject'), "message" => $request->input('message'), "attachments" => $request->file('attachment_file')]); return redirect()->back()->with('email-sent', true); }
public function postUkloniPoruku() { if (Mailbox::where(Input::get('inout') == 'inbox' ? 'korisnici_id' : 'od_id', Auth::user()->id)->where('id', Input::get('id'))->where('copy', Input::get('inout') == 'inbox' ? 0 : 1)->update(['aktivan' => 0])) { return json_encode(['msg' => 'Успешно сте уколонили поруку.', 'check' => 1]); } else { return json_encode(['msg' => 'Десила се грешка.', 'check' => 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(); } }