We can use Advanced Custom Fields (ACF) to create email sending form in WordPress Admin panel easily. This can be useful if you want to create an invitation system or manually send emails to your users or others.
We will use wp_mail
function with acf/save_post
action hook. So when we click the update button, it will send an email.
Note: This is not a step-by-step tutorial. This is an idea of how you can code it easily.
We will first create a new admin page named Send Email, and then assign our ACF form to this new admin page. Custom admin page only works with ACF PRO. So if you are using the free version, you can include the form in any other available pages.
We create some basic email form fields: from name, from email, recipient (repeater – name and email address), email subject and email message.
This is how it looks (click on image to enlarge):

Now that our form is ready, we will handle our form submit.
function careless_send_email_with_acf( $post_id ) {
//get current screen - to identify "Send Email" option page
$screen = get_current_screen();
//this must work only for "Send Email" option page - check against "Send Email" option page id
if ( $post_id !== 'options' || $screen->id !== 'email-settings_page_send_email_invite' ) {
return;
}
/*** process mail header ****/
//define header
$headers = array();
//enable html
$headers[] = 'MIME-Version: 1.0';
$headers[] = "Content-type: text/html; charset=" . get_bloginfo('charset');
//from name
$from_name = get_field( 'careless_email_from_name', 'options' );
//from email
$from_email = get_field( 'careless_from_email', 'options' );
//email subject
$subject = get_field( 'careless_email_subject', 'options' );
//email body
$message = do_shortcode( get_field( 'careless_email_message', 'options' ) );
//from
$headers[] = 'From: ' . $from_name . ' <' . $from_email . '>';
//email recipients - to
$send = false;
//recipients
$recipients = get_field( 'careless_email_recipient', 'options' );
foreach ( $recipients as $recipient ) {
$to = $recipient['name'] . ' <' . $recipient['email'] . '>';
$send = wp_mail( $to, $subject, $message, $headers );
}
//remove recipient data after email was sent
if ( $send ) {
delete_field( 'careless_email_recipient', 'options' );
} else {//could not send email
wp_die( 'Could not send email! Please try again later.' );
}
}
add_action( 'acf/save_post', 'careless_send_email_with_acf', 20 );
In above code, we have avoided running this for every acf/save_post
action call. We have checked screen id for our specific form page, with the help of get_current_screen
function on line 2-8. We can check detailed current screen data using following snippet (make sure to remove this code after you get your necessary information):
function careless_check_current_screen() {
print_r( get_current_screen() );
exit();
}
add_action( 'current_screen', 'careless_check_current_screen' );
On line 45, we have deleted the recipient field data when email was sent successfully using delete_field
. This shows how easily we can delete ACF field data on save.
Code goes in the functions.php
file of your active theme.