Amit

How to send email to users selecting username in wordpress without plugin.

Here is the full code

<div class="row">
	<?php if(isset($_POST['indvisual_submit'])) {
	     $ind_mail = $_POST['indvisual_mail'];
	     $ind_message = $_POST['indvisual_message'];
	     $to = $ind_mail;
	     $admin_email = get_option( 'admin_email' );
	     $subject = 'Admin Message';
	     $body = '<p><strong>Message:</strong> '.$ind_message.'</p> ';
	     $headers = array('Content-Type: text/html; charset=UTF-8','From: '.$admin_email .'');
	     $sent =  wp_mail( $to, $subject, $body, $headers );
	     if($sent) {
	     echo "<h2>Email Successfully Sent</h2>";
	     } else  {
	     echo "<h2>Failed</h2>";
	     }
	} ?>
	 <form method="post" class="bulk-form mt-3 p-4">
         <div class="form-group">
             <select name="indvisual_mail" class="form-control" id="" placeholder="Pick a username...">
                 <?php 
                     // WordPress get all user emails except admin
                     $args = array('orderby' => 'display_name', 'role__not_in' => ['administrator'] );
                     $wp_user_query = new WP_User_Query($args);
                     $authors = $wp_user_query->get_results();
                     if (!empty($authors)) {
                         foreach ($authors as $author) {
                             $author_info = get_userdata($author->ID);
                             echo '<option value="' . $author_info->user_email . '">' . $author_info->display_name . '</option>';
                         }
                     } else {
                         echo 'No results';
                 } ?>
             </select>
         </div>
         <div class="form-group">
             <textarea name="indvisual_message" class="form-control" id="" cols="30" rows="5" placeholder="Type You Message"></textarea>
         </div>
         <div class="form-group">
             <input class="btn btn-block btn-primary" name="indvisual_submit" type="submit" value="Send">
         </div>
	 </form>
</div>

Last Update on:March 5th, 2023 at 5:19 pm


How to send bulk email to all users selecting user roles in wordpress without plugin
Previous post