1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

email script

Discussion in 'PHP & Perl' started by lanbo, Feb 4, 2011.

  1. lanbo

    lanbo Jr. VIP Jr. VIP Premium Member

    Joined:
    Aug 23, 2009
    Messages:
    3,431
    Likes Received:
    594
    Home Page:
    hi, does anyone know how to make a loop script to send emails to an email list in a separate file?

    for example, one file could have
    email1@gmail.com
    email2@gmail.com
    email3@gmail.com

    and the script would loop and send the same email to each one.

    thanks!
     
  2. voyevoda

    voyevoda Regular Member Premium Member

    Joined:
    Mar 21, 2010
    Messages:
    217
    Likes Received:
    97
    Location:
    Eastern Front
    Code:
    <?php
    
    $email_file = "/path/to/emails.txt"
    
    $subject    = "subject";
    $message    = "message body";
    $headers    = "From: someguy@whatever.com";
    
    $emails = explode("\n", file_get_contents($email_file));
    
    foreach($emails as $to) {
      echo "Emailing $to ... ";
      mail(trim($to), $subject, $message, $headers);
      echo "done\n";
    }
    
    ?>
    
    Not tested, but it should work fine. See http://php.net/mail for extra options for mail(). $to is trim()ed because Windows is gay and uses \r\n for newlines. You could also preg_split("/\r?\n/", file_get_contents($email_file)) and you wouldn't have to trim() $to.
     
    • Thanks Thanks x 1
  3. Kaimi

    Kaimi Newbie

    Joined:
    Dec 6, 2009
    Messages:
    35
    Likes Received:
    230
    Home Page:
    Code:
    <?php
    $h = fopen("mails.txt", "r");
    if($h)
    {
     while(($mail = fgets($handle)) !== false)
     {
      $mail = trim($mail);
      print mail($mail, 'Message subject', 'Message body') ? "$mail - ok<br />" : "$mail - err<br />";
      ob_flush();
      flush();
     }
     fclose($h);
    }
    ?>
    
     
  4. lanbo

    lanbo Jr. VIP Jr. VIP Premium Member

    Joined:
    Aug 23, 2009
    Messages:
    3,431
    Likes Received:
    594
    Home Page:
    thanks a lot!
    PHP:
    $email_file "/path/to/emails.txt";
    btw you forgot the ;
     
  5. HungryMind

    HungryMind Newbie

    Joined:
    Jul 10, 2010
    Messages:
    9
    Likes Received:
    0
    I think fopen("mails.txt", "r"); is not the right way to get mail text, you can use classes for it or you can include file to get mail text.
     
  6. arasferra

    arasferra Registered Member

    Joined:
    Nov 5, 2009
    Messages:
    99
    Likes Received:
    13
    Research Pear PHP mailing class, thats more advanced and you can use SMTP.
     
  7. chrum

    chrum Newbie

    Joined:
    Mar 1, 2011
    Messages:
    14
    Likes Received:
    2
    Try phpmailer with smtp auth
    For opening large e-mail lists use:
    Code:
    $file_handle = fopen("mailfile", "r");
    while (!feof($file_handle)) {
       $mail = fgets($file_handle);
    echo $mail;
    }
    
    my 1st post btw :p
     
    Last edited: Mar 6, 2011