title: Send email to multiple recipients from txt file using PHP Swiftmailer
body: <p>I have been working on a script to send emails using Swiftmailer to a list of recipients stored in txt file. It is supposed to take emails from <code>./receivers</code> folder all of them named <code>TH001.txt, TH002.txt</code>,... . When I run the code it goes to infinite loop with error:</p>
  
  <pre><code>"
  PHP Notice:  Undefined index: 0
   in /var/www/sipnati/sendmail.php on line 37
  PHP Notice:  Undefined index: 0
   in /var/www/sipnati/sendmail.php on line 37
  "
  </code></pre>
  
  <p>It looks like I have messed up something with parsing the names of the files in <code>./receivers</code> 
  folder. I am running Ubuntu Server 12.04 with PHP5.3 . I am completely stuck, any help pointing the mistake will be greatly appreciated.</p>
  
  <pre><code>    &lt;?php
  
  require_once 'swift_required.php';
  
  define("SMTP_SERVER", "xxx");
  define("FROM_NAME", "xxx");
  define("FROM_EMAIL", "xxx");
  define("USERNAME", "xxx");
  define("PASSWORD", "xxx");
  
  
  
  define("LOG_PATH", "./");
  
  class sendMail {
  
      public function sendMail() {
  
          $transport = Swift_SmtpTransport::newInstance(SMTP_SERVER, 25)
              -&gt;setUsername(USERNAME)
              -&gt;setPassword(PASSWORD)
              ;
  
          $mailer = Swift_Mailer::newInstance($transport);
  
  
   /*     $counter = file_get_contents("./variables.txt");
          $emails_to_send = $counter + 200;
          $file_counter = file_get_contents("./variables1.txt"); */
  
          $scan=array_values(array_diff(scandir('./receivers/'), array('..', '.','.htaccess')));
          $mails=array();
          $c=count($scan);
          if(isset($scan[0])){
              for($i=0;$i&lt;$c;$i++){
                  if(substr($scan[$i], strlen($scan[$i] - 3), 3) &lt;&gt; "txt"){
                      $tmp=file('./receivers/'.$scan[$i],FILE_IGNORE_NEW_LINES);
                      $mails=array_merge($mails,$tmp);
                  }
              }
          }
   /*     file_put_contents("./variables1.txt", $count-1); */
          $regexp = '/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/';
  
          $count = 0;
          if($c&gt;0){
              foreach ($mails as $key =&gt; $value) {
                  while ($count &lt; count($mails)) { /* replaced $counter with count($mails) */
  
                      if (preg_match($regexp, $value) != 0) {
  
                          $message = Swift_Message::newInstance('Wonderful Subject')
                              -&gt;setFrom(array(FROM_EMAIL =&gt; FROM_NAME))
                              -&gt;setTo(array('receiver@domain.org', 'other@domain.org' =&gt; 'A name'))
                              -&gt;setSubject(file_get_contents('./thailand_subject.txt'))
                              -&gt;setBody(file_get_contents('./thailand_body.txt'))
                              ;
  
                          if ($mailer-&gt;send($message)) {
                              $count++;
                          } else {
                              $not_sent[] = $value;
                          }
                      } else {
                          $not_sent[] = $value;
                      }
                      $count++;
                  }
                  /*file_put_contents("./variables.txt", $file_counter++);*/
                  break;
              }
          }
          if (!isset($not_sent))
              $not_sent = null;
          $this-&gt;logToFile($count, count($mails), $not_sent);
      }
  
      private function logToFile($count, $on, $undelivered = null) {
          $toLog = "Sent " . $count . "/" . $on . " mails. Not sent: \n" . print_r($undelivered, true);
          file_put_contents(LOG_PATH . "MAIL_LOG_" . microtime(), $toLog);
      }
  
  }
  
  $p = new sendMail();
  ?&gt;
  </code></pre>