Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-02-2007, 11:15 AM   PM User | #1
CodilX
New Coder

 
Join Date: Jun 2006
Location: eth0
Posts: 68
Thanks: 0
Thanked 0 Times in 0 Posts
CodilX is an unknown quantity at this point
send newsletter from php

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:

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

PHP Code:
$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?
__________________
<? print("Hello World") ?>
CodilX is offline   Reply With Quote
Old 06-02-2007, 12:03 PM   PM User | #2
kbluhm
Senior Coder

 
kbluhm's Avatar
 
Join Date: Apr 2007
Location: Philadelphia, PA, USA
Posts: 1,502
Thanks: 2
Thanked 258 Times in 254 Posts
kbluhm will become famous soon enough
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 mailtrim$email ), $subject$message$header );
}
fclose$fp );

?> 
__________________
ZCE

Last edited by kbluhm; 06-02-2007 at 05:13 PM.. Reason: added third option for huge files
kbluhm is offline   Reply With Quote
Old 06-02-2007, 12:27 PM   PM User | #3
CodilX
New Coder

 
Join Date: Jun 2006
Location: eth0
Posts: 68
Thanks: 0
Thanked 0 Times in 0 Posts
CodilX is an unknown quantity at this point
wow thx a million! works perfectly! hug
__________________
<? print("Hello World") ?>
CodilX is offline   Reply With Quote
Old 06-02-2007, 02:35 PM   PM User | #4
whizard
Senior Coder

 
whizard's Avatar
 
Join Date: Jan 2005
Location: Philadelphia, PA, USA
Posts: 1,457
Thanks: 10
Thanked 37 Times in 37 Posts
whizard will become famous soon enoughwhizard will become famous soon enough
A database would work really well for storing the email addresses, and would do a better job of keeping them private..

HTH
Dan
__________________
If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.
Step 1: Learn. Step 2: Search. Step 3: Post here.
whizard is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:27 PM.


Advertisement
Log in to turn off these ads.