Here's an example with three options of sending: one email with everyone Bcc'd, one one at a time to each email, or one at a time if the email file is really, really big.
PHP Code:
<?php
$content = $_POST['content'];
$subject = 'our newsletter';
$header = 'From: Newsletter <no-reply@mydomain.com>';
Example 1: You can send to all at once via Bcc:
PHP Code:
// read emails from file
$emails = array_map( 'trim', file( 'emails.txt' ) );
$count = count( $emails );
$to = 'Newsletter <no-reply@mydomain.com>';
// could use:
// $header .= "\r\nBcc: " . implode( ', ', $emails );
// but we'll remove as we add to avoid doubling our memory
// consumption, in case the email list gets large
$header .= "\r\nBcc: ";
for ( $i = 0; $i < $count; $i++ )
{
$header .= ( $i ? ', ' : '' ) . $emails[$i];
unset( $emails[$i] );
}
$send_contact = mail( $to, $subject, $message, $header );
?>
Example 2: Send to one at a time:
PHP Code:
// read emails from file
$emails = array_map( 'trim', file( 'emails.txt' ) );
$count = count( $emails );
for ( $i = 0; $i < $count; $i++ )
{
$send_contact = mail( $emails[$i], $subject, $message, $header );
}
?>
Example 3: If the text's filesize becomes larger than PHP's memory limit:
PHP Code:
$fp = fopen( 'emails.txt', 'r' );
while ( $email = fgets( $fp ) )
{
$send_contact = mail( trim( $email ), $subject, $message, $header );
}
fclose( $fp );
?>