How can PHP be used to read email addresses and names from a TXT file for newsletter distribution?

To read email addresses and names from a TXT file for newsletter distribution using PHP, you can create a script that reads the file line by line, extracts the email addresses and names, and stores them in an array or database for further processing.

<?php
// Open the TXT file for reading
$file = fopen('emails.txt', 'r');

// Initialize an empty array to store email addresses and names
$contacts = [];

// Read the file line by line
while (!feof($file)) {
    $line = fgets($file);
    $data = explode(',', $line);
    
    // Extract email address and name
    $email = trim($data[0]);
    $name = trim($data[1]);
    
    // Store email address and name in the contacts array
    $contacts[] = ['email' => $email, 'name' => $name];
}

// Close the file
fclose($file);

// Output the contacts array for further processing
print_r($contacts);
?>