We have to Trigger email when appointment is booked by patient. Please guide me how we can achieve this with steps.
Hi @Vickey_Chavan ,
I believe appointment requests made by patients in the portal will be passed to clinic staff for approval . Please check this:
To trigger an email when an appointment is booked by a patient in OpenEMR, you will need to hook into the appointment creation logic and use PHPâs mail() function or a mailing library like PHPMailer. OpenEMR uses a combination of PHP backend and JavaScript/Knockout frontend, so weâll focus on the backend part that handles appointment creation.
Shape
Goal: Send email notification when a new appointment is booked
Letâs go through the steps to implement this:
Shape
Step 1: Find where the appointment is created
When a patient books an appointment through the portal or front desk, the backend stores the appointment in the database, typically via:
interface/main/calendar/add_edit_event.php
or
interface/main/calendar/submit_event.php
Also check: library/appointments.inc.php â utility functions
Shape
Step 2: Add email trigger in the backend
Inside submit_event.php, find the section where the appointment is successfully created. After that, add email logic.
Example code snippet in interface/main/calendar/submit_event.php:
// After appointment creation and database insertion
if ($new_event) {
// Compose email
$to = $patient_email; // Fetch from patient_data table
$subject = "Appointment Confirmation";
$message = "Dear $patient_name,\n\nYour appointment has been successfully scheduled on $date at $time.\n\nThank you!";
$headers = "From: clinic@example.com";
// Send email using mail()
mail($to, $subject, $message, $headers);
}
Shape
Step 3: Fetch patient email and appointment details
Use SQL to fetch patient details like:
$sql = âSELECT fname, lname, email FROM patient_data WHERE pid = ?â;
$res = sqlQuery($sql, array($patient_id));
$patient_email = $res[âemailâ];
$patient_name = $res[âfnameâ] . â â . $res[âlnameâ];
Shape
Step 4: Use PHPMailer for better email handling (optional)
OpenEMR already uses PHPMailer internally.
Example using PHPMailer:
use PHPMailer\PHPMailer\PHPMailer;
$mail = new PHPMailer();
$mail->isSMTP();
$mail->Host = âsmtp.example.comâ;
$mail->SMTPAuth = true;
$mail->Username = âyou@example.comâ;
$mail->Password = âyour_passwordâ;
$mail->SMTPSecure = âtlsâ;
$mail->Port = 587;
$mail->setFrom(âclinic@example.comâ, âClinic Nameâ);
$mail->addAddress($patient_email, $patient_name);
$mail->Subject = âAppointment Confirmationâ;
$mail->Body = âDear $patient_name,\n\nYour appointment is confirmed for $date at $time.â;
if (!$mail->send()) {
error_log('Mailer Error: ' . $mail->ErrorInfo);
}
Shape
Step 5: Testing
Book a test appointment from the calendar or portal.
Monitor the email logs.
Ensure proper configuration of:
php.ini SMTP settings (for mail())
SMTP credentials (for PHPMailer)
Shape
Common Errors You Might Face in OpenEMR
Let me know the specific error youâre seeing (you said: âsolve this error in OpenEMRâ), and Iâll solve it right away.
Please provide:
Error message text
File name where it occurs
Any stack trace or logs from OpenEMR
Shape
Would you like me to give you a plug-and-play function you can drop into your OpenEMR to test this now?
If you have any clarification from this. Get your professional free support