title: PHP mailer and FPDF attachment with loop - script stops after first run
body: <p>I'm running a following script to generate and send email. The email body is generated in a while loop (content differs) - it works fine. But now I have tried to include a script to generate PDF attachment (via FPDF library), in each iteration the attachment is different.</p>
  
  <p>Problem is: the loop runs just once, for the first case and after it stops. Thank you for your commnents in advance.</p>
  
  <p>My code:</p>
  
  <pre><code>&lt;?
  $mail = new PHPMailer();
  $mail-&gt;SMTPDebug = 1;                                
  $mail-&gt;isSMTP();   
  $mail-&gt;addReplyTo('');
  $mail-&gt;isHTML(true);                              
  $mail-&gt;Subject = "";
  $mail-&gt;Host = 'smtp.gmail.com';  
  $mail-&gt;SMTPAuth = true;                              
  $mail-&gt;SMTPSecure = 'tls';                            
  $mail-&gt;Port = 587;                                    
  $mail-&gt;CharSet = 'utf-8';
  $mail-&gt;setFrom('');
  
  while(($data=MySQL_Fetch_Array($vysl))!=NULL) {
  require_once('invoicetopdf.php');
  $message="";
  $mail-&gt;AddStringAttachment($invoice, 'Invoice.pdf', 'base64', 'application/pdf');
  $mail-&gt;Username = "";
  $mail-&gt;Password = "";
  $mail-&gt;addAddress($to);
  $mail-&gt;Body = $message;
  
  if (!$mail-&gt;send()) {echo "Mailer Error: " . $mail-&gt;ErrorInfo;} 
  
  else {
  
  $mail-&gt;clearAddresses();
  $mail-&gt;ClearAllRecipients();
  $mail-&gt;clearAttachments(); 
  
  echo "Ok";
  
  }  
  } //while
  
  //invoicetopdf.php:
  
  $data = MySQL_Fetch_Array($vysl);     
  
  require_once('../knihovny/pdf/fpdf.php');
  
  $pdf = new PDF();
  $pdf-&gt;.....;
  
  $invoice=$pdf-&gt;Output('S');
  
  ?&gt;
  </code></pre>