2024-06-26 12:28:46 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Mailgun;
|
|
|
|
|
|
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
|
use Illuminate\Http\Request;
|
2024-06-28 06:58:04 +00:00
|
|
|
use Illuminate\Support\Facades\DB;
|
2024-09-12 11:56:53 +00:00
|
|
|
use Illuminate\Support\Facades\Storage;
|
2024-06-26 12:28:46 +00:00
|
|
|
class EmailController extends Controller
|
|
|
|
|
{
|
|
|
|
|
public function saveEmail(Request $request){
|
2024-09-12 11:56:53 +00:00
|
|
|
|
|
|
|
|
// DB::beginTransaction();
|
2024-06-26 12:28:46 +00:00
|
|
|
|
2024-09-12 11:56:53 +00:00
|
|
|
// try {
|
2024-06-28 06:58:04 +00:00
|
|
|
|
|
|
|
|
$token = $request->input('token');
|
|
|
|
|
$timestamp = $request->input('timestamp');
|
|
|
|
|
$signature = $request->input('signature');
|
2024-06-26 12:28:46 +00:00
|
|
|
|
2024-06-28 06:58:04 +00:00
|
|
|
if (!verifyMailgunSignature($token, $timestamp, $signature)) {
|
|
|
|
|
update_setting('aw_test','Invalid signature.');
|
|
|
|
|
abort(403, 'Invalid signature.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$data = $this->extractMailgunData($request->all());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$to_email = $data['to_email'];
|
|
|
|
|
$message = $data['message'];
|
|
|
|
|
|
|
|
|
|
update_setting('aw_test',$to_email);
|
|
|
|
|
|
|
|
|
|
$company = get_company('email',$to_email);
|
|
|
|
|
|
|
|
|
|
if($company){
|
2024-06-29 19:15:55 +00:00
|
|
|
$ticket = insertTicket($data['from_email'], $company->email, $data['subject'], $message,'inbox',$data['from_name'] );
|
2024-09-12 11:56:53 +00:00
|
|
|
if($ticket){
|
2024-06-28 06:58:04 +00:00
|
|
|
$response = createResponse($ticket->id,$message);
|
2024-09-12 11:56:53 +00:00
|
|
|
|
|
|
|
|
$attachmentCount = $request->input('attachment-count', 0);
|
|
|
|
|
|
|
|
|
|
update_setting('aw_test',$attachmentCount);
|
|
|
|
|
|
|
|
|
|
for ($i = 1; $i <= $attachmentCount; $i++) {
|
|
|
|
|
$attachment = $request->file("attachment-$i");
|
|
|
|
|
update_setting('aw_test',$attachment->getClientOriginalName());
|
|
|
|
|
if ($attachment && $attachment->isValid()) {
|
|
|
|
|
// Define a unique filename, possibly using the original filename and appending a timestamp or a unique id
|
|
|
|
|
$filename = time() . '_' . $attachment->getClientOriginalName();
|
|
|
|
|
|
|
|
|
|
// Save the attachment to the local or specific disk
|
|
|
|
|
$filePath = $attachment->storeAs('tickets/' . $ticket->id, $filename, 'public');
|
|
|
|
|
$fileUrl = url(Storage::url($filePath));
|
|
|
|
|
update_setting('aw_test',$fileUrl);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2024-06-28 06:58:04 +00:00
|
|
|
}else{}
|
|
|
|
|
|
2024-09-12 11:56:53 +00:00
|
|
|
// update_setting('aw_test',json_encode($request->all()));
|
2024-06-28 06:58:04 +00:00
|
|
|
|
2024-09-12 11:56:53 +00:00
|
|
|
// DB::commit();
|
|
|
|
|
// } catch (\Exception $e) {
|
|
|
|
|
|
|
|
|
|
// DB::rollBack();
|
|
|
|
|
// update_setting('aw_test',json_encode($e->getMessage()));
|
|
|
|
|
// sendEmailViaMailgun( 'ai.rapidev.tech', 'support@ai.rapidev.tech', '16bsse18212@gmail.com', 'error', json_encode($e->getMessage()));
|
|
|
|
|
// // Handle the exception
|
|
|
|
|
// }
|
2024-06-26 12:28:46 +00:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function extractMailgunData($data) {
|
2024-06-28 06:58:04 +00:00
|
|
|
|
2024-06-26 12:28:46 +00:00
|
|
|
// Prepare an array to hold the extracted data
|
|
|
|
|
$extractedData = [
|
|
|
|
|
'from_email' => $data['from'],
|
|
|
|
|
'to_email' => $data['To'],
|
|
|
|
|
'from_name' => '', // This will be extracted from the 'from' field
|
|
|
|
|
'subject' => $data['subject'],
|
2024-09-12 11:56:53 +00:00
|
|
|
'message' => $data['body-html'],
|
2024-06-26 12:28:46 +00:00
|
|
|
'mime_version' => $data['Mime-Version'],
|
|
|
|
|
'dkim_signature' => $data['Dkim-Signature']
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
// Extract the sender's name from the 'from' field
|
|
|
|
|
if (preg_match('/^(.*?) <(.*?)>$/', $data['from'], $matches)) {
|
|
|
|
|
$extractedData['from_name'] = $matches[1];
|
|
|
|
|
$extractedData['from_email'] = $matches[2];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $extractedData;
|
|
|
|
|
}
|
|
|
|
|
}
|