Skip to content
PHP Logo

How can the MySQL structure be modified to accommodate sending emails to multiple recipients in a PHP mail system?

To accommodate sending emails to multiple recipients in a PHP mail system using MySQL, you can create a separate table to store email addresses for each recipient. This table can have a foreign key relationship with the main email table to associate multiple recipients with each email. When sending an email, you can query this recipient table to fetch all the email addresses and send the email to each recipient individually.

php
// Assuming you have an 'emails' table to store email details and a 'recipients' table to store recipient email addresses

// Query to fetch email details and recipient email addresses
$query = "SELECT e.subject, e.body, r.email FROM emails e
          JOIN recipients r ON e.id = r.email_id
          WHERE e.id = :email_id";

// Execute the query and send email to each recipient
$stmt = $pdo->prepare($query);
$stmt->execute(['email_id' => $email_id]);

while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    $to = $row['email'];
    $subject = $row['subject'];
    $message = $row['body'];
    
    // Send email to each recipient
    mail($to, $subject, $message);
}