I have a basic API endpoint that receives the info and sends out 2 emails: a copy to the admin, and another copy to the client (they are different). What’s the best way to create an admin template, and a client template, and then populate with data (like user’s name, email, etc)?

[ad_1]

Hi

I have a basic API endpoint. When the user submits a form, the info is sent to the endpoint using JS, and after processing, I need to send an email to a specific email (for example, the admin), and another one to the client with more information.

At the moment, I have to hardcode everything into HTML strings and then have 2 different wp\_mail functions. A simplified version of my code looks like this:

// after all sanitation and validation (above)

$receivers = [“[email protected]”, “[email protected]”];

$headers[] = ‘Content-Type: text/html; charset=UTF-8’;

$subject_admin = “Form Submission: {$data[ ‘name’ ]} – {$data[ ’email’ ]}”;
$subject_client = “Thank you for filling out the form”;

$message_admin = “”;
$message_admin .= “ADMIN MESSAGE CONTENT ……”;

$message_client = “”;
$message_client .= “CLIENT MESSAGE CONTENT …..”;

try {
// ADMIN email
wp_mail( $receivers, $subject_admin, $message_admin, $headers );
} catch ( Exepction $e ) {
// Error handling
}

try {
// CLIENT email
wp_mail( $data[ ’email’ ], $subject_client, $message_client, $headers );
} catch ( Exepction $e ) {
// Error handling
}

// Send JSON success email whether emails are sent or not
wp_send_json_success();

This seems very clunky, especially that the client and admin emails are much longer than this with a few conditions in them.

I was thinking about making a template file and then including them, but I’m not sure if that’s the best way.

Any idea is welcome.

Thanks

[ad_2]

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer