Sending user data over part 2

I am seeking some more advice on the way to handle this.

I have got one page with links to each admin member which when clicked takes their display name over. On the second page which is a form it takes that display names and populates the subject field with their display name. I need to grab the email address that is associated to that user too but use that as the email address the form on the second page gets sent to as currently my script can only send it to an email address I hard code into it.

Read More

So page one is:

<?php
$args1 = array(
 'role' => 'committee',
 'orderby' => 'user_nicename',
 'order' => 'ASC'
);
 $committee = get_users($args1);


foreach ($committee as $user) {
echo ' 

    <a href="../contact-form?displayname=' . $user->display_name . '"><b style="font-size:18px;">
    <tr>
        <td style="padding: 10px;">' .$user->job_title .' - </td>
        <td style="padding: 10px;">' .$user->display_name .'</td>
    </tr></b></a><br><br>';

 }

?>

Page two is:

<?php $displayname = $_GET['displayname'];?>

<form role="form" method="post" action="../mailuser.php">  

<div class="form-group">

<input type="hidden" name="displayname" value="<?php echo $displayname ?>">
<input type="text" name="hp" class="hp" value="" alt="if you fill this field out then your email will not be sent">

</div>


<div class="form-group">
<label for="InputName">Your name</label>
<input type="name" class="form-control" id="InputName" placeholder="Enter your name" name="username">
</div>

<div class="form-group">                      
<label for="InputEmail">Email address</label>
<input type="email" class="form-control" id="InputEmail" placeholder="you@example.com" name="emailFrom">
</div>

<div class="form-group">                             
<label for="InputMsg">Message</label>
<textarea class="form-control" rows="8" id="InputMsg" placeholder="Please begin typing your message..." name="emailMessage"></textarea>   
</div>    

<button type="submit" class="btn btn-primary pull-right">Send</button>
</form>

And my send script has my email hard coded in as:

$mail->From = ‘myemail@dummy.com’;

So I need that be variable depending on which person you clicked on in the first place. It needs to be sent to both my hard coded email and also the persons email that is associated to them in the WordPress user database.

Related posts

Leave a Reply

1 comment

  1. Based on our comment discussion, you should be able to do something along the lines of the following on page two. Be sure to correct my email_address, I’m not sure if that’s how get_users returns the email address or not.

    <?php
    
    $displayname = $_GET['displayname'];
    
    $args1 = array(
     'role' => 'committee',
     'orderby' => 'user_nicename',
     'order' => 'ASC'
    );
    $committee = get_users($args1);
    
    $matchingEmail = false;
    foreach ($committee as $user) {
        if ( !$matchingEmail && $user->display_name == $displayname ) {
            // great, we found our match
            $matchingEmail = $user->email_address; // I don't know if email_address is right, double check this and modify if necessary
        }
    }
    
    if ( $matchingEmail ) {
        // only proceed if a matching email address is found
        ?>
        <form role="form" method="post" action="../mailuser.php">  
    
            <div class="form-group">
    
                <input type="hidden" name="displayname" value="<?php echo $displayname; ?>">
                <input type="hidden" name="matchingEmail" value="<?php echo $matchingEmail; ?>">
                <input type="text" name="hp" class="hp" value="" alt="if you fill this field out then your email will not be sent">
    
            </div>
    
    
            <div class="form-group">
                <label for="InputName">Your name</label>
                <input type="name" class="form-control" id="InputName" placeholder="Enter your name" name="username">
            </div>
    
            <div class="form-group">                      
                <label for="InputEmail">Email address</label>
                <input type="email" class="form-control" id="InputEmail" placeholder="you@example.com" name="emailFrom">
            </div>
    
            <div class="form-group">                             
                <label for="InputMsg">Message</label>
                <textarea class="form-control" rows="8" id="InputMsg" placeholder="Please begin typing your message..." name="emailMessage"></textarea>   
            </div>    
    
            <button type="submit" class="btn btn-primary pull-right">Send</button>
        </form>
        <?php
    } else {
        ?>
        <p>Something is wrong, I can't find your email address! Please try again.</p>
        <?php
    }
    ?>
    

    Finally on page three, where you send the email, you can do something like:

    <?php $mail->addAddress(stripslashes( $_POST["matchingEmail"] ) ); ?>