title: php - create ics file dynamically and email as attachment
body: <p>I need to create a calendar invite dynamically and email the ics file as an attachment. I'm using sendgrid to send emails.</p>
  
  <p>This is my php script currently:</p>
  
  <pre><code>include("/Users/path/sendgrid-php.php");
  $sendgrid = new SendGrid("uname", "pass");
  $email    = new SendGrid\Email();
  
  $ical = "BEGIN:VCALENDAR
  VERSION:2.0
  PRODID:-//hacksw/handcal//NONSGML v1.0//EN
  BEGIN:VEVENT
  UID:" . md5(uniqid(mt_rand(), true)) . "@test.com
  DTSTAMP:" . gmdate('Ymd').'T'. gmdate('His') . "Z
  DTSTART:19970714T170000Z
  DTEND:19970715T035959Z
  SUMMARY:New event
  END:VEVENT
  END:VCALENDAR";
  
  header('Content-type: text/calendar; charset=utf-8');
  header('Content-Disposition: inline; filename=invite.ics');
  
  $email-&gt;addTo("test@test.com")
      -&gt;setFrom("test2@test2.com")
      -&gt;setSubject("Test-Subject")
      -&gt;setHtml("Test-Body")
      -&gt;setAttachment($ical);
  
  $sendgrid-&gt;send($email);
  </code></pre>
  
  <p>Now, whenever I run this script, instead of sending the ics file as an attachment to the email address provided, my browser automatically downloads the ics file, closes the page, and doesn't do anything (no email is sent). </p>
  
  <p>Can you all please tell me where am I going wrong here? I've just started with PHP so chances are likely that I must have made some silly error.</p>
  
  <p>Thanks.</p>