'Need to send email to all users in database with one button?

I have managed by some miracle to send emails to a single user from the database. And I thought sending it to all users would be a no-brainer. However, all the research on google that I did are using mysqli which I am not using. I need this done via PHP PDO instead.

Here is what I have so far, but it just isn't working. I am have managed to get my users emails in a drop down menu which I am proud of, and I am trying to include one option, named : All emails or All users. Which if selected and submit the information will send this email to all users in my database.

<?php include('templates/navbar.php'); ?> <!-- include statement used to insert a part of the html document, not to waste time editing all the pages one by one, instead i can just edit one and it will apply to them all if i include it -->
<?php 
$dbHost = "localhost";
/** hostname of my database */
$dbUser = "root";
/** user name of my database */              /** These are all variables that have been declared */
$dbPassword = "root";
/** password of my database */
$dbName = "comp1321";
/** database name */


/** Connection to my database using the PDO method */
try {
    $dsn = "mysql:host=" . $dbHost . ";dbname=" . $dbName;
    /** Example: $dbHost will take the hostname of "localhost" from the declared variables */
    $pdo = new PDO($dsn, $dbUser, $dbPassword);
} catch (PDOException $e) {
    echo "DB Connection Failed: " . $e->getMessage();
    /** this is a error message in case the database did not connect, it will show on the website */
}


    $query = $pdo->prepare('SELECT emailAddress from user');
    $query->execute(); /** Executing query */
    $results = $query->fetchAll(); /** assigning results to the query which will fetch all data that adhere to that query */
?>

<?php
$message_sent = false;
if(isset($_POST['emailAddress']) && $_POST['emailAddress'] != ''){
   
if( filter_var($_POST['emailAddress'], FILTER_VALIDATE_EMAIL) ){
    // submit the form
    $emailAddress = $_POST['emailAddress'];
    $userName = $_POST['userName'];
    $userMessage = $_POST['Message'];
    
    
    $to = (implode " ," $emailAddress);
    $body = "";
    
    $body .= "From: ".$userName. "\r\n";
    $body .= "Username: ".$userName. "\r\n";
    $body .= "Message: ".$userMessage. "\r\n";
    
    mail($to,$emailAddress, $body);
    $message_sent = true;
    }
}

?>


<section class="main">
    <div class="contactbox">
        <form class="contact-font" method="POST" action="">
            <!-- creates a contact form with post method which will be included in the next coursework for the php insertion of data into database -->
            <br>
            <label for="emailAddress" class="required">Email Address</label> <!-- this is the label of the first input field -->
            <br><br>
            <select name="emailAddress" id="">
                <?php foreach ($results as $r) {
           echo "<option>";
            echo $r['emailAddress'];
           echo "</option>";
        } ?>
            <option value="All emails">
            <?php  $results['emailAddress'];?>
            </option>
            </select>

            <br>
            <br>
            <label for="userName" class="required">Username</label>
            <div class="">
                <input type="text" id="userName" name="userName" class="inputStyle" placeholder="Username" required>
                <!-- this input type is an email, which will make sure the user enters an email with a @ -->
            </div>
            <br>
            <label for="Message" class="required text-align-center">Message</label>
            <div class="">
                <textarea id="Message" name="Message" wrap="soft" class="inputStyle" placeholder="Your message..." rows="6" maxlength="3000" required></textarea>
                <!-- text area used for long messages, with a soft wrap which will not break up any long words, with a max word length of 3000 words -->
            </div>
            <br>
            <br>
            <div class="text-align-center">
                <button type="submit" id="" class="button-contact">Send Message</button>
                <!-- submit button which allows user to submit form -->
            </div>
            <br>
        </form>
    </div>
</section>

<?php include('templates/footer.php'); ?> <!-- include statement used to insert a part of the html document, not to waste time editing all the pages one by one, instead i can just edit one and it will apply to them all if i include it -->


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source