GWM : zoho mail api integration
This commit is contained in:
parent
5e9b34a40b
commit
cfe362270c
@ -1434,8 +1434,8 @@ class PlanController extends ResourceController
|
|||||||
{
|
{
|
||||||
|
|
||||||
echo '<pre>';
|
echo '<pre>';
|
||||||
// sendPlanCreationMail(23);
|
sendPlanCreationMail(35);
|
||||||
print_r(getPlanApproverAction(32)); die;
|
print_r(getPlanApproverAction(35)); die;
|
||||||
|
|
||||||
// echo '<pre>';
|
// echo '<pre>';
|
||||||
// print_r(getAllowedClassForUser(2,4)['hotel']); die;
|
// 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)
|
// function send_email($org_id , $toEmailId, $template , $userData , $planData = null, $url = null)
|
||||||
// {
|
// {
|
||||||
// try {
|
// try {
|
||||||
@ -400,15 +400,142 @@ 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();
|
||||||
|
|
||||||
|
// // 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'];
|
||||||
|
// }
|
||||||
|
|
||||||
|
// // 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']);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// // 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']);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// // 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']);
|
||||||
|
|
||||||
|
// // 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)) {
|
||||||
|
|
||||||
|
// // $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}");
|
||||||
|
// $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()];
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
|
||||||
|
// }
|
||||||
|
|
||||||
|
|
||||||
|
// zoho api service
|
||||||
if (!function_exists('send_email')) {
|
if (!function_exists('send_email')) {
|
||||||
function send_email($org_id, $toEmailId, $template, $userData, $planData = null, $url = null)
|
function send_email($org_id, $toEmailId, $template, $userData, $planData = null, $url = null)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
// Load models
|
|
||||||
$organizationModel = new \App\Models\OrganizationModel();
|
$organizationModel = new \App\Models\OrganizationModel();
|
||||||
$mailTemplateModel = new \App\Models\MailTemplateModel();
|
$mailTemplateModel = new \App\Models\MailTemplateModel();
|
||||||
|
|
||||||
// Fetch organization & template
|
|
||||||
$orgData = $organizationModel->where('org_id', $org_id)->first();
|
$orgData = $organizationModel->where('org_id', $org_id)->first();
|
||||||
$templateData = $mailTemplateModel->where('org_id', $org_id)->where('template_name', $template)->first();
|
$templateData = $mailTemplateModel->where('org_id', $org_id)->where('template_name', $template)->first();
|
||||||
|
|
||||||
@ -417,87 +544,100 @@ if (!function_exists('send_email')) {
|
|||||||
return ['status' => 'failed', 'code' => 404, 'message' => 'Missing organization or template data'];
|
return ['status' => 'failed', 'code' => 404, 'message' => 'Missing organization or template data'];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Replace placeholders from user data
|
// Replace placeholders
|
||||||
foreach ($userData as $key => $value) {
|
foreach ($userData as $key => $value) {
|
||||||
$templateData['body_html'] = str_replace('%' . $key . '%', $value ?? '', $templateData['body_html']);
|
$templateData['body_html'] = str_replace('%' . $key . '%', $value ?? '', $templateData['body_html']);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Replace placeholders from plan data
|
|
||||||
if (!empty($planData)) {
|
if (!empty($planData)) {
|
||||||
foreach ($planData as $key => $value) {
|
foreach ($planData as $key => $value) {
|
||||||
$templateData['body_html'] = str_replace('%' . $key . '%', $value ?? '', $templateData['body_html']);
|
$templateData['body_html'] = str_replace('%' . $key . '%', $value ?? '', $templateData['body_html']);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Exceptional reason block
|
|
||||||
if (!empty($planData['exceptional_plan_reason'])) {
|
if (!empty($planData['exceptional_plan_reason'])) {
|
||||||
$reasonHtml = '
|
$templateData['body_html'] .= '
|
||||||
<table style="width:100%; border-collapse:collapse; font-family:Arial, sans-serif;">
|
<table style="width:100%; border-collapse:collapse; font-family:Arial, sans-serif;">
|
||||||
<tr>
|
<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;">
|
<td style="text-align:left;background-color:#ffdbdb;color:#960000;padding:7px 15px;font-weight:bold;">
|
||||||
Reason for travel policy exception
|
Reason for travel policy exception
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<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;">
|
<td style="text-align:left;background-color:#fff5f5;color:#960000ab;padding:10px 15px;">
|
||||||
' . esc($planData['exceptional_plan_reason']) . '
|
' . esc($planData['exceptional_plan_reason']) . '
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
</table>';
|
</table>';
|
||||||
$templateData['body_html'] .= $reasonHtml;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add review URL if present
|
|
||||||
if ($url !== null) {
|
if ($url !== null) {
|
||||||
$templateData['body_html'] = str_replace('%trip_review_link%', $url, $templateData['body_html']);
|
$templateData['body_html'] = str_replace('%trip_review_link%', $url, $templateData['body_html']);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialize email
|
// Set up ZeptoMail API request
|
||||||
$email = \Config\Services::email(false);
|
$endpoint = "https://api.zeptomail.com/v1.1/email";
|
||||||
$email->initialize([
|
$apiKey = env('ZOHO_API_KEY');
|
||||||
'protocol' => 'smtp',
|
$sender = env('ZOHO_SENDER_MAIL');
|
||||||
'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,
|
|
||||||
]);
|
|
||||||
|
|
||||||
$email->setFrom($orgData['sender_email'], $orgData['name'] ?? 'TripApprovalTool');
|
$attachments = [];
|
||||||
$email->setTo($toEmailId);
|
|
||||||
$email->setSubject($templateData['subject']);
|
|
||||||
$email->setMessage($templateData['body_html']);
|
|
||||||
|
|
||||||
// Attach file if exists
|
$filePath = WRITEPATH . 'pdf/trips/trip_' . $planData['plan_id'] . '.pdf';
|
||||||
// if (!empty($planData['pdf_file_path']) && file_exists($planData['pdf_file_path'])) {
|
if (!empty($filePath) && file_exists($filePath) && is_readable($filePath)) {
|
||||||
// $email->attach($planData['pdf_file_path']);
|
$attachments[] = [
|
||||||
// }
|
"content" => base64_encode(file_get_contents($filePath)),
|
||||||
|
"mime_type" => "application/pdf",
|
||||||
if (!empty($planData['pdf_file_path']) && file_exists($planData['pdf_file_path']) && is_readable($planData['pdf_file_path'])) {
|
"name" => basename($filePath)
|
||||||
$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']}");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$payload = [
|
||||||
|
"from" => [
|
||||||
|
"address" => $sender,
|
||||||
|
"name" => $orgData['name'] ?? "TripApprovalTool"
|
||||||
|
],
|
||||||
|
"to" => [
|
||||||
|
["email_address" => ["address" => $toEmailId]]
|
||||||
|
],
|
||||||
|
"subject" => $templateData['subject'],
|
||||||
|
"htmlbody" => $templateData['body_html'],
|
||||||
|
];
|
||||||
|
|
||||||
// Send email
|
if (!empty($attachments)) {
|
||||||
if ($email->send()) {
|
$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}");
|
log_message('info', "[EMAIL SENT] To: {$toEmailId}, Template: {$template}");
|
||||||
return ['status' => 'success', 'code' => 200, 'message' => 'Email sent successfully', 'data' => $toEmailId];
|
return ['status' => 'success', 'code' => 200, 'message' => 'Email sent successfully', 'data' => $toEmailId];
|
||||||
} else {
|
} else {
|
||||||
// Log full debugger info
|
log_message('error', "[EMAIL FAILED] HTTP: {$httpCode}, Response: {$response}");
|
||||||
$debug = $email->printDebugger(['headers', 'subject', 'body']);
|
return ['status' => 'failed', 'code' => $httpCode, 'message' => 'Email sending failed', 'response' => $response];
|
||||||
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)];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (\Throwable $e) {
|
} catch (\Throwable $e) {
|
||||||
@ -507,8 +647,6 @@ if (!function_exists('send_email')) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!function_exists('send_email_agent')) {
|
if (!function_exists('send_email_agent')) {
|
||||||
function send_email_agent($org_id , $plan_id)
|
function send_email_agent($org_id , $plan_id)
|
||||||
{
|
{
|
||||||
|
|||||||
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