2

Can someone guide me in right direction.

I am trying to send my all calendar events to my users using mail. I have a php function which is working as per my expectations. I am trying to implement the same functionality in zend framework using zend_mail.

Here is the function that is working.

function sendIcalEmail($firstname,$lastname,$email,$meeting_date,$meeting_name,$meeting_duration) {

    $from_name = "RK";
    $from_address = "m.ravikant10@gmail.com";
    $subject = "Meeting Booking"; //Doubles as email subject and meeting subject in calendar
    $meeting_description = "Here is a brief description of my meeting\n\n";
    $meeting_location = "My Office"; //Where will your meeting take place


    //Convert MYSQL datetime and construct iCal start, end and issue dates
    $meetingstamp = strtotime($meeting_date . " UTC");    
    $dtstart= gmdate("Ymd\THis\Z",$meetingstamp);
    $dtend= gmdate("Ymd\THis\Z",$meetingstamp+$meeting_duration);
    $todaystamp = gmdate("Ymd\THis\Z");

    //Create unique identifier
    $cal_uid = date('Ymd').'T'.date('His')."-".rand()."@mydomain.com";

    //Create Mime Boundry
    $mime_boundary = "----Meeting Booking----".md5(time());

    //Create Email Headers
    $headers = "From: ".$from_name." <".$from_address.">\n";
    $headers .= "Reply-To: ".$from_name." <".$from_address.">\n";

    $headers .= "MIME-Version: 1.0\n";
    $headers .= "Content-Type: multipart/alternative; boundary=\"$mime_boundary\"\n";
    $headers .= "Content-class: urn:content-classes:calendarmessage\n";

    //Create Email Body (HTML)
    $message = '';
    $message .= "--$mime_boundary\n";
    $message .= "Content-Type: text/html; charset=UTF-8\n";
    $message .= "Content-Transfer-Encoding: 8bit\n\n";

    $message .= "<html>\n";
    $message .= "<body>\n";
    $message .= '<p>Dear '.$firstname.' '.$lastname.',</p>';
    $message .= '<p>Here is my HTML Email / Used for Meeting Description</p>';    
    $message .= "</body>\n";
    $message .= "</html>\n";
    $message .= "--$mime_boundary\n";

    //Create ICAL Content (Google rfc 2445 for details and examples of usage) 
    $ical =    'BEGIN:VCALENDAR
PRODID:-//Microsoft Corporation//Outlook 11.0 MIMEDIR//EN
VERSION:2.0
METHOD:REQUEST
BEGIN:VEVENT
ORGANIZER:MAILTO:'.$from_address.'
DTSTART:'.$dtstart.'
DTEND:'.$dtend.'
LOCATION:'.$meeting_location.'
TRANSP:OPAQUE
SEQUENCE:0
UID:'.$cal_uid.'
DTSTAMP:'.$todaystamp.'
DESCRIPTION:'.$meeting_description.'
SUMMARY:'.$subject.'
PRIORITY:5
CLASS:PUBLIC
END:VEVENT
END:VCALENDAR';   

    $message .= 'Content-Type: text/calendar;name="meeting.ics";method=REQUEST;charset=utf-8\n';
    $message .= 'Content-Type: text/calendar;name="meeting.ics";method=REQUEST\n';
    $message .= "Content-Transfer-Encoding: 8bit\n\n";
    $message .= $ical;            

    //SEND MAIL
    //echo $message;die;
    $mail_sent = mail( $email, $subject, $message, $headers );

    if($mail_sent)     {
        return true;
    } else {
        return false;
    }   

}

I tried to change zend headers and message body but its not working. As in the function there are multiple headers being send in message body.

//Create email
        $email = new Zend_Mail('UTF-8');
        $email->setFrom($senderEmail, $senderName);
        $email->addTo($toEmail, $toName);
        $email->setSubject($subject);

        $timestamp  = date('Ymd').'T'.date('His');
        $uid        = date('Ymd').'T'.date('His') . "-" . rand() . '@domain.com';
        $dtCreated  = date('Ymd').'T'.date('His');
        $dtStart    = date('Ymd', strtotime($start)) . 'T' . date('His', strtotime($start));
        $dtEnd      = date('Ymd', strtotime($end)) . 'T' . date('His', strtotime($end));
        $eventSubject       = 'adding calendar events';
        $eventDescription   = $description;


        $ical = <<<ICALENDAR_DATA
