Skip to content

Instantly share code, notes, and snippets.

@nutch31
Created May 26, 2019 04:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nutch31/9b1f53970566223336d686d127583116 to your computer and use it in GitHub Desktop.
Save nutch31/9b1f53970566223336d686d127583116 to your computer and use it in GitHub Desktop.
<?php
namespace App\Http\Controllers;
use App\Http\Requests\requestCreateFormalEmail;
use App\Http\Requests\requestResetPassword;
use Illuminate\Http\Request;
use App\ResetPassword;
use App\FormalEmail;
use Illuminate\Support\Facades\Storage;
use App\Jobs\ResetPasswordEmail;
use App\Jobs\CreateFormalEmail;
class AlphaController extends Controller
{
/**
* @param Request $request
* @return \Illuminate\Http\JsonResponse
*/
public function createFormalEmail(Request $request)
{
if(!empty($request->file('file')))
{
$file = $request->file('file');
$raw_data = file_get_contents($file->getRealPath());
$file_mime_type = $file->getMimeType();
$file_name = str_random(5).'_'.$request->get('file_name');
$path_name = $request->file('file')->storeAs(
'uploads', $file_name
);
}
else
{
$raw_data = NULL;
$file_mime_type = NULL;
$file_name = NULL;
$path_name = NULL;
}
// Job
dispatch(new CreateFormalEmail(
$request->get('email_from'),
$request->get('email'),
$request->get('email_cc'),
$request->get('email_bcc'),
$request->get('name'),
$request->get('subject_name'),
$request->get('content'),
$request->get('file_name'),
$raw_data,
$file_mime_type));
// Body Mail
$html = view('email.sendCreateFormalEmail', [
"name" => $request->get('name'),
"subject_name" => $request->get('subject_name'),
"content" => $request->get('content')
]);
// Query
FormalEmail::create([
'name' => $request->get('name'),
'email_from' => $request->get('email_from'),
'email' => json_encode($request->get('email')),
'email_cc' => json_encode($request->get('email_cc')),
'email_bcc' => json_encode($request->get('email_bcc')),
'subject_name' => $request->get('subject_name'),
'content' => $request->get('content'),
'file' => $path_name,
'file_name' => $file_name,
'body_mail' => $html,
'status' => 1
]);
return response()->json([
"message" => "Success",
"Status" => "Success"
], 200);
}
}
<?php
namespace App\Jobs;
use App\Mail\SendFormalEmail;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Support\Facades\Mail;
class CreateFormalEmail implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $email_from;
public $email;
public $email_cc;
public $email_bcc;
public $name;
public $subject_name;
public $content;
public $file_name;
public $raw_data;
public $file_mime_type;
//public $timeout = 120;
public $tries = 3;
/**
* CreateFormalEmail constructor.
* @param $email_from
* @param $email
* @param $email_cc
* @param $email_bcc
* @param $name
* @param $subject_name
* @param $content
* @param $file_name
* @param $raw_data
* @param $file_mime_type
*/
public function __construct($email_from, $email, $email_cc, $email_bcc, $name, $subject_name, $content, $file_name, $raw_data, $file_mime_type)
{
$this->email_from = $email_from;
$this->email = $email;
$this->email_cc = $email_cc;
$this->email_bcc = $email_bcc;
$this->name = $name;
$this->subject_name = $subject_name;
$this->content = $content;
$this->file_name = $file_name;
$this->raw_data = $raw_data;
$this->file_mime_type = $file_mime_type;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$email_cc = [];
if(!empty($this->email_cc))
{
$email_cc = array_filter($this->email_cc);
}
$email_bcc = [];
if(!empty($this->email_bcc))
{
$email_bcc = array_filter($this->email_bcc);
}
// Mail
$mail = Mail::to($this->email);
if(!empty($email_cc))
{
$mail->cc($email_cc);
}
if(!empty($email_bcc))
{
$mail->bcc($email_bcc);
}
$mail->send(new SendFormalEmail(
$this->email_from,
$this->name,
$this->subject_name,
$this->content,
$this->file_name,
$this->raw_data,
$this->file_mime_type
));
}
/**
* The job failed to process.
*
* @param Exception $exception
* @return void
*/
public function failed(Exception $exception)
{
}
}
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class SendFormalEmail extends Mailable
{
use Queueable, SerializesModels;
public $email_from;
public $name;
public $subject_name;
public $content;
public $file_name;
public $raw_data;
public $file_mime_type;
/**
* SendFormalEmail constructor.
* @param $email_from
* @param $name
* @param $subject_name
* @param $content
* @param $file_name
* @param $raw_data
* @param $file_mime_type
*/
public function __construct($email_from, $name, $subject_name, $content, $file_name, $raw_data, $file_mime_type)
{
$this->email_from = $email_from;
$this->name = $name;
$this->subject_name = $subject_name;
$this->content = $content;
$this->file_name = $file_name;
$this->raw_data = $raw_data;
$this->file_mime_type = $file_mime_type;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
$mail = $this->from($this->email_from)
->view('email.sendCreateFormalEmail')
->subject($this->subject_name)
->with([
"name" => $this->name,
"subject_name" => $this->subject_name,
"content" => $this->content
]);
if(!empty($this->raw_data))
{
$mail->attachData($this->raw_data, $this->file_name, [
'mime' => $this->file_mime_type,
]);
}
return $mail;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment