Trigger email when appointment is booked by p[atient

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:

https://www.open-emr.org/wiki/index.php/Patient_Use_of_the_Patient_Portal_in_OpenEMR_v6%2B#Appointments

1 Like

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

:white_check_mark: Goal: Send email notification when a new appointment is booked

Let’s go through the steps to implement this:

Shape

:white_check_mark: 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:

:file_folder: interface/main/calendar/add_edit_event.php
or
:file_folder: interface/main/calendar/submit_event.php

Also check: :file_folder: library/appointments.inc.php – utility functions

Shape

:white_check_mark: 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.

:round_pushpin: 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

:white_check_mark: 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

:white_check_mark: 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

:white_check_mark: 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

:pushpin: 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