BEGIN:VCALENDAR
PRODID:-//Product/Platform/Name//EN
VERSION:2.0
CALSCALE:GREGORIAN
METHOD:REQUEST
BEGIN:VEVENT
DTSTART:{$dtStart}
DTEND:{$dtEnd}
DTSTAMP:{$timestamp}
UID:{$uid}
SUMMARY:{$eventSubject}
DESCRIPTION:{$eventDescription}
CREATED:{$dtCreated}
LAST-MODIFIED:{$dtCreated}
LOCATION:{$location}
SEQUENCE:0
STATUS:CONFIRMED
TRANSP:OPAQUE
ORGANIZER:MAILTO:xyz@gmail.com
BEGIN:VALARM
ACTION:DISPLAY
DESCRIPTION:My cal request
TRIGGER:-P0DT0H10M0S
END:VALARM
END:VEVENT
END:VCALENDAR
ICALENDAR_DATA;

            //$email->addHeader('Content-class', 'urn:content-classes:calendarmessage');
    //$email->addHeader('Content-Type', 'text/calendar; method=REQUEST; charset="UTF-8"');
    //$email->addHeader('Content-Transfer-Encoding', '7bit');

        $attach = new Zend_Mime_Part($ical);
        $attach->type       = 'text/calendar';
        $attach->disposition= Zend_Mime::DISPOSITION_INLINE;
        $attach->encoding   = Zend_Mime::ENCODING_8BIT;
        $attach->filename   = 'calendar.ics'; 

        $email->addAttachment($attach);
        $email->setBodyText('name');
            $email->send($transport);
Ravi Kant Mishra
  • 778
  • 1
  • 7
  • 13

1 Answers1

2

Could you post your current attempt using Zend_Mail? The snippet you provided has nothing to do with your Zend_Mail problem.

Update: I tested your code and everything seems to work. Make sure the contents of ther $ical var are exactly the same. The code I used to test:

    $email = new Zend_Mail('UTF-8');
    $email->setFrom('someone@somewhere.com', 'Someone');
    $email->addTo('someone@somewhere.com', 'Someone');
    $email->setSubject($subject);

    $ical = '{valid ical content}';

    $attach = new Zend_Mime_Part($ical);
    $attach->type = 'text/calendar';
    $attach->disposition = Zend_Mime::DISPOSITION_INLINE;
    $attach->encoding = Zend_Mime::ENCODING_8BIT;
    $attach->filename = 'calendar.ics'; 

    $email->addAttachment($attach);
    $email->setBodyText('Test body');
    $email->send();
Remko
  • 968
  • 6
  • 19
  • I have updated my question and added the code. headers are comment right now. – Ravi Kant Mishra Jul 16 '13 at 10:55
  • both the codes are working and showing calendar.ics as attachment. but in outlook zend code dosen't show the accept and reject options. – Ravi Kant Mishra Jul 16 '13 at 11:35
  • 1
    I see. Seems not directly related to your code. Microsoft does not directly supports ical events in this way. See this article: http://support.microsoft.com/kb/944094/en-us There seems to be some sort of bug in where a appointment that exists will prompt for some sort of update. Is it possible that your 'test appointment' is already present in your Oulook calendar? – Remko Jul 16 '13 at 14:51
  • Appointments are getting created randomly by a web app. So they can not be present in outlook. If possible then please execute my first piece of php code. You will get all the options automatically. there is no need to add the appointments earlier. – Ravi Kant Mishra Jul 16 '13 at 16:25
  • I've tested your code and it worked. Got angry. Tested it with the ZF code, failed. Tested again. Seems the content of the Ical has critical details for Outlook to work. To rule out any Ical request errors I used your $ical var in my ZF test-code and it worked. Played arround with headers a little but they do not realy make a difference. I guess my (manually written) Ical request does not comply with the standards of Outlook. Can you test your generated $ical value with the ZF code? – Remko Jul 17 '13 at 12:07
  • I tested my core php $ical value with zend framework. It seems not working. Can you provide me your code that you used with ZF and working. – Ravi Kant Mishra Jul 17 '13 at 12:26
  • I added the following part inside your function http://codepad.org/ikD4cF0d if we got it working I will post the complete ZF script for future reference – Remko Jul 17 '13 at 12:33
  • sure thanks. I am also trying for the last few days with no luck. – Ravi Kant Mishra Jul 17 '13 at 12:47
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/33636/discussion-between-okmer-and-ravi-kant-mishra) – Remko Jul 17 '13 at 12:52
  • There is some problem on outlook 2007. Anyhow I managed to get outlook 2010 and my both codes are working. – Ravi Kant Mishra Jul 17 '13 at 16:29
  • I would advise to play around with the information found on: http://stackoverflow.com/questions/5670515/outlook-2003-does-not-import-ics-calendar-while-outlook-2007-does Also I would test the METHOD:PUBLISH/METHOD:REQUEST param as it seems this makes a difference depending on the Outlook version. – Remko Jul 17 '13 at 17:49
  • I have already tried with both METHOD options, as I am struggling from last few days. I tried almost all the codes available over internet. But will try once again. I m little bit happy now at-least my code is working on 2010. thanks again buddy I got some idea after discussing with u. if I can help you with anything let me know. – Ravi Kant Mishra Jul 17 '13 at 18:02