Sending Emails with Custom Email Template

Hi,
How can I send emails with custom templates ?
I created a file and named it “ChangePatientPassword.en-US.php” and placed it inside the language folder.

template file content:

Subject: Password Changed
From: <?= $From ?>
To: <?= $To ?>
Cc:
Bcc:
Format: HTML

<p>Dear Sir/Madam,</p>

<p>Your password has been changed.</p>
<p>Your new password is <?= $Password ?></p>

<p>Please feel free to contact us in case of further queries.</p>

<p>
Best Regards,<br>
Support
</p>

my code to send email:

	$rndPass = "123456";
        Config("EMAIL_PATIENT_CHANGE_PASSWORD_TEMPLATE", "ChangePatientPassword.php"); // Set 
        $email = new Email();
                    $email->load(Config("EMAIL_PATIENT_CHANGE_PASSWORD_TEMPLATE"), data: [
                        "From" => Config("SENDER_EMAIL"), // Replace Sender
                        "To" => $row["EmailAddress"], // Replace Recipient
                        "Password" => $rndPass
                    ]);  
        $email->send();

the error message:
C:\xampp\htdocs\telehealth\vendor\dflydev\dot-access-data\src\Data.php(132): No data exists at the given path: "EMAIL_PATIENT_CHANGE_PASSWORD_TEMPLATE"What’s wrong?
Thanks

Hi,
I used this code and problem fixed.

	if (!is_null($row['EmailAddress']) && $row['EmailAddress'] != ""){		
	$email = new Email();
	$email->load('ChangePatientPassword.php', data: [
	"From" => Config("SENDER_EMAIL"), // Replace Sender
	"To" => $row['EmailAddress'], // Replace Recipient
	"Password" => $rndPass
	]);	
	$email->send();
	}

Thanks

Hi,
I used mentioned codes in a custom file (without header/footer) to send email. I received the following error message. I used suggested codes in the following post in my custom page.
https://discourse.hkvstore.com/t/how-to-load-data-from-database-into-custom-files-v2024/8911/1 code to send email:

	//send email
	if (!is_null($_POST['EmailAddress']) && $_POST['EmailAddress'] != ""){
	//end Email		
	$email = new Email();
	$email->load('ChangePatientPassword.php', data: [
	"From" => Config("SENDER_EMAIL"), // Replace Sender
	"To" => $_POST['EmailAddress'], // Replace Recipient
	"Password" => $_POST['SecCode']
	]);	
	$email->send();

Error Message:Fatal error: Uncaught Error: Call to a member function fetchTemplate() on null in C:\xampp\htdocs\telehealth\src\Email.php:70 Stack trace: #0 C:\xampp\htdocs\telehealth\savemyinformation.php(60): PHPMaker2024\TELEHEALTH\Email->load(‘ChangePatientPa…’, ‘’, Array) #1 {main} thrown in C:\xampp\htdocs\telehealth\src\Email.php on line 70Thanks

Hi,
My code to send email in a custom file without header/footer and the error message:

	//send email
	if (!is_null(Post('EmailAddress')) && Post('EmailAddress') != ""){	
	$email = new Email();
	$email->load('NewPatient.php', Post('LanguageID'), data: [
	"From" => Config("SENDER_EMAIL"), // Replace Sender
	"To" => Post('EmailAddress'), // Replace Recipient
	"Password" => Post('SecCode')
	]);	
	$email->send();
	}
    //end Email

Error:Fatal error: Uncaught Error: Call to a member function fetchTemplate() on null in C:\xampp\htdocs\telehealth\src\Email.php:70 Stack trace: #0 C:\xampp\htdocs\telehealth\savemyinformation.php(58): PHPMaker2024\TELEHEALTH\Email->load(‘NewPatient.php’, ‘fa-IR’, Array) #1 {main} thrown in C:\xampp\htdocs\telehealth\src\Email.php on line 70What’s wrong?
Thanks

What is NewPatient.php? Is it a Custom File? And in which folder did you put that file?

Hi,It’s the language file. I provided it for both English and Persian. I placed them inside the language folder.I tested the code in another custom file with header/footer and it works fine.

I used the file structure in my custom file (without header/footer), as suggested by “mobhar” in the following post.
https://discourse.hkvstore.com/t/how-to-load-data-from-database-into-custom-files-v2024/8911/1 idea?

If you use your own file without using the container, some code won’t work. You may refer to the index.php of the project and copy code to create the container, e.g.

$containerBuilder = new ContainerBuilder();
$containerBuilder->addDefinitions("src/definitions.php");
$container = $containerBuilder->build();

That’s why you should avoid the old way of using standalone files, nowadays it is better to treat your application as an integrated app, not a group of standaone files. Avoid accessing standalone files by xxx.php in the URL. You may want to read Using Route_Action server event.

Hi,
Thank you. it works.
adding the following codes at the begining of custom file solved the Email problem.

$containerBuilder = new ContainerBuilder();
$containerBuilder->addDefinitions("src/definitions.php");
$container = $containerBuilder->build();

Thanks