Email send after export to pdf (v2024)

Hi All,

I can’t seem to get this right, The email isn’t sending and I don’t get a Failed message.
have put this code into Page_Exported event. Help please.

function Page_Exported() {
    if ($this->Export == "pdf") {
        $filename = "ServiceDocket_" . date("Ymd_His") . ".pdf";
        $filepath = "tmp/" . $filename;
        file_put_contents($filepath, $this->ExportDoc->Text);

        $email = new \PHPMailer\PHPMailer\PHPMailer();

        try {
            $email->isSMTP();
            $email->Host       = 'smtp.example.com';
            $email->SMTPAuth   = true;
            $email->Username   = 'your_email@example.com';
            $email->Password   = 'your_password';
            $email->SMTPSecure = \PHPMailer\PHPMailer\PHPMailer::ENCRYPTION_STARTTLS;
            $email->Port       = 587;

            $email->setFrom('your_email@example.com', 'Your Name');
            $email->addAddress('recipient@example.com', 'Recipient Name');

            $email->addAttachment($filepath, $filename);

            $email->isHTML(true);
            $email->Subject = 'Service Docket PDF';
            $email->Body    = 'Please find the attached service docket PDF.';

            if ($email->send()) {
                WriteLog("Email sent successfully.");
                // SetSuccessMessage may not show because of PDF output
            } else {
                WriteLog("Email sending failed: " . $email->ErrorInfo);
            }

        } catch (Exception $e) {
            WriteLog("Email exception: " . $email->ErrorInfo);
        }

        if (file_exists($filepath)) {
            unlink($filepath);
        }
    }
}

Which version are you using? If you use v2025, note that:

  1. The signature of Page_Exported server event is:
function Page_Exported(object $doc): void

The export document is passed to the event as argument, don’t use $this->ExportDoc, there is no such property.

  1. If exporting to PDF, note that $doc->Text is HTML (before rendering as PDF), it is not the PDF document.

  2. There is no built-in WriteLog(). If it is not your own function, your code caused error.

  3. You don’t need to create PHPMailer instance yourself, you may use the global function SendEmail(). Read the notes and learn where you should put your attachment file.

  4. You better enable Debug and check the server log. If you use SendEmail(), PHPMailer error will also be logged.

Hi,

Sorry. It is V24.16

Many Thanks

All above applies to v2024 as well.