title: How to attach a .csv (generated by php) via using SendGrid?
body: <p>I leave a simple text for fast reading.</p>
  
  <p>I'm trying to figure out how to send a csv file via sendgrid, csv is used with array, it's </p>
  
  <pre><code>fputcsv($output, array('Column 1', 'Column 2', 'Column 3'));
  </code></pre>
  
  <p>It's not a real file from server, simply to create file and export data to users.</p>
  
  <p>The problem is... addAttachment or setAttachment returned some errors, they requiere where to find a file from path or directory.</p>
  
  <p>How to attach a file without requesting an existing file from directory via sendgrid?</p>
  
  <p>EDIT:</p>
  
  <pre><code>        //Function to generate csv file
              private function array2csv($array) {
              if (count($array) == 0) {
                  return null;
              }
              $lista = $array;
              ob_start();
              $fp = fopen('php://output', 'w');
              foreach ($array as $campos) {
                  fputcsv($fp, $campos);
              }
              fclose($fp);
              return ob_get_clean();
          }
  
      //Call a function to send csv file
          public function sendCSVFile(){
  
              $data = $this-&gt;reqProducts();
              //prepareParams is csv templating and formatting function.
              $products = $this-&gt;prepareParams($data);
              $products = $this-&gt;array2csv($products);
  
              $body = "Hello, you have an attached file."
  
  
              $email = new SendGrid\Email();
              $email
                -&gt;addTo('foo@bar.com')
                -&gt;setFrom('me@bar.com')
                -&gt;setSubject($body)
                -&gt;setText($body)
                -&gt;setHtml('&lt;strong&gt;Hello World!&lt;/strong&gt;')
                -&gt;addAttachment($products);
  
          }
  </code></pre>
  
  <p>Error output is:</p>
  
  <p><a href="https://i.stack.imgur.com/ZHpLj.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ZHpLj.png" alt="enter image description here"></a></p>