Adding some more comments for readability
This commit is contained in:
parent
7a752f2881
commit
d1acd64801
|
|
@ -1122,8 +1122,10 @@ EOT;
|
|||
* @param string|null $bcc - BCC email address(es), optional (comma-separated)
|
||||
*/
|
||||
function email_pdf_with_custom_recipients($id = null, $to = null, $cc = null, $bcc = null) {
|
||||
// Disable automatic rendering of a view
|
||||
$this->autoRender = false;
|
||||
|
||||
// Retrieve recipient emails from form data if not provided as arguments
|
||||
if (empty($to) && !empty($this->params['form']['to'])) {
|
||||
$to = $this->params['form']['to'];
|
||||
}
|
||||
|
|
@ -1134,16 +1136,18 @@ function email_pdf_with_custom_recipients($id = null, $to = null, $cc = null, $b
|
|||
$bcc = $this->params['form']['bcc'];
|
||||
}
|
||||
|
||||
// Basic validation
|
||||
// Basic validation for required parameters
|
||||
if (empty($id) || empty($to)) {
|
||||
$msg = 'Document ID and recipient email are required.';
|
||||
echo json_encode(array('success' => false, 'message' => $msg));
|
||||
return;
|
||||
}
|
||||
|
||||
// Configure SMTP settings for email delivery
|
||||
$this->Email->smtpOptions = Configure::read('smtp_settings');
|
||||
$this->Email->delivery = 'smtp';
|
||||
|
||||
// Load the document and its attachments
|
||||
$document = $this->Document->read(null, $id);
|
||||
error_log("[email_pdf_with_custom_recipients] Document loaded: " . print_r($document['Document'], true));
|
||||
error_log("[email_pdf_with_custom_recipients] DocumentAttachments: " . print_r($document['DocumentAttachment'], true));
|
||||
|
|
@ -1153,20 +1157,21 @@ function email_pdf_with_custom_recipients($id = null, $to = null, $cc = null, $b
|
|||
return;
|
||||
}
|
||||
|
||||
// Ensure the PDF has been generated before emailing
|
||||
if (empty($document['Document']['pdf_filename'])) {
|
||||
$msg = 'Error. Please generate the PDF before attempting to email it';
|
||||
echo json_encode(array('success' => false, 'message' => $msg));
|
||||
return;
|
||||
}
|
||||
|
||||
$pdf_dir = Configure::read('pdf_directory');
|
||||
$attachment_files = array($pdf_dir.$document['Document']['pdf_filename']);
|
||||
foreach($document['DocumentAttachment'] as $document_attachment) {
|
||||
// Build the list of attachments (PDF + any additional attachments)
|
||||
$pdf_dir = Configure::read('pdf_directory');
|
||||
$attachment_files = array($pdf_dir.$document['Document']['pdf_filename']);
|
||||
foreach($document['DocumentAttachment'] as $document_attachment) {
|
||||
$attachment = $this->Document->DocumentAttachment->Attachment->read(null, $document_attachment['attachment_id']);
|
||||
$attachment_files[] = $attachment['Attachment']['file'];
|
||||
error_log("[email_pdf_with_custom_recipients] Added attachment: " . $attachment['Attachment']['file']);
|
||||
}
|
||||
|
||||
}
|
||||
if (!empty($document['DocumentAttachment'])) {
|
||||
foreach ($document['DocumentAttachment'] as $document_attachment) {
|
||||
if (!empty($document_attachment['attachment_id'])) {
|
||||
|
|
@ -1181,33 +1186,38 @@ function email_pdf_with_custom_recipients($id = null, $to = null, $cc = null, $b
|
|||
error_log("[email_pdf_with_custom_recipients] All attachments: " . print_r($attachment_files, true));
|
||||
$this->Email->attachments = $attachment_files;
|
||||
|
||||
$enquiry = $this->Document->getEnquiry($document);
|
||||
// Get related enquiry for the document
|
||||
$enquiry = $this->Document->getEnquiry($document);
|
||||
|
||||
// Parse and validate recipient email addresses
|
||||
$toArray = $this->parse_email_to_array($to);
|
||||
if (empty($toArray)) {
|
||||
$msg = 'Invalid recipient email address.';
|
||||
echo json_encode(array('success' => false, 'message' => $msg));
|
||||
return;
|
||||
} else {
|
||||
$this->Email->to = implode(', ', $toArray);
|
||||
|
||||
}
|
||||
if (empty($toArray)) {
|
||||
$msg = 'Invalid recipient email address.';
|
||||
echo json_encode(array('success' => false, 'message' => $msg));
|
||||
return;
|
||||
} else {
|
||||
$this->Email->to = implode(', ', $toArray);
|
||||
}
|
||||
$ccArray = $this->parse_email_to_array($cc);
|
||||
if (!empty($ccArray)) {
|
||||
$this->Email->cc = $ccArray;
|
||||
}
|
||||
$bccArray = $this->parse_email_to_array($bcc);
|
||||
if (!empty($ccArray)) {
|
||||
$this->Email->cc = $ccArray;
|
||||
}
|
||||
$bccArray = $this->parse_email_to_array($bcc);
|
||||
if (!empty($bccArray)) {
|
||||
$this->Email->bcc = $bccArray;
|
||||
}
|
||||
$this->Email->bcc = $bccArray;
|
||||
}
|
||||
|
||||
// Set reply-to and from addresses
|
||||
$this->Email->replyTo = 'CMC Technologies - Sales <sales@cmctechnologies.com.au>';
|
||||
$this->Email->from = 'CMC Technologies - Sales <sales@cmctechnologies.com.au>';
|
||||
|
||||
// Determine document type, email template, and subject
|
||||
$docType = $this->Document->getDocType($document);
|
||||
$template = $docType . '_email';
|
||||
$subject = !empty($enquiry['Enquiry']['title']) ? $enquiry['Enquiry']['title'] . ' ' : 'Document';
|
||||
error_log("[email_pdf_with_custom_recipients] Enquiry Title: " . empty($enquiry['Enquiry']['title']) . $enquiry['Enquiry']['title']);
|
||||
error_log("[email_pdf_with_custom_recipients] Enquiry Title: " . empty($enquiry['Enquiry']['title']) . $enquiry['Enquiry']['title']);
|
||||
|
||||
// Customise subject and template based on document type
|
||||
switch($docType) {
|
||||
case 'quote':
|
||||
$subject = !empty($enquiry['Enquiry']['title']) ? "Quotation: " . $enquiry['Enquiry']['title'] : 'Quotation';
|
||||
|
|
@ -1251,6 +1261,7 @@ function email_pdf_with_custom_recipients($id = null, $to = null, $cc = null, $b
|
|||
break;
|
||||
}
|
||||
|
||||
// Set email template and other parameters
|
||||
$this->Email->template = $template;
|
||||
$this->Email->subject = $subject;
|
||||
$this->Email->sendAs = 'both';
|
||||
|
|
@ -1259,6 +1270,7 @@ function email_pdf_with_custom_recipients($id = null, $to = null, $cc = null, $b
|
|||
$this->set('document', $document);
|
||||
$this->set('DocFullName', $this->Document->getDocFullName($document['Document']['type']));
|
||||
|
||||
// Attempt to send the email and handle errors
|
||||
$sent = false;
|
||||
try {
|
||||
$sent = $this->Email->send();
|
||||
|
|
|
|||
Loading…
Reference in a new issue