通过zend_mail发送ical

时间:2013-07-15 03:23:52

标签: php zend-framework icalendar zend-mail

有人可以指导我朝正确的方向发展。

我正在尝试使用邮件将所有日历事件发送给我的用户。我有一个PHP功能,按照我的期望工作。我正在尝试使用zend_mail在zend框架中实现相同的功能。

这是正在运作的功能。

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;
    }   

}

我尝试更改zend标头和邮件正文,但它无法正常工作。在函数中,在消息体中发送了多个标头。

//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);

1 个答案:

答案 0 :(得分:2)

您可以使用Zend_Mail发布当前的尝试吗?您提供的代码段与Zend_Mail问题无关。

更新: 我测试了你的代码,一切似乎都有效。确保ther $ ics var的内容完全相同。我用来测试的代码:

    $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();