Home » How To Send Custom Emails in WordPress

How To Send Custom Emails in WordPress

Last updated on July 5, 2020 by

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.

Table of Contents
1. Sending Simple Emails Using wp_mail In WordPress
2. Sending Emails With Bcc and Cc Using wp_mail In WordPress
3. Sending Emails With Attachments In WordPress
4. Sending Emails With All Together (Bcc, Cc, Attachment) in WordPress

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 = '[email protected]';

$name = ( isset( $_POST['name'] ) ) ? sanitize_text_field( $_POST['name'] ) : 'Site name';

$email = ( isset( $_POST['email'] ) ) ? sanitize_email( $_POST['email'] ) : '[email protected]';

$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 = '[email protected]';

$name = ( isset( $_POST['name'] ) ) ? sanitize_text_field( $_POST['name'] ) : 'Site name';

$email = ( isset( $_POST['email'] ) ) ? sanitize_email( $_POST['email'] ) : '[email protected]';

$bcc = ( isset( $_POST['bcc'] ) ) ? $_POST['bcc'] : '[email protected]';

$cc = ( isset( $_POST['cc'] ) ) ? $_POST['cc'] : '[email protected]';

$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 = '[email protected]';

$name = ( isset( $_POST['name'] ) ) ? sanitize_text_field( $_POST['name'] ) : 'Site name';

$email = ( isset( $_POST['email'] ) ) ? sanitize_email( $_POST['email'] ) : '[email protected]';

$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!

 
 

Leave a Comment