Skip to content

Instantly share code, notes, and snippets.

@codingdotlog
Last active November 28, 2019 10:59
Show Gist options
  • Save codingdotlog/ccfca37c59b56b39b0759dc10e7a98ce to your computer and use it in GitHub Desktop.
Save codingdotlog/ccfca37c59b56b39b0759dc10e7a98ce to your computer and use it in GitHub Desktop.
<?php
namespace App\Http\Controllers;
use Webklex\IMAP\Client;
use App\InsertTicket;
class EmailPollingService extends Controller {
public function check() {
$oClient = new Client([
'host' => env("IMAP_HOST", "somedefaultvalue"),
'port' => env("IMAP_PORT", "somedefaultvalue"),
'encryption' => env("IMAP_ENCRYPTION", "somedefaultvalue"),
'validate_cert' => env("IMAP_VALIDATE_CERT", "somedefaultvalue"),
'username' => env("IMAP_USERNAME", "somedefaultvalue"),
'password' => env("IMAP_PASSWORD", "somedefaultvalue"),
'protocol' => env("IMAP_PROTOCOL", "somedefaultvalue"),
]);
$oClient->connect();
$aFolder = $oClient->getFolders();
foreach($aFolder as $oFolder){
$aMessage = $oFolder->query()->since(now()->subDays(1))->get();
foreach($aMessage as $oMessage) {
/*
InsertTicket::create([
'subject'=>$oMessage->getSubject(),
'body'=>$oMessage->getSubject(),
['body' => $oMessage->getHTMLBody(true)],
]);
*/
$ticket = InsertTicket::firstOrNew(
['subject' => $oMessage->getSubject()],
['body' => $oMessage->getHTMLBody(true)],
['sender' => $oMessage->getSender()[0]->mail],
['opened_at' => $oMessage->getDate()],
['attachment' => $oMessage->getAttachments()->count()]);
$ticket->save();
}
}
}
}
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class InsertTicket extends Model
{
protected $table = 'tickets';
protected $fillable = ['subject', 'body', 'sender', 'opened_at', 'attachment'];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment