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
- Are there any potential pitfalls to be aware of when generating PDF documents from web pages?
- What best practices should be followed when sending emails through PHP to ensure they are received correctly and with the intended formatting?
- What are the security considerations when allowing downloads of backup files via a PHP script on a web server?