...

View Full Version : send newsletter from php



CodilX
06-02-2007, 12:15 PM
Hi everyone.

I'm in a bit of a corner here with my .php script

I have a .txt file, in which my newsletter recipients are stored. it looks like this:


email1@email.com
email2@email.com
email3@email.com
email4@email.com
...

I want to send them all an email directly from my site, to each of them seperatly, so they couldn't see each others e-mail, and also with 1 script and 1 click :)

my .php so far looks dull:



$content = $_POST['content'];

$subject ="our newsletter";

$mail_from="no-reply@mydomain.com";
$header="from: Newsletter <no-reply@mydomain.com>";

///$to ="$email";

$send_contact=mail($to,$subject,$message,$header);


I dunno how to send to all of the recipients seperatly the same e-mail with 1 click. can someone please help out?

kbluhm
06-02-2007, 01:03 PM
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

$content = $_POST['content'];
$subject = 'our newsletter';
$header = 'From: Newsletter <no-reply@mydomain.com>';

Example 1: You can send to all at once via Bcc:


// 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:

// 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:

$fp = fopen( 'emails.txt', 'r' );
while ( $email = fgets( $fp ) )
{
$send_contact = mail( trim( $email ), $subject, $message, $header );
}
fclose( $fp );

?>

CodilX
06-02-2007, 01:27 PM
wow thx a million! works perfectly! hug

whizard
06-02-2007, 03:35 PM
A database would work really well for storing the email addresses, and would do a better job of keeping them private..

HTH
Dan



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum