title: I'm creating a .csv file using fputcsv() and I would like to use AddAttachment() in PHPMailer to send it off automatically
body: <p>I've edited the original post, hopefully this is more to the point.  I would like to drop the .csv file created by fputcsv() and drop it into AddAttachment and send off.  Or, if you know how to take the .csv file created and send it to a directory on the server that would be fine too.  I think the most direct question is how do I take the results of fputcsv() and drop it into AddAttachments?  Do I use temp file?</p>
  <pre><code>header('Content-Type: text/csv');
  header('Content-Disposition: attachment; filename=&quot;file.csv&quot;');
  header('Cache-Control: no-cache, no-store, must-revalidate');
  header('Pragma: no-cache');
  header('Expires: 0');
  
          $csvoutput = fopen('php://output', 'w');
          $row = getRow($result3);
          $headers = array_keys($row);
          fputcsv($csvoutput, $headers);
              
                 while ($row = getRow($result3)) {
      
                      fputcsv($csvoutput, $row);
      
                  }
  
  $mail = new PHPMailer();
  $mail-&gt;IsSMTP();
  $mail-&gt;Host       = &quot;smtp.website.com&quot;;
  $mail-&gt;Port       = 587;
  $mail-&gt;SMTPAuth   = true;
  $mail-&gt;SMTPSecure = &quot;tls&quot;;
  $mail-&gt;Username   = $email_user;
  $mail-&gt;Password   = $email_pass;
  $mail-&gt;SetFrom(&quot;example@example.com&quot;, &quot;Ex&quot;);
  $mail-&gt;AddAddress(&quot;example@example.com&quot;, &quot;Ex&quot;);
  $mail-&gt;isHTML(true);
  $mail-&gt;WordWrap = 50;
  $mail-&gt;Subject    = &quot;Example&quot;;
  $mail-&gt;Body       = &quot;Example&quot;;
  $result = $mail-&gt;Send();
  
  
  
  fclose($csvoutput);
  exit;
  </code></pre>
  <p>Do I need the headers() if I'm not downloading it to the page?</p>
  <pre><code>header('Content-Type: text/csv');
  header('Content-Disposition: attachment; filename=&quot;file.csv&quot;');
  header('Cache-Control: no-cache, no-store, must-revalidate');
  header('Pragma: no-cache');
  header('Expires: 0');
  </code></pre>
  <p>Also, I know this needs to be changed:</p>
  <pre><code>$csvoutput = fopen('php://output', 'w');
  </code></pre>
  <p>Please be as detailed as possible, I am unfamiliar with working with files and I couldnt find a tutorial specific to this.</p>