We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 42596
    • 45 Posts
    Sorry about the title of this post but I had no idea how to put this into one sentence haha...

    I have written a script that takes a few variables and then generates a custom made ics file which then (if your on your pc / mac) you can just download and it should open with whatever mail client (if any) you have installed.

    This all works fine and dandy until you get to the mobile side of things. I have seen it work the way I want on other websites so I know its possible but I'm not 100% sure where I'm going wrong, but I think I'm close!

    Here is the block of code that I'm using to generate the ics:

    $desc = str_replace("\n", '\n', $desc);
            $desc = str_replace(array("\r","\n","\r"), '', $desc);
            
            $ical = "BEGIN:VCALENDAR"."\n";
            $ical .= "VERSION:2.0"."\n";
            $ical .= "PRODID:-//Smartershows.com//TheBatteryShow//EN"."\n";
            $ical .= "X-WR-CALNAME;CHARSET=utf-8:".$subj."\n";
            $ical .= "METHOD:PUBLISH"."\n";
            $ical .= "X-MS-OLK-FORCEINSPECTOROPEN:TRUE"."\n";
            $ical .= "BEGIN:VEVENT"."\n";
            $ical .= "SUMMARY;CHARSET=utf-8:".$subj."\n";
            $ical .= "LOCATION;CHARSET=utf-8:".$loc."\n";
            $ical .= "URL:".$url."\n";
            $ical .= "UID:30fc985de98ed0dabfeb13722e3c82259fcd33e3"."\n";
            $ical .= "DESCRIPTION:".$desc."\n";
            $ical .= "DTSTART:".$sDate."T".$sTime."\n";
            $ical .= "DTEND:".$eDate."T".$eTime."\n";
            $ical .= "END:VEVENT"."\n";
            $ical .= "END:VCALENDAR"."\n";
            
        //set correct content-type-header
        header('Content-Disposition: attachment; filename=AddToCalendar.ics');
        header('Content-Type: text/calendar; charset=utf-8');
        return $ical;
        exit;


    So, like I said before its all fine and dandy until you load it in a mobile device. I have tried this on an iPad, iPhone and android phones (also tried in different browsers on said devices).

    No matter the device, the same thing happens...I just get the ics file as text in the browser.

    The reason I think this is MODx related? (or nginx at least) is that when I look at the headers with an inspection tool on download the content-type is coming out as text/html when I am clearly setting it as text/calendar. I can change the other header, with the filename, and it respects that and changes. Just not the most important one that tells the browser what is being spit out! I'm really not sure where the change is happening.

    The above code block is in a snippet and if it matters, is surrounded by other code to generate calendar links for yahoo/google/hotmail but this block that I have shown you is inside an if statement and only this should be executed if the correct requirements are met...it shouldnt matter but I thought it was worth mentioning.

    Any help on this would be greatly appreciated!

    (also sorry if I posted this in the wrong place...mods feel free to move it if need be...)

    Thanks!
      • 42596
      • 45 Posts
      Thanks everyone for the overwhelming response on this! I solved it in the end though, if anyone else ends up having a similar problem, it turns out that i was being an idiot and just didn't set the correct content type on the document that was outputting the ICS. I had to create a new one with the correct file type but after that everything was fine...
        • 28042 ☆ A M B ☆
        • 24,524 Posts
        Thanks for coming back and posting the solution. I'm still not entirely sure what you're talking about, but it would be good to know about if it ever should happen that I need to know about this.
          Studying MODX in the desert - http://sottwell.com
          Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
          Join the Slack Community - http://modx.org
          • 23491 ☆ A M B ☆
          • 1,056 Posts
          Quote from: sottwell at Mar 25, 2014, 07:04 AM
          Thanks for coming back and posting the solution. I'm still not entirely sure what you're talking about, but it would be good to know about if it ever should happen that I need to know about this.

          Susan, I am pretty sure he is referring to setting the Content Type for the actual MODX Resource (under the Settings tab). It essentially overrides any custom Content-Type headers in many cases, so ensuring .ics there is key.

          I've used this approach many times, and have just submitted a PR to get .ics (iCalendar) content type added to the core: https://github.com/modxcms/revolution/pull/11349
            Mike Reid - www.pixelchutes.com
            MODx Ambassador / Contributor
            [Module] MultiMedia Manager / [Module] SiteSearch / [Snippet] DocPassword / [Plugin] EditArea / We support FoxyCart
            ________________________________
            Where every pixel matters.
            • 42596
            • 45 Posts
            You are indeed correct pixelchutes....