user3495256
user3495256

Reputation: 153

PHP | Sending email invitation in outlook

I am trying to create an invitation email which is sent in outlook, the user usually has the choice of accepting the appointment which populates the calender in outlook. I am using PHP mailer and having trouble, see below:

Example of how the user should receive the email: enter image description here

However when the email is sent, it does not actually send the invitation. Instead text is sent, see picture below of the received email (I have censored the email and personal names): enter image description here

I honestly do not know what is wrong, below is my code. Please assume the variables have values assigned to them, as there will be too much to post otherwise:

                $participant_name = $userid_result["realname"];
                $participant_email = $userid_result["email_primary"];

                $to = $participant_email;

                $organizer          = 'jimmy Smith';
                $organizer_email    = '[email protected]';

                $participant_name_1 = $participant_name;
                $participant_email_1= $participant_email;

                $location           = "N/A";
                $date               = $row_date_from;
                $startTime          = $row_date_from;
                $endTime            = $row_date_to;
                $dateend            = $row_date_to;
                $subject            = 'Holiday Request';
                $desc               = 'Email regarding your holiday request made on Vision';

                $headers = 'Content-Type:text/calendar; Content-Disposition: inline; charset=utf-8;\r\n';
                $headers .= "Content-Type: text/plain;charset=\"utf-8\"\r\n"; 

                $message = "BEGIN:VCALENDAR\r\n
                VERSION:2.0\r\n
                PRODID:-//Vision/NONSGML v1.0//EN\r\n
                METHOD:REQUEST\r\n
                BEGIN:VEVENT\r\n
                UID:" . md5(uniqid(mt_rand(), true)) . "example.com\r\n
                DTSTAMP:" . gmdate('Ymd').'T'. gmdate('His') . "Z\r\n
                DTSTART:".$date."T".$startTime."00Z\r\n
                DTEND:".$dateend."T".$endTime."00Z\r\n
                SUMMARY:".$subject."\r\n
                ORGANIZER;CN=".$organizer.":mailto:".$organizer_email."\r\n
                LOCATION:".$location."\r\n
                DESCRIPTION:".$desc."\r\n
                ATTENDEE;CUTYPE=INDIVIDUAL;ROLE=REQ-PARTICIPANT;PARTSTAT=NEEDS-ACTION;RSVP=TRUE;CN".$participant_name_1.";X-NUM-GUESTS=0:MAILTO:".$participant_email_1."\r\n
                END:VEVENT\r\n
                END:VCALENDAR\r\n";

              //  $headers .= $message;                 
                $zero = 0; 
                send_email($to,$subject,$message,$zero,$headers);

The code for send_email function can be read here: http://pastebin.com/bxJFHUh8

Regards

Upvotes: 1

Views: 2152

Answers (1)

Synchro
Synchro

Reputation: 37808

You can't just set multiple content-type headers and hope it works! This is why libraries like PHPMailer exist to help you avoid doing such silly things!

The problem is that Outlook does not follow the appropriate standards. A message like this should be formatted as a multipart/alternative structure (like typical combined plain/html messages) in which one of the parts is a text/calendar type. Outlook wants this to be a multipart/related structure instead, as is usually used for attachments. There is a good discussion of this problem in this PHPMailer bug report.

PHPMailer is bundled with a simple vcal generator class in the 'extras' folder, and the unit tests have an example of how to use it.

As you will gather from the bug report, this isn't solved and a working solution is pretty messy in any environment since you need to implement both MIME structures (with duplicate data) in order for it to work in both Outlook and gmail.

Upvotes: 1

Related Questions