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"
"database/sql"
"fmt"
"io"
"log"
"net/http"
"os"
"strconv"
@ -502,25 +504,46 @@ func (h *QuotesHandler) SendQuoteReminderEmailWithAttachment(ctx context.Context
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{}
if _, err := os.Stat(pdfPath); err == nil {
// PDF exists, attach it
type Attachment struct {
Filename string
FilePath string
}
attachments = []interface{}{
Attachment{
Filename: pdfFilename,
FilePath: pdfPath,
},
resp, err := http.Get(pdfPath)
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 {
Filename string
FilePath string
}
attachments = []interface{}{
Attachment{
Filename: pdfFilename,
FilePath: tmpFile.Name(),
},
}
} else {
log.Printf("Failed to copy PDF content for quote %d: %v", quoteID, err)
}
} 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 doesn't exist, just send email without attachment
// If PDF download fails, just send email without attachment
// Send the email (with or without attachment)
err := h.emailService.SendTemplateEmailWithAttachments(
err = h.emailService.SendTemplateEmailWithAttachments(
recipient,
subject,
templateName,
@ -617,8 +640,8 @@ func (h *QuotesHandler) SendManualReminder(w http.ResponseWriter, r *http.Reques
ccs = append(ccs, userEmail)
}
// Attach PDF quote
pdfPath := fmt.Sprintf("webroot/pdf/%d.pdf", quoteID)
// Attach PDF quote from URL
pdfURL := fmt.Sprintf("https://stg.cmctechnologies.com.au/pdf/%s.pdf", enquiryRef)
pdfFilename := fmt.Sprintf("%s.pdf", enquiryRef)
// Get username from request
@ -636,7 +659,7 @@ func (h *QuotesHandler) SendManualReminder(w http.ResponseWriter, r *http.Reques
templateData,
ccs,
usernamePtr,
pdfPath,
pdfURL,
pdfFilename,
)