Updating pdf attachment logic

This commit is contained in:
Finley Ghosh 2025-12-05 22:17:38 +11:00
parent d898149810
commit 46cf098dfa

View file

@ -4,6 +4,8 @@ import (
"context" "context"
"database/sql" "database/sql"
"fmt" "fmt"
"io"
"log"
"net/http" "net/http"
"os" "os"
"strconv" "strconv"
@ -502,10 +504,20 @@ func (h *QuotesHandler) SendQuoteReminderEmailWithAttachment(ctx context.Context
return fmt.Errorf("invalid reminder type: %v", reminderType) return fmt.Errorf("invalid reminder type: %v", reminderType)
} }
// Check if PDF exists and prepare attachment if found // Download PDF from URL and prepare attachment if available
var attachments []interface{} var attachments []interface{}
if _, err := os.Stat(pdfPath); err == nil { resp, err := http.Get(pdfPath)
// PDF exists, attach it if err == nil && resp.StatusCode == 200 {
defer resp.Body.Close()
// Create temporary file for the PDF
tmpFile, err := os.CreateTemp("", "quote_*.pdf")
if err == nil {
defer os.Remove(tmpFile.Name())
defer tmpFile.Close()
// Copy PDF content to temp file
if _, err := io.Copy(tmpFile, resp.Body); err == nil {
type Attachment struct { type Attachment struct {
Filename string Filename string
FilePath string FilePath string
@ -513,14 +525,25 @@ func (h *QuotesHandler) SendQuoteReminderEmailWithAttachment(ctx context.Context
attachments = []interface{}{ attachments = []interface{}{
Attachment{ Attachment{
Filename: pdfFilename, Filename: pdfFilename,
FilePath: pdfPath, FilePath: tmpFile.Name(),
}, },
} }
} else {
log.Printf("Failed to copy PDF content for quote %d: %v", quoteID, err)
} }
// If PDF doesn't exist, just send email without attachment } else {
log.Printf("Failed to create temporary file for quote %d PDF: %v", quoteID, err)
}
} else if err != nil {
log.Printf("Failed to download PDF from %s for quote %d: %v", pdfPath, quoteID, err)
} else if resp != nil {
defer resp.Body.Close()
log.Printf("PDF download returned status %d for quote %d at %s", resp.StatusCode, quoteID, pdfPath)
}
// If PDF download fails, just send email without attachment
// Send the email (with or without attachment) // Send the email (with or without attachment)
err := h.emailService.SendTemplateEmailWithAttachments( err = h.emailService.SendTemplateEmailWithAttachments(
recipient, recipient,
subject, subject,
templateName, templateName,
@ -617,8 +640,8 @@ func (h *QuotesHandler) SendManualReminder(w http.ResponseWriter, r *http.Reques
ccs = append(ccs, userEmail) ccs = append(ccs, userEmail)
} }
// Attach PDF quote // Attach PDF quote from URL
pdfPath := fmt.Sprintf("webroot/pdf/%d.pdf", quoteID) pdfURL := fmt.Sprintf("https://stg.cmctechnologies.com.au/pdf/%s.pdf", enquiryRef)
pdfFilename := fmt.Sprintf("%s.pdf", enquiryRef) pdfFilename := fmt.Sprintf("%s.pdf", enquiryRef)
// Get username from request // Get username from request
@ -636,7 +659,7 @@ func (h *QuotesHandler) SendManualReminder(w http.ResponseWriter, r *http.Reques
templateData, templateData,
ccs, ccs,
usernamePtr, usernamePtr,
pdfPath, pdfURL,
pdfFilename, pdfFilename,
) )