GWM : zoho mail api integration
This commit is contained in:
parent
5e9b34a40b
commit
cfe362270c
@ -1434,8 +1434,8 @@ class PlanController extends ResourceController
|
||||
{
|
||||
|
||||
echo '<pre>';
|
||||
// sendPlanCreationMail(23);
|
||||
print_r(getPlanApproverAction(32)); die;
|
||||
sendPlanCreationMail(35);
|
||||
print_r(getPlanApproverAction(35)); die;
|
||||
|
||||
// echo '<pre>';
|
||||
// print_r(getAllowedClassForUser(2,4)['hotel']); die;
|
||||
|
||||
@ -298,7 +298,7 @@ if (!function_exists('sendUserCreationMail')) {
|
||||
}
|
||||
}
|
||||
|
||||
if (!function_exists('send_email')) {
|
||||
// if (!function_exists('send_email')) {
|
||||
// function send_email($org_id , $toEmailId, $template , $userData , $planData = null, $url = null)
|
||||
// {
|
||||
// try {
|
||||
@ -400,113 +400,251 @@ if (!function_exists('send_email')) {
|
||||
// }
|
||||
|
||||
|
||||
if (!function_exists('send_email')) {
|
||||
function send_email($org_id, $toEmailId, $template, $userData, $planData = null, $url = null)
|
||||
{
|
||||
try {
|
||||
// Load models
|
||||
$organizationModel = new \App\Models\OrganizationModel();
|
||||
$mailTemplateModel = new \App\Models\MailTemplateModel();
|
||||
// if (!function_exists('send_email')) {
|
||||
// function send_email($org_id, $toEmailId, $template, $userData, $planData = null, $url = null)
|
||||
// {
|
||||
// try {
|
||||
// // Load models
|
||||
// $organizationModel = new \App\Models\OrganizationModel();
|
||||
// $mailTemplateModel = new \App\Models\MailTemplateModel();
|
||||
|
||||
// Fetch organization & template
|
||||
$orgData = $organizationModel->where('org_id', $org_id)->first();
|
||||
$templateData = $mailTemplateModel->where('org_id', $org_id)->where('template_name', $template)->first();
|
||||
// // Fetch organization & template
|
||||
// $orgData = $organizationModel->where('org_id', $org_id)->first();
|
||||
// $templateData = $mailTemplateModel->where('org_id', $org_id)->where('template_name', $template)->first();
|
||||
|
||||
if (!$orgData || !$templateData) {
|
||||
log_message('error', "[EMAIL ERROR] Missing org/template data for org_id={$org_id}, template={$template}");
|
||||
return ['status' => 'failed', 'code' => 404, 'message' => 'Missing organization or template data'];
|
||||
}
|
||||
// if (!$orgData || !$templateData) {
|
||||
// log_message('error', "[EMAIL ERROR] Missing org/template data for org_id={$org_id}, template={$template}");
|
||||
// return ['status' => 'failed', 'code' => 404, 'message' => 'Missing organization or template data'];
|
||||
// }
|
||||
|
||||
// Replace placeholders from user data
|
||||
foreach ($userData as $key => $value) {
|
||||
$templateData['body_html'] = str_replace('%' . $key . '%', $value ?? '', $templateData['body_html']);
|
||||
}
|
||||
// // Replace placeholders from user data
|
||||
// foreach ($userData as $key => $value) {
|
||||
// $templateData['body_html'] = str_replace('%' . $key . '%', $value ?? '', $templateData['body_html']);
|
||||
// }
|
||||
|
||||
// Replace placeholders from plan data
|
||||
if (!empty($planData)) {
|
||||
foreach ($planData as $key => $value) {
|
||||
$templateData['body_html'] = str_replace('%' . $key . '%', $value ?? '', $templateData['body_html']);
|
||||
}
|
||||
// // Replace placeholders from plan data
|
||||
// if (!empty($planData)) {
|
||||
// foreach ($planData as $key => $value) {
|
||||
// $templateData['body_html'] = str_replace('%' . $key . '%', $value ?? '', $templateData['body_html']);
|
||||
// }
|
||||
|
||||
// Exceptional reason block
|
||||
if (!empty($planData['exceptional_plan_reason'])) {
|
||||
$reasonHtml = '
|
||||
<table style="width:100%; border-collapse:collapse; font-family:Arial, sans-serif;">
|
||||
<tr>
|
||||
<td style="text-align:left;background-color:#ffdbdb;color:#960000;padding:7px 15px;font-weight:bold;border-top-left-radius:5px;border-top-right-radius:5px;border-bottom:1px solid #c65e56;">
|
||||
Reason for travel policy exception
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td style="text-align:left;background-color:#fff5f5;color:#960000ab;padding:10px 15px;border-bottom-left-radius:5px;border-bottom-right-radius:5px;">
|
||||
' . esc($planData['exceptional_plan_reason']) . '
|
||||
</td>
|
||||
</tr>
|
||||
</table>';
|
||||
$templateData['body_html'] .= $reasonHtml;
|
||||
}
|
||||
}
|
||||
// // Exceptional reason block
|
||||
// if (!empty($planData['exceptional_plan_reason'])) {
|
||||
// $reasonHtml = '
|
||||
// <table style="width:100%; border-collapse:collapse; font-family:Arial, sans-serif;">
|
||||
// <tr>
|
||||
// <td style="text-align:left;background-color:#ffdbdb;color:#960000;padding:7px 15px;font-weight:bold;border-top-left-radius:5px;border-top-right-radius:5px;border-bottom:1px solid #c65e56;">
|
||||
// Reason for travel policy exception
|
||||
// </td>
|
||||
// </tr>
|
||||
// <tr>
|
||||
// <td style="text-align:left;background-color:#fff5f5;color:#960000ab;padding:10px 15px;border-bottom-left-radius:5px;border-bottom-right-radius:5px;">
|
||||
// ' . esc($planData['exceptional_plan_reason']) . '
|
||||
// </td>
|
||||
// </tr>
|
||||
// </table>';
|
||||
// $templateData['body_html'] .= $reasonHtml;
|
||||
// }
|
||||
// }
|
||||
|
||||
// Add review URL if present
|
||||
if ($url !== null) {
|
||||
$templateData['body_html'] = str_replace('%trip_review_link%', $url, $templateData['body_html']);
|
||||
}
|
||||
// // Add review URL if present
|
||||
// if ($url !== null) {
|
||||
// $templateData['body_html'] = str_replace('%trip_review_link%', $url, $templateData['body_html']);
|
||||
// }
|
||||
|
||||
// Initialize email
|
||||
$email = \Config\Services::email(false);
|
||||
$email->initialize([
|
||||
'protocol' => 'smtp',
|
||||
'SMTPHost' => $orgData['mail_host'],
|
||||
'SMTPUser' => $orgData['mail_user_name'],
|
||||
'SMTPPass' => $orgData['mail_password'], // Use app password if Gmail
|
||||
'SMTPPort' => (int) $orgData['mail_port'],
|
||||
'SMTPCrypto' => 'tls', // ensure STARTTLS
|
||||
'mailType' => 'html',
|
||||
'charset' => 'utf-8',
|
||||
'newline' => "\r\n",
|
||||
'wordWrap' => true,
|
||||
]);
|
||||
// // Initialize email
|
||||
// $email = \Config\Services::email(false);
|
||||
// $email->initialize([
|
||||
// 'protocol' => 'smtp',
|
||||
// 'SMTPHost' => $orgData['mail_host'],
|
||||
// 'SMTPUser' => $orgData['mail_user_name'],
|
||||
// 'SMTPPass' => $orgData['mail_password'],
|
||||
// 'SMTPPort' => (int) $orgData['mail_port'],
|
||||
// // 'SMTPCrypto' => 'tls', // ensure STARTTLS
|
||||
// 'mailType' => 'html',
|
||||
// 'charset' => 'utf-8',
|
||||
// 'newline' => "\r\n",
|
||||
// 'wordWrap' => true,
|
||||
// ]);
|
||||
|
||||
$email->setFrom($orgData['sender_email'], $orgData['name'] ?? 'TripApprovalTool');
|
||||
$email->setTo($toEmailId);
|
||||
$email->setSubject($templateData['subject']);
|
||||
$email->setMessage($templateData['body_html']);
|
||||
// $email->setFrom($orgData['sender_email'], $orgData['name'] ?? 'TripApprovalTool');
|
||||
// $email->setTo($toEmailId);
|
||||
// $email->setSubject($templateData['subject']);
|
||||
// $email->setMessage($templateData['body_html']);
|
||||
|
||||
// Attach file if exists
|
||||
// if (!empty($planData['pdf_file_path']) && file_exists($planData['pdf_file_path'])) {
|
||||
// $email->attach($planData['pdf_file_path']);
|
||||
// }
|
||||
// // Attach file if exists
|
||||
// // if (!empty($planData['pdf_file_path']) && file_exists($planData['pdf_file_path'])) {
|
||||
// // $email->attach($planData['pdf_file_path']);
|
||||
// // }
|
||||
// // $filePath = WRITEPATH . 'pdf/trips/trip_' . $planData['plan_id'] . '.pdf';
|
||||
// // if (!empty($filePath) && file_exists($filePath) && is_readable($filePath)) {
|
||||
|
||||
if (!empty($planData['pdf_file_path']) && file_exists($planData['pdf_file_path']) && is_readable($planData['pdf_file_path'])) {
|
||||
$email->attach(
|
||||
$planData['pdf_file_path'],
|
||||
'attachment',
|
||||
basename($planData['pdf_file_path']),
|
||||
'application/pdf'
|
||||
);
|
||||
} else {
|
||||
log_message('error', "[EMAIL] PDF file not attached. Path: {$planData['pdf_file_path']}");
|
||||
}
|
||||
// // $email->attach($filePath, 'attachment', basename($filePath), 'application/pdf');
|
||||
|
||||
// // } else {
|
||||
// // log_message('error', "[EMAIL] PDF file not attached. Path: {$filePath}");
|
||||
// // }
|
||||
|
||||
// $filePath = WRITEPATH . 'pdf/trips/trip_' . $planData['plan_id'] . '.pdf';
|
||||
|
||||
// if (!empty($filePath) && file_exists($filePath) && is_readable($filePath)) {
|
||||
// // Read the file content
|
||||
// $fileContent = file_get_contents($filePath);
|
||||
|
||||
// if ($fileContent === false) {
|
||||
// log_message('error', "[EMAIL] Failed to read PDF file content. Path: {$filePath}");
|
||||
// // Handle error: perhaps skip attachment or throw an exception
|
||||
// } else {
|
||||
// // Attach the file content directly
|
||||
// $email->attach($fileContent, 'attachment', basename($filePath), 'application/pdf');
|
||||
// log_message('info', "[EMAIL] PDF file attached successfully. Path: {$filePath}");
|
||||
// }
|
||||
// } else {
|
||||
// log_message('error', "[EMAIL] PDF file not attached. Path: {$filePath}");
|
||||
// }
|
||||
|
||||
|
||||
// Send email
|
||||
if ($email->send()) {
|
||||
log_message('info', "[EMAIL SENT] To: {$toEmailId}, Template: {$template}");
|
||||
return ['status' => 'success', 'code' => 200, 'message' => 'Email sent successfully', 'data' => $toEmailId];
|
||||
} else {
|
||||
// Log full debugger info
|
||||
$debug = $email->printDebugger(['headers', 'subject', 'body']);
|
||||
log_message('error', "[EMAIL FAILED] To: {$toEmailId}, Template: {$template}\n" . strip_tags($debug));
|
||||
return ['status' => 'failed', 'code' => 500, 'message' => 'Email sending failed', 'debug' => strip_tags($debug)];
|
||||
}
|
||||
// // Send email
|
||||
// if ($email->send()) {
|
||||
// log_message('info', "[EMAIL SENT] To: {$toEmailId}, Template: {$template}");
|
||||
// $email->clear(true); // 'true' clears attachments as well
|
||||
// return ['status' => 'success', 'code' => 200, 'message' => 'Email sent successfully', 'data' => $toEmailId];
|
||||
// } else {
|
||||
// // Log full debugger info
|
||||
// $debug = $email->printDebugger(['headers', 'subject', 'body']);
|
||||
// log_message('error', "[EMAIL FAILED] To: {$toEmailId}, Template: {$template}\n" . strip_tags($debug));
|
||||
// $email->clear(true); // 'true' clears attachments as well
|
||||
// return ['status' => 'failed', 'code' => 500, 'message' => 'Email sending failed', 'debug' => strip_tags($debug)];
|
||||
// }
|
||||
|
||||
} catch (\Throwable $e) {
|
||||
log_message('critical', "[EMAIL EXCEPTION] To: {$toEmailId}, Error: " . $e->getMessage() . ' on line ' . $e->getLine());
|
||||
return ['status' => 'failed', 'code' => 500, 'message' => 'Exception: ' . $e->getMessage()];
|
||||
// } catch (\Throwable $e) {
|
||||
// log_message('critical', "[EMAIL EXCEPTION] To: {$toEmailId}, Error: " . $e->getMessage() . ' on line ' . $e->getLine());
|
||||
// return ['status' => 'failed', 'code' => 500, 'message' => 'Exception: ' . $e->getMessage()];
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
// }
|
||||
|
||||
|
||||
// zoho api service
|
||||
if (!function_exists('send_email')) {
|
||||
function send_email($org_id, $toEmailId, $template, $userData, $planData = null, $url = null)
|
||||
{
|
||||
try {
|
||||
$organizationModel = new \App\Models\OrganizationModel();
|
||||
$mailTemplateModel = new \App\Models\MailTemplateModel();
|
||||
|
||||
$orgData = $organizationModel->where('org_id', $org_id)->first();
|
||||
$templateData = $mailTemplateModel->where('org_id', $org_id)->where('template_name', $template)->first();
|
||||
|
||||
if (!$orgData || !$templateData) {
|
||||
log_message('error', "[EMAIL ERROR] Missing org/template data for org_id={$org_id}, template={$template}");
|
||||
return ['status' => 'failed', 'code' => 404, 'message' => 'Missing organization or template data'];
|
||||
}
|
||||
|
||||
// Replace placeholders
|
||||
foreach ($userData as $key => $value) {
|
||||
$templateData['body_html'] = str_replace('%' . $key . '%', $value ?? '', $templateData['body_html']);
|
||||
}
|
||||
|
||||
if (!empty($planData)) {
|
||||
foreach ($planData as $key => $value) {
|
||||
$templateData['body_html'] = str_replace('%' . $key . '%', $value ?? '', $templateData['body_html']);
|
||||
}
|
||||
|
||||
if (!empty($planData['exceptional_plan_reason'])) {
|
||||
$templateData['body_html'] .= '
|
||||
<table style="width:100%; border-collapse:collapse; font-family:Arial, sans-serif;">
|
||||
<tr>
|
||||
<td style="text-align:left;background-color:#ffdbdb;color:#960000;padding:7px 15px;font-weight:bold;">
|
||||
Reason for travel policy exception
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td style="text-align:left;background-color:#fff5f5;color:#960000ab;padding:10px 15px;">
|
||||
' . esc($planData['exceptional_plan_reason']) . '
|
||||
</td>
|
||||
</tr>
|
||||
</table>';
|
||||
}
|
||||
}
|
||||
|
||||
if ($url !== null) {
|
||||
$templateData['body_html'] = str_replace('%trip_review_link%', $url, $templateData['body_html']);
|
||||
}
|
||||
|
||||
// Set up ZeptoMail API request
|
||||
$endpoint = "https://api.zeptomail.com/v1.1/email";
|
||||
$apiKey = env('ZOHO_API_KEY');
|
||||
$sender = env('ZOHO_SENDER_MAIL');
|
||||
|
||||
$attachments = [];
|
||||
|
||||
$filePath = WRITEPATH . 'pdf/trips/trip_' . $planData['plan_id'] . '.pdf';
|
||||
if (!empty($filePath) && file_exists($filePath) && is_readable($filePath)) {
|
||||
$attachments[] = [
|
||||
"content" => base64_encode(file_get_contents($filePath)),
|
||||
"mime_type" => "application/pdf",
|
||||
"name" => basename($filePath)
|
||||
];
|
||||
}
|
||||
|
||||
$payload = [
|
||||
"from" => [
|
||||
"address" => $sender,
|
||||
"name" => $orgData['name'] ?? "TripApprovalTool"
|
||||
],
|
||||
"to" => [
|
||||
["email_address" => ["address" => $toEmailId]]
|
||||
],
|
||||
"subject" => $templateData['subject'],
|
||||
"htmlbody" => $templateData['body_html'],
|
||||
];
|
||||
|
||||
if (!empty($attachments)) {
|
||||
$payload["attachments"] = $attachments;
|
||||
}
|
||||
|
||||
$headers = [
|
||||
"Content-Type: application/json",
|
||||
"Authorization: Zoho-enczapikey " . $apiKey
|
||||
];
|
||||
|
||||
$ch = curl_init();
|
||||
curl_setopt($ch, CURLOPT_URL, $endpoint);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($ch, CURLOPT_POST, true);
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload));
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
||||
|
||||
$response = curl_exec($ch);
|
||||
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
|
||||
if (curl_errno($ch)) {
|
||||
$error_msg = curl_error($ch);
|
||||
log_message('error', "[EMAIL ERROR] Curl failed: {$error_msg}");
|
||||
return ['status' => 'failed', 'code' => 500, 'message' => 'Curl error', 'error' => $error_msg];
|
||||
}
|
||||
|
||||
curl_close($ch);
|
||||
|
||||
$resp = json_decode($response, true);
|
||||
|
||||
if ($httpCode === 200 && isset($resp['request_id'])) {
|
||||
log_message('info', "[EMAIL SENT] To: {$toEmailId}, Template: {$template}");
|
||||
return ['status' => 'success', 'code' => 200, 'message' => 'Email sent successfully', 'data' => $toEmailId];
|
||||
} else {
|
||||
log_message('error', "[EMAIL FAILED] HTTP: {$httpCode}, Response: {$response}");
|
||||
return ['status' => 'failed', 'code' => $httpCode, 'message' => 'Email sending failed', 'response' => $response];
|
||||
}
|
||||
|
||||
} catch (\Throwable $e) {
|
||||
log_message('critical', "[EMAIL EXCEPTION] To: {$toEmailId}, Error: " . $e->getMessage() . ' on line ' . $e->getLine());
|
||||
return ['status' => 'failed', 'code' => 500, 'message' => 'Exception: ' . $e->getMessage()];
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (!function_exists('send_email_agent')) {
|
||||
|
||||
BIN
writable/pdf/trips/trip_35.pdf
Normal file
BIN
writable/pdf/trips/trip_35.pdf
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user