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);
}
}
}