Send an email after submitting the form


This tutorial will show you how to submit an email after submitting the form.

  1. Edit your map
  2. In the widget “Submit locations settings”, click on the menu “Add/Edit custom fields”.
  3. Create a new custom field where users can add their email address. To do that:
    1. Click on the button “Add new custom field”.
    2. In the field “Name”, enter the custom field name (for example: “_email_address”).
    3. In the field “Label”, enter the field title (for example, “Your Email”).
    4. In the field “Display location (Tabs)”, select the tab where you want to display the field.
    5. In the field “Visibility”, select the option “Activate (visible)”.
    6. In the field “Display type”, select the option “Text email”.
    7. Save your changes.
  4. Now, in the file “functions.php” of your theme/child theme, paste the following PHP code:
/**
 * "Progress Map, Submit Locations".
 * 
 * This will send an email after submitting the form:
 * 
 * The first argument is the inserted (new) post ID.
 * The second argument is the content of "$_POST".
 * The thrid argument is the post data (author, status, post type, title, content & excerpt).
 * The fourth argument is the sanitized values of our custom fields & taxonomies.
 * 
 */
function cspmsl_send_email_after_submit($inserted_post_id, $posted_fields, $post_data, $sanitized_values){
	
	/**
	 * [@email_field_name] This is the name you've used in the field "Name" while creating ...
	 * ... your email custom field. Using another name won't work */
	 
	$email_field_name = '_email_address';
	
	/**
	 * Get out if there's no email */
	 
	if(!isset($posted_fields[$email_field_name]))
		return;
	
	/**
	 * 1. Get the administrator email */
	 	
	$admin_email = get_bloginfo('admin_email');
	
	/**
	 * 2. The user email. The one to wich you want to send the email */
		 
	$send_to = $posted_fields[$email_field_name]; 
	
	/**
	 * 3. Send the email */
	 
	if((!empty($send_to) && is_email($send_to)) && (!empty($admin_email) && is_email($admin_email))){
		
		/** 
		 * [@subject] The subject of your email.
		 * Feel free to change it! */
		 
		$subject = 'Your subject';
		
		/** 
		 * [@body] The content of your email.
		 * Feel free to change it! */
		 		
		$body  = 'Your message 1.';
		$body .= "\r\n"; // This is a line break.
		$body .= 'Your message 2.';
		
		/** 
		 * [@blogname] The name of your website.
		 * Feel free to change it! */
		 		
		$blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
		
		/** 
		 * [@headers] The headers of your email */
		 				
		$headers = sprintf( 'From: "%1$s" <%2$s>', $blogname, $admin_email ) . "\r\n" . sprintf( 'Reply-To: <%s>', $send_to );
		
		wp_mail($send_to, $subject, $body, $headers);
		
	}

}
add_filter('cspmsl_after_save_fields', 'cspmsl_send_email_after_submit', 10, 4);

Add “Cc:” and/or “Bcc:” recipients

To set the “From:” email address to something other than the default sender, or to add “Cc:” and/or “Bcc:” recipients, refer to the above PHP code.

Change the line 64

$headers = sprintf( 'From: "%1$s" <%2$s>', $blogname, $admin_email ) . "\r\n" . sprintf( 'Reply-To: <%s>', $send_to );

To

$headers[] = sprintf( 'From: "%1$s" <%2$s>', $blogname, $admin_email );
$headers[] = 'Cc: John Q Codex <jqc@wordpress.org>'; // Email 1
$headers[] = 'Cc: Will J Codex <wjc@wordpress.org>'; // Email 2...
$headers[] = 'Bcc: iluvwp@wordpress.org'; // You can also use a simple email address
$headers[] = sprintf( 'Reply-To: <%s>', $send_to );