Skip to content

Instantly share code, notes, and snippets.

@ghermans
Created November 23, 2016 21:25
Show Gist options
  • Save ghermans/ba4c034060a868b3d23975d754919c6e to your computer and use it in GitHub Desktop.
Save ghermans/ba4c034060a868b3d23975d754919c6e to your computer and use it in GitHub Desktop.
Laravel IMAP Mail Sync
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
class AddMailIdToMailAttachmentsTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('mail_attachments', function(Blueprint $table)
{
$table->integer("mail_id")->unsigned();
$table->foreign("mail_id")->references("id")->on("mails")->onDelete("cascade");
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('mail_attachments', function(Blueprint $table)
{
$table->dropForeign("mail_attachments_mail_id_foreign");
$table->dropColumn("mail_id");
});
}
}
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
class CreateMailAttachmentsTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('mail_attachments', function(Blueprint $table)
{
$table->increments('id');
$table->bigInteger("aid");
$table->string("name")->nullable();
$table->string("path")->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('mail_attachments');
}
}
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
class CreateMailsTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('mails', function(Blueprint $table)
{
$table->increments('id');
$table->bigInteger("mid");
$table->dateTime("date");
$table->string("subject")->nullable();
$table->string("fromName")->nullable();
$table->string("fromAddress")->nullable();
$table->string("to")->nullable();
$table->string("cc")->nullable();
$table->string("reply")->nullable();
$table->text("plain")->nullable();
$table->text("html")->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('mails');
}
}
<?php
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
class MailCheck extends Command {
/**
* The console command name.
*
* @var string
*/
protected $name = 'mail:check';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Check IMAP mails and sync.';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function fire()
{
$mailbox = new PhpImap\Mailbox('{imap.gmail.com:993/imap/ssl}INBOX', '*****@gmail.com', '***********', public_path()."/upload/mail-attachments/");
$mailIds = $mailbox->searchMailBox('ALL');
if(!$mailIds) {
$this->info("No mail!");
} else {
foreach($mailIds as $mailId) {
if(MailArchive::where("mid", $mailId)->count() == 0) {
$getMail = $mailbox->getMail($mailId);
$tos = [];
foreach($getMail->to as $toMail => $toName) {
$tos[] = $toName."(".$toMail.")";
}
$ccs = [];
foreach($getMail->cc as $ccMail => $ccName) {
$ccs[] = $ccName."(".$ccMail.")";
}
$replys = [];
foreach($getMail->replyTo as $replyMail => $replyName) {
$replys[] = $replyName."(".$replyMail.")";
}
$mail = new MailArchive;
$mail->mid = $getMail->id;
$mail->date = $getMail->date;
$mail->subject = $getMail->subject;
$mail->fromName = $getMail->fromName;
$mail->fromAddress = $getMail->fromAddress;
$mail->to = implode(",", $tos);
$mail->cc = implode(",", $ccs);
$mail->reply = implode(",", $replys);
$mail->plain = $getMail->textPlain;
$mail->html = $getMail->textHtml;
$mail->save();
foreach($getMail->getAttachments() as $getAttachment) {
$attachment = new MailAttachment();
$attachment->aid = $getAttachment->id;
$attachment->name = $getAttachment->name;
$attachment->path = $getAttachment->filePath;
$attachment->mail_id = $mail->id;
$attachment->save();
}
$this->info("New Mail:".$mail->subject);
}
}
}
}
/**
* Get the console command arguments.
*
* @return array
*/
protected function getArguments()
{
return array(
);
}
/**
* Get the console command options.
*
* @return array
*/
protected function getOptions()
{
return array(
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment