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);
?>
Related Questions
- What potential pitfalls should be considered when including external PHP configuration files, such as "konfiguration.php," in a script?
- Are there any best practices or guidelines for constructing SOAP requests in PHP to avoid receiving a "bad request" response?
- How do popular forum platforms like PHPBB handle displaying online users, and can this be replicated in custom PHP scripts?