FEAT_EXPLORED_GMAILAPI_ATTACHEMENT&MAIL_DELIVERY_STATUS
This commit is contained in:
parent
9f94baf362
commit
25bbfa1125
@ -355,7 +355,8 @@ $routes->cli('cli/processjobs', 'JobWorker::processJobs');
|
|||||||
|
|
||||||
$routes->get("processjob", "JobWorker::processJob");
|
$routes->get("processjob", "JobWorker::processJob");
|
||||||
$routes->cli('cli/new_gmail_token', 'MasterController::generateNewGmailAPIToken');
|
$routes->cli('cli/new_gmail_token', 'MasterController::generateNewGmailAPIToken');
|
||||||
$routes->cli('cli/send_mail_cli(/:any)?', 'MasterController::testGmailAPIViaCLI$1');
|
$routes->cli('cli/send_mail_cli', 'MasterController::testGmailAPIViaCLI');
|
||||||
|
$routes->cli('cli/check_bounce_mail_cli', 'MasterController::testCheckBounceMails');
|
||||||
$routes->cli('cli/app_check_list', 'MasterController::appCheckList');
|
$routes->cli('cli/app_check_list', 'MasterController::appCheckList');
|
||||||
$routes->cli('cli/new_gdrive_token', 'GoogleDriveController::generateNewGoogleDriveAccessToken');
|
$routes->cli('cli/new_gdrive_token', 'GoogleDriveController::generateNewGoogleDriveAccessToken');
|
||||||
|
|
||||||
|
|||||||
@ -34,6 +34,8 @@ use App\Models\InsurerExcelExportTemplateModel;
|
|||||||
use App\Models\SettingsModel;
|
use App\Models\SettingsModel;
|
||||||
use App\Models\VehicleModel;
|
use App\Models\VehicleModel;
|
||||||
|
|
||||||
|
use CodeIgniter\CLI\CLI;
|
||||||
|
|
||||||
class MasterController extends AdminController
|
class MasterController extends AdminController
|
||||||
{
|
{
|
||||||
use ResponseTrait;
|
use ResponseTrait;
|
||||||
@ -1672,7 +1674,7 @@ class MasterController extends AdminController
|
|||||||
echo "Check the following..!\n";
|
echo "Check the following..!\n";
|
||||||
echo "1. Update Gmail Oauth credentials in 'gmail_api_oauth_credentials' field in 'settings' table in JSON format.\n";
|
echo "1. Update Gmail Oauth credentials in 'gmail_api_oauth_credentials' field in 'settings' table in JSON format.\n";
|
||||||
echo "2. Call this cli route to generate new access token: 'php public/index.php cli/new_gmail_token' from root of the project.\n";
|
echo "2. Call this cli route to generate new access token: 'php public/index.php cli/new_gmail_token' from root of the project.\n";
|
||||||
echo "3. Call this cli route : 'php public/index.php cli/send_mail_cli someone@gmail.com' to send test mail to testGmailAPIViaCLI ( replace someone@gmail.com with real time mail).\n";
|
echo "3. Call this cli route : 'php public/index.php cli/send_mail_cli --to someone@gmail.com' to send test mail to testGmailAPIViaCLI ( replace someone@gmail.com with real time mail).\n";
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|||||||
@ -93,6 +93,7 @@ class MailHelper
|
|||||||
$emaill = $params['mail'];
|
$emaill = $params['mail'];
|
||||||
$subject = $params['subject'];
|
$subject = $params['subject'];
|
||||||
$message = $params['message'];
|
$message = $params['message'];
|
||||||
|
$attachments = isset($params['attachments']) && count($params['attachments']) ? $params['attachments'] : [];
|
||||||
|
|
||||||
//check BCC mail
|
//check BCC mail
|
||||||
if (isset($params['bcc'])) {
|
if (isset($params['bcc'])) {
|
||||||
@ -118,7 +119,7 @@ class MailHelper
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
$res = $gmailapi->sendMessage($emaill, $subject, $message, 'no-reply@nhanceindia.in', $reply_to, $cc, $bcc);
|
$res = $gmailapi->sendMessage($emaill, $subject, $message, 'no-reply@nhanceindia.in', $reply_to, $cc, $bcc,$attachments);
|
||||||
|
|
||||||
if($res['status'])
|
if($res['status'])
|
||||||
{
|
{
|
||||||
|
|||||||
@ -26,18 +26,20 @@ class GmailAPI
|
|||||||
'https://www.googleapis.com/auth/gmail.addons.current.message.readonly',
|
'https://www.googleapis.com/auth/gmail.addons.current.message.readonly',
|
||||||
'https://www.googleapis.com/auth/gmail.readonly',
|
'https://www.googleapis.com/auth/gmail.readonly',
|
||||||
'https://www.googleapis.com/auth/gmail.labels',
|
'https://www.googleapis.com/auth/gmail.labels',
|
||||||
'https://www.googleapis.com/auth/gmail.send'
|
'https://www.googleapis.com/auth/gmail.send',
|
||||||
|
'https://mail.google.com/'
|
||||||
]);
|
]);
|
||||||
$this->setAuthConfig();
|
$this->setAuthConfig();
|
||||||
//die();
|
//die();
|
||||||
$this->client->setAccessType('offline');
|
$this->client->setAccessType('offline');
|
||||||
$this->client->setPrompt('select_account consent');
|
$this->client->setPrompt('select_account consent');
|
||||||
|
// echo uri_string();//die();
|
||||||
if (php_sapi_name() !== 'cli' || (uri_string() == 'cli/send_mail_cli' || uri_string() == 'cli/processjob') )
|
if (php_sapi_name() !== 'cli' || (uri_string() == 'cli/send_mail_cli' || uri_string() == 'cli/processjob' || uri_string() == 'cli/check_bounce_mail_cli') )
|
||||||
{
|
{
|
||||||
|
// echo 'getting token';die();
|
||||||
$this->setTokenFromDB(); // Call only if not in CLI
|
$this->setTokenFromDB(); // Call only if not in CLI
|
||||||
}
|
}
|
||||||
|
// die();
|
||||||
$this->service = new Gmail($this->client);
|
$this->service = new Gmail($this->client);
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -99,10 +101,12 @@ class GmailAPI
|
|||||||
$settingsModel->where('id', 1)->set([$this->tokenField => $token])->update();
|
$settingsModel->where('id', 1)->set([$this->tokenField => $token])->update();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function createMessage($to, $subject, $htmlContent, $from, $replyTo, $cc = [], $bcc = [])
|
public function createMessage($to, $subject, $htmlContent, $from, $replyTo, $cc = [], $bcc = [], $attachments = [])
|
||||||
{
|
{
|
||||||
$boundary = uniqid(rand(), true);
|
// print_r($to);die();
|
||||||
|
$boundary = uniqid(rand(), true); // Unique boundary for separating parts
|
||||||
|
|
||||||
|
// Initialize raw message headers
|
||||||
$rawMessageString = "From: $from\r\n";
|
$rawMessageString = "From: $from\r\n";
|
||||||
$rawMessageString .= "To: $to\r\n";
|
$rawMessageString .= "To: $to\r\n";
|
||||||
$rawMessageString .= "Reply-To: $replyTo\r\n";
|
$rawMessageString .= "Reply-To: $replyTo\r\n";
|
||||||
@ -117,26 +121,53 @@ class GmailAPI
|
|||||||
|
|
||||||
$rawMessageString .= "Subject: $subject\r\n";
|
$rawMessageString .= "Subject: $subject\r\n";
|
||||||
$rawMessageString .= "MIME-Version: 1.0\r\n";
|
$rawMessageString .= "MIME-Version: 1.0\r\n";
|
||||||
$rawMessageString .= "Content-Type: multipart/alternative; boundary=\"$boundary\"\r\n\r\n";
|
$rawMessageString .= "Content-Type: multipart/mixed; boundary=\"$boundary\"\r\n\r\n";
|
||||||
|
|
||||||
|
// Add HTML content as an alternative part
|
||||||
$rawMessageString .= "--$boundary\r\n";
|
$rawMessageString .= "--$boundary\r\n";
|
||||||
$rawMessageString .= "Content-Type: text/html; charset=UTF-8\r\n";
|
$rawMessageString .= "Content-Type: text/html; charset=UTF-8\r\n";
|
||||||
$rawMessageString .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
|
$rawMessageString .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
|
||||||
$rawMessageString .= $htmlContent . "\r\n";
|
$rawMessageString .= $htmlContent . "\r\n\r\n";
|
||||||
|
|
||||||
|
// Process each attachment
|
||||||
|
foreach ($attachments as $attachment) {
|
||||||
|
$filePath = $attachment['filePath'];
|
||||||
|
$fileName = $attachment['fileName'];
|
||||||
|
$fileData = file_get_contents($filePath);
|
||||||
|
|
||||||
|
if ($fileData === false) {
|
||||||
|
continue; // Skip this attachment if file cannot be read
|
||||||
|
}
|
||||||
|
|
||||||
|
$base64File = base64_encode($fileData);
|
||||||
|
|
||||||
|
// Add each attachment with appropriate headers
|
||||||
|
$rawMessageString .= "--$boundary\r\n";
|
||||||
|
$rawMessageString .= "Content-Type: application/octet-stream; name=\"$fileName\"\r\n";
|
||||||
|
$rawMessageString .= "Content-Disposition: attachment; filename=\"$fileName\"\r\n";
|
||||||
|
$rawMessageString .= "Content-Transfer-Encoding: base64\r\n\r\n";
|
||||||
|
$rawMessageString .= chunk_split($base64File) . "\r\n\r\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
// End boundary to signify the end of the message
|
||||||
$rawMessageString .= "--$boundary--";
|
$rawMessageString .= "--$boundary--";
|
||||||
|
|
||||||
|
// Encode the final raw message in base64 for Gmail API
|
||||||
$rawMessage = base64_encode($rawMessageString);
|
$rawMessage = base64_encode($rawMessageString);
|
||||||
$rawMessage = str_replace(['+', '/', '='], ['-', '_', ''], $rawMessage);
|
$rawMessage = str_replace(['+', '/', '='], ['-', '_', ''], $rawMessage);
|
||||||
|
|
||||||
|
// Create Gmail message object
|
||||||
$message = new Message();
|
$message = new Message();
|
||||||
$message->setRaw($rawMessage);
|
$message->setRaw($rawMessage);
|
||||||
|
|
||||||
return $message;
|
return $message;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function sendMessage($to, $subject, $htmlContent, $from, $replyTo, $cc = [], $bcc = [])
|
|
||||||
|
public function sendMessage($to, $subject, $htmlContent, $from, $replyTo, $cc = [], $bcc = [],$attachments = [])
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$message = $this->createMessage($to, $subject, $htmlContent, $from, $replyTo, $cc, $bcc);
|
$message = $this->createMessage($to, $subject, $htmlContent, $from, $replyTo, $cc, $bcc,$attachments);
|
||||||
$res = $this->service->users_messages->send('me', $message);
|
$res = $this->service->users_messages->send('me', $message);
|
||||||
return array('status' => true, 'data' => $res);
|
return array('status' => true, 'data' => $res);
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
@ -177,4 +208,59 @@ class GmailAPI
|
|||||||
$this->myLogger->logme('error', 'New token generated and stored in the database.');
|
$this->myLogger->logme('error', 'New token generated and stored in the database.');
|
||||||
print 'New token generated and stored in the database.';
|
print 'New token generated and stored in the database.';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public function checkBounceStatus( string $userId = 'me',string $from_date = null,string $to_date = null,int $count = null,string $page = null)
|
||||||
|
{
|
||||||
|
$query = 'from:mailer-daemon OR "delivery failed" OR "failure notice"';
|
||||||
|
if($from_date != '' && $to_date != '')
|
||||||
|
{
|
||||||
|
$query .= 'after:' . $from_date . ' before:' . $to_date;
|
||||||
|
}
|
||||||
|
$maxResults = $count ? $count : 20;
|
||||||
|
$pageToken = $page ? $page : null;
|
||||||
|
|
||||||
|
$params = ['q' => $query,'maxResults' => $maxResults];
|
||||||
|
if(isset($pageToken)){ $params['pageToken'] = $pageToken; }
|
||||||
|
$messages = $this->service->users_messages->listUsersMessages($userId, $params);
|
||||||
|
|
||||||
|
$bounceDetails = [];
|
||||||
|
$pageToken = $messages->getNextPageToken();
|
||||||
|
$bounceDetails['pageToken'] = $pageToken;
|
||||||
|
if (count($messages->getMessages()) > 0) {
|
||||||
|
foreach ($messages->getMessages() as $message) {
|
||||||
|
// Get full message details
|
||||||
|
$msg = $this->service->users_messages->get($userId, $message->getId(), ['format' => 'full']);
|
||||||
|
|
||||||
|
// Extract important headers and content
|
||||||
|
$headers = $msg->getPayload()->getHeaders();
|
||||||
|
$subject = null;
|
||||||
|
$date = null;
|
||||||
|
$to = null;
|
||||||
|
|
||||||
|
foreach ($headers as $header) {
|
||||||
|
if ($header->getName() === 'Subject') {
|
||||||
|
$subject = $header->getValue();
|
||||||
|
}
|
||||||
|
if ($header->getName() === 'Date') {
|
||||||
|
$date = $header->getValue();
|
||||||
|
}
|
||||||
|
if ($header->getName() === 'To') {
|
||||||
|
$to = $header->getValue();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$bounceDetails['data'][] = [
|
||||||
|
'subject' => $subject,
|
||||||
|
'date' => $date,
|
||||||
|
'to' => $to,
|
||||||
|
'snippet' => $msg->getSnippet(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $bounceDetails;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -67,6 +67,7 @@
|
|||||||
<th class="font-weight-medium">Name</th>
|
<th class="font-weight-medium">Name</th>
|
||||||
<th class="font-weight-medium">EMP Code</th>
|
<th class="font-weight-medium">EMP Code</th>
|
||||||
<th class="font-weight-medium">Relationship</th>
|
<th class="font-weight-medium">Relationship</th>
|
||||||
|
<th class="font-weight-medium">Gender</th>
|
||||||
<th class="font-weight-medium">Date of Birth</th>
|
<th class="font-weight-medium">Date of Birth</th>
|
||||||
<th class="font-weight-medium">Policy name</th>
|
<th class="font-weight-medium">Policy name</th>
|
||||||
<th class="font-weight-medium">Insurer name</th>
|
<th class="font-weight-medium">Insurer name</th>
|
||||||
@ -100,6 +101,7 @@
|
|||||||
<td><?php echo $employee['name']; ?></td>
|
<td><?php echo $employee['name']; ?></td>
|
||||||
<td><?php echo $employee['emp_code']; ?></td>
|
<td><?php echo $employee['emp_code']; ?></td>
|
||||||
<td><?php echo $employee['relationship']; ?></td>
|
<td><?php echo $employee['relationship']; ?></td>
|
||||||
|
<td><?php echo $employee['gender']; ?></td>
|
||||||
<td><?php echo date('d/m/Y', strtotime($employee['dob'])); ?></td>
|
<td><?php echo date('d/m/Y', strtotime($employee['dob'])); ?></td>
|
||||||
<td><?php echo isset($employee['policy_type']) ? $employee['policy_type'] : ''; ?> - <?php echo isset($employee['policy_no']) ? $employee['policy_no'] : ''; ?></td>
|
<td><?php echo isset($employee['policy_type']) ? $employee['policy_type'] : ''; ?> - <?php echo isset($employee['policy_no']) ? $employee['policy_no'] : ''; ?></td>
|
||||||
<td><?php echo $employee['insurer_short_name']; ?></td>
|
<td><?php echo $employee['insurer_short_name']; ?></td>
|
||||||
|
|||||||
Binary file not shown.
Loading…
Reference in New Issue
Block a user