Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jun 2006
    Location
    eth0
    Posts
    68
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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") ?>

  • #2
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    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 );

    ?> 
    Last edited by kbluhm; 06-02-2007 at 05:13 PM. Reason: added third option for huge files

  • #3
    New Coder
    Join Date
    Jun 2006
    Location
    eth0
    Posts
    68
    Thanks
    0
    Thanked 0 Times in 0 Posts
    wow thx a million! works perfectly! hug
    <? print("Hello World") ?>

  • #4
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    A database would work really well for storing the email addresses, and would do a better job of keeping them private..

    HTH
    Dan
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •