Often, developers need to send custom emails in WordPress when they have custom forms with different input fields. So when they submit a custom form, then they want to send a mail with all form details. So without wasting time, let’s see how to do it.
An Ideal Place to Add Code in WordPress
Most Important: Add the following code to your child theme’s functions.php file. If you add custom code directly to your parent theme’s functions.php file then it will be wiped entirely when you update the theme.
If you are using a custom theme and if it doesn’t require any update then you can directly place the code into wp-content/themes/your-theme/function.php file.
Please also note that we have not tested all the presented codes here.
Send Custom Email With wp_mail Function in WordPress
The wp_mail() is WordPress’ inbuilt function which is used to sends an email, similar to PHP’s mail function.
The wp_mail() will return false if any errors occurred otherwise return true. A true
return value does not mean that the user received the email successfully. But it just only means that the method used was able to process the request without any errors.
Syntax:
wp_mail( $to, $subject, $message, $headers, $attachments );
01 Sending Simple Emails Using wp_mail In WordPress
Let’s create a re-usable function which then can be used anywhere in any file.
/*@ Mail function */
if( !function_exists('sc_wp_mail') ):
function sc_wp_mail($from, $to, $subject, $template){
$headers[] = 'From: '. $from . "\r\n";
$headers[] ='Reply-To: ' . $from . "\r\n";
$headers[] = 'Content-Type: text/html; charset=UTF-8';
$is_mail_sent = wp_mail($to, $subject, $template, $headers);
return $is_mail_sent;
}
endif;
Now, you can use sc_wp_mail()
function anywhere you like and pass the parameters.
USAGE:
We are presuming that you are submitting a custom form with some fields and you wanted to send an email with those data. So we will use the above function like:
$to = 'admin@example.com';
$name = ( isset( $_POST['name'] ) ) ? sanitize_text_field( $_POST['name'] ) : 'Site name';
$email = ( isset( $_POST['email'] ) ) ? sanitize_email( $_POST['email'] ) : 'john.doe@example.com';
$from = $name.' '.$email; // Combine name & email
$subject = 'Mail Subject';
$mail_template = 'You have a new message from ScratchCode';
sc_wp_mail( $from, $to, $subject, $mail_template );
02 Sending Emails With Bcc and Cc Using wp_mail In WordPress
Use the following function if you would like to use Bcc & Cc functionality.
/*@ Mail function with CC & BCC support in WordPress */
if( !function_exists('sc_wp_mail') ):
function sc_wp_mail($from, $to, $subject, $template, $cc = null, $bcc = null){
$headers[] = 'From: '. $from . "\r\n";
$headers[] = 'Reply-To: ' . $from . "\r\n";
if (!empty($cc)) :
if (is_array($cc)) :
foreach ($cc as $key => $value) :
$headers[] = 'Cc: ' . $value . "\r\n";
endforeach;
else:
$headers[] = 'Cc: ' . $cc . "\r\n";
endif;
endif;
if (!empty($bcc)) :
if (is_array($bcc)) :
foreach ($bcc as $key => $value) :
$headers[] = 'Bcc: ' . $value . "\r\n";
endforeach;
else:
$headers[] = 'Bcc: ' . $bcc . "\r\n";
endif;
endif;
$headers[] = 'Content-Type: text/html; charset=UTF-8';
$is_mail_sent = wp_mail($to, $subject, $template, $headers);
return $is_mail_sent;
}
endif;
USAGE:
We are presuming that you are submitting a custom form with some fields and you wanted to send an email with those data. So we will use the above function like:
$to = 'admin@example.com';
$name = ( isset( $_POST['name'] ) ) ? sanitize_text_field( $_POST['name'] ) : 'Site name';
$email = ( isset( $_POST['email'] ) ) ? sanitize_email( $_POST['email'] ) : 'john.doe@example.com';
$bcc = ( isset( $_POST['bcc'] ) ) ? $_POST['bcc'] : 'john.doe@example.com';
$cc = ( isset( $_POST['cc'] ) ) ? $_POST['cc'] : 'john.doe@example.com';
$from = $name.' '.$email; // Combine name & email
$subject = 'Mail Subject';
$mail_template = 'You have a new message from ScratchCode';
sc_wp_mail( $from, $to, $subject, $mail_template, $cc, $bcc );
Now, you can use sc_wp_mail()
function anywhere you like and pass the parameters as shown above usage example. Also, note that above Bcc and Cc will work with the array as well as a single value. For array, your input name should be like <input type="email" name="bcc[]">
and for Cc input name should be like <input type="email" name="cc[]">
.
For single Bcc and Cc, you can use <input type="email" name="bcc">
and <input type="email" name="cc">
respectively.
03 Sending Emails With Attachments In WordPress
Use the following function if you would like to send attachments with the emails.
/*@ Mail function */
if( !function_exists('sc_wp_mail') ):
function sc_wp_mail($from, $to, $subject, $template, $attachments=null){
$headers[] = 'From: '. $from . "\r\n";
$headers[] ='Reply-To: ' . $from . "\r\n";
$headers[] = 'Content-Type: text/html; charset=UTF-8';
$is_mail_sent = wp_mail($to, $subject, $template, $headers, $attachments);
return $is_mail_sent;
}
endif;
USAGE:
We are presuming that you are submitting a custom form with some fields and you wanted to send an email with those data. So we will use the above function like:
$to = 'admin@example.com';
$name = ( isset( $_POST['name'] ) ) ? sanitize_text_field( $_POST['name'] ) : 'Site name';
$email = ( isset( $_POST['email'] ) ) ? sanitize_email( $_POST['email'] ) : 'john.doe@example.com';
$from = $name.' '.$email; // Combine name & email
$subject = 'Mail Subject';
$mail_template = 'You have a new message from ScratchCode';
/*@ Handle Attachment Upload */
if ( ! function_exists( 'wp_handle_upload' ) ) {
require_once( ABSPATH . 'wp-admin/includes/file.php' );
}
$uploadedfile = $_FILES['file'];
$upload_overrides = array( 'test_form' => false );
$movefile = wp_handle_upload( $uploadedfile, $upload_overrides );
if ( !empty($movefile) ) :
$attachments = $movefile[ 'file' ];
//$attachment = array(WP_CONTENT_DIR . '/uploads/file_to_attach.zip'); // For static attachment
sc_wp_mail( $from, $to, $subject, $mail_template, $attachments );
endif;
Now, you can use sc_wp_mail()
function anywhere you like and pass the parameters as shown above usage example. You attachment form input should be like <input type="file" name="file">
.
If you don’t want to use form input for attachment and if you would like to send static attachment then you can use line no. 29 code and remove all media upload code.
04 Sending Emails With All Together (Bcc, Cc, Attachment) in WordPress
Here, we have created all the Bcc, Cc, and Attachment together into one single function. Also, we have made Bcc, Cc, and Attachment is optional so you can also submit your form without or with these fields.
/*@ Mail function with CC, BCC and Attachments in WordPress */
if( !function_exists('sc_wp_mail') ):
function sc_wp_mail($from, $to, $subject, $template, $cc = null, $bcc = null, $attachments = null){
$headers[] = 'From: '. $from . "\r\n";
$headers[] = 'Reply-To: ' . $from . "\r\n";
if (!empty($cc)):
foreach ($cc as $key => $value) :
$headers[] = 'Cc: ' . $value . "\r\n";
endforeach;
endif;
if (!empty($bcc)):
foreach ($bcc as $key => $value) :
$headers[] = 'Bcc: ' . $value . "\r\n";
endforeach;
endif;
$headers[] = 'Content-Type: text/html; charset=UTF-8';
$is_mail_sent = wp_mail($to, $subject, $template, $headers, $attachments);
return $is_mail_sent;
}
endif;
That’s all for the day. We hope this article helped you to send custom emails in WordPress.
Additionally, read our guide on How to Setup SMTP in WordPress Without Any Plugins
Please let us know in the comments if everything worked as expected, your issues, or any questions. If you think this article saved your time & money, please do comment, share, like & subscribe. Thank you in advance 🙂 Keep Smiling! Happy Coding!
That is a very good tip especially to those fresh to the blogosphere. Clemmie Galen Radburn
Utterly indited written content , regards for information. Caprice Woody Aprilette
This is my first time go to see at here and i am in fact pleassant to read all at one place. Aida Grantham Ratib
Thanks-a-mundo for the blog article. Really looking forward to read more. Great. Chickie Darnall Emlen
Everyone loves it whenever people get together and share ideas. Great site, stick with it. Dulcine Woodman Loralyn
I pay a visit everyday a few web pages and sites to read articles or reviews, however this blog offers feature based posts. Amandy Stearne Fogel
Im searching on the web trying to find just how to begin this blog site thing as well as your website is nearly certainly really impressive. Henryetta Fidel Brandwein
Thanks for sharing your info. I truly appreciate your efforts and I will be waiting for your further post thanks once again. Celisse Haley Ilbert
I am perpetually thought about this, thanks for putting up. Nickie Reid Katharyn
Hey Indir, We are glad that you are liking our posts. Thank you 🙂
This is an excellent, an eye-opener for sure! You are obviously very knowledgeable. Some nice points there. This information is magnificent. Indira Benyamin Raviv
Hey Bluray, Thank you for your kind word. Keep visiting our blog for more articles.
You made some decent points there. I did a search on the issue and found most individuals will approve with your blog. Gussie Benedict Rudin
We are glad that you resolved your issue. Thank you 🙂
I think the admin of this site is truly working hard for his site, because here every information is quality based stuff. Ros Brandtr Jenine
Enjoyed examining this, very good stuff, regards . “I will do my best. That is all I can do. I ask for your help-and God’s.” by Lyndon B. Johnson. Torrie Dare Pagas
Thank you 🙂
Hi!
I have a WordPress website and i apply some custom php code.
Actually i created a random code generator button is only work when user is logged-in into wordpress , when user click on button some codes are appearing in ..
Now i want to send an email of codes to user.
Please Help me
Many Thanks
Hey Talha, You can just send the generated code into the email body.