I have certain instances where I want to send emails to both user and admin accordingly using wp_mail. The first instance is when a new user is created. The following code is used to create users:

//Get all users and if user exists get username and email or else create new user as support customer. 
$user = strstr($mail['header']->fromaddress, '<',true);
$email = $mail['header']->fromaddress;

for ($j=1; $j < $total; $j++) { 
    $mail = $emails-> get($j);

    add_post_meta($post_id, 'username', $email, True);  
    add_post_meta($post_id, 'email', $user, True);
         $userdata = array(
        'user_login'    => ucwords(strstr($mail['header']->fromaddress, '<',true)),
        'user_pass'     => '',
        'user_nicename' => ucwords(strstr($mail['header']->fromaddress, '<',true)),   
        'display_name'  => ucwords(strstr($mail['header']->fromaddress, '<',true)),
        'user_email'    => strstr($mail['header']->fromaddress, '<'),
        'role'          => 'support_customer',
    $user_id = wp_insert_user($userdata);

  if( $user_id ) :
        $get_userdata = get_userdata( $user_id );
        $get_user_email = $userdata['user_email'];

     wp_mail( $userdata['user_email'],'test sub', 'Email' .$userdata['user_email']. 'is created','');
            if ( ! is_wp_error( $user_id ) ) {
                 wp_update_post( array(
                  'ID'          => $post_id,
                  'post_author' => $user_id,
    ) );

After the user is created both the admin and user should get an email. I also created a template to be sent:

<section id="email-template">
    <div class="container">
      <div class="row">
        <div class="wrapper">
          <div class="logo">
            <img src="./img/logo.png" alt="LOGO">
          <div class="text-1">
            <p>Hi <b>Jeon Jungkook</b>,<br>
              A client you are in charge of just posted a new reply to his ticket <a href="#123">"Spring"</a>.
          <div class="message">
            <p>Lorem ipsum dolor sit, amet consectetur adipisicing elit. Iure non
              exercitationem maxime nostrum eaque eos quaerat libero explicabo id?
              Temporibus omnis qui dolorum provident debitis voluptatum veritatis
              ipsa necessitatibus mollitia quaerat consequuntur.</p>
          <div class="ticket-button">
            <a href="https://codethemes.co/wp-admin/post.php?post=33177&action=edit" class="btn">View Ticket </a>
          <div class="message">
            <p class="after">If you are responsible for the ticket, please go ahead and do the needful.</p>
            <div class="down">
              <p class="up">Updates by FAQPress </p>
              <div class="vertical"></div>
              <p><a href="#">Unsubscribe.</a></p>

Now I want to use this template and sent an email. Similarly the second instance is for comments and so on. If I can get help with this instance I think I can do rest by myself.

  • WP already has a checkbox in the settings to notify administrators by email when new users sign up. On a multisite it's int he settings page of the network admin under registration settings with the label Registration notification. Other than that, what exactly about wp_mail is unclear? What have you tried so far?
    – Tom J Nowell
    Feb 7, 2022 at 12:30
  • I am trying to create a plugin that does this stuff so I don't want to use the WordPress default setting.
    – Rohit kc
    Feb 7, 2022 at 12:30
  • That doesn't mean you can't re-use the built in functions and APIs to avoid rebuilding half of it yourself. You didn't answer the question I asked at the end of my last comment though :(
    – Tom J Nowell
    Feb 7, 2022 at 12:31
  • Also, I want to use wp_mail to send emails to multiple users such as both admin and user at the same time. I have seen the functionality of wp_mail and also I am new to all this so I want to use it to send it to multiple users.
    – Rohit kc
    Feb 7, 2022 at 12:32
  • I'm unclear what the question is, you've stated your goals, but are you asking for examples of how to use wp_mail? Are you saying you know how to use wp_mail to send an email but want to know how to do a specific thing? You need to write a specific question that can be factually and fully answered, but this isn't phrased as a question
    – Tom J Nowell
    Feb 7, 2022 at 13:18


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.