<?php$dtStart = '20090825T131415Z';//yyyymmddThhmmssZ
$dtEnd = '20090826T151617Z';
//--------------------
//create text file
$ourFileName = "icsFile/calendar.txt";
$ourFileHandle = fopen($ourFileName, 'w') or die("can't open file1");
fclose($ourFileHandle);
//
//edit temp file
$myFile = "icsFile/calendar.txt";
$fh = fopen($myFile, 'w') or die("can't open file2");$stringData = "
BEGIN:VCALENDAR\n
PRODID:-//Microsoft Corporation//Outlook 11.0 MIMEDIR//EN\n
VERSION:2.0\n
METHOD:REQUEST\n
BEGIN:VEVENT\n
ORGANIZER:MAILTO:[email protected]\n
DTSTAMP:".date('Ymd').'T'.date('His')."\n
DTSTART:$dtStart\n
DTEND:$dtEnd\n
TRANSP:OPAQUE\n
SEQUENCE:0\n
UID:".date('Ymd').'T'.date('His')."-".rand()."-domain.com\n
SUMMARY:do something someware\n
DESCRIPTION:  This is just a test\n
PRIORITY:5\n
X-MICROSOFT-CDO-IMPORTANCE:1\n
CLASS:PUBLIC\n
END:VEVENT\n
END:VCALENDAR";
fwrite($fh, $stringData);
fclose($fh);
//email temp file
    $fileatt = "icsFile/calendar.txt"; // Path to the file
    $fileatt_type = "application/octet-stream"; // File Type
    $fileatt_name = "ical.ics"; // Filename that will be used for the file as the attachment    $email_from = "[email protected]"; // Who the email is from
    $email_subject = "Email test"; // The Subject of the email
    $email_message = "this is a sample message \n\n next line \n\n next line"; // Message that the email has in it    $email_to = "[email protected]"; // Who the email is too    $headers = "From: ".$email_from;    $file = fopen($fileatt,'rb');
    $data = fread($file,filesize($fileatt));
    fclose($file);    $semi_rand = md5(time());
    $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";    $headers .= "\nMIME-Version: 1.0\n" .
    "Content-Type: multipart/mixed;\n" .
    " boundary=\"{$mime_boundary}\"";    $email_message .= "This is a multi-part message in MIME format.\n\n" .
    "--{$mime_boundary}\n" .
    "Content-Type:text/html; charset=\"iso-8859-1\"\n" .
    "Content-Transfer-Encoding: 7bit\n\n" .
    $email_message . "\n\n";    $data = chunk_split(base64_encode($data));    $email_message .= "--{$mime_boundary}\n" .
    "Content-Type: {$fileatt_type};\n" .
    " name=\"{$fileatt_name}\"\n" .
    //"Content-Disposition: attachment;\n" .
    //" filename=\"{$fileatt_name}\"\n" .
    "Content-Transfer-Encoding: base64\n\n" .
    $data . "\n\n" .
    "--{$mime_boundary}--\n";    $ok = @mail($email_to, $email_subject, $email_message, $headers);
        if($ok) {    } else {
    die("Sorry but the email could not be sent. Please go back and try again!");
    }?>
结果是:Sorry but the email could not be sent. Please go back and try again!由于刚接触php,看代码也不是很明白,谢谢高手能给指点一二  :)

解决方案 »

  1.   

    谢谢,之前用asp。net做的发送邮件能够成功,当初用的是iis,现在把iis卸了,跟这个有没有关系呢?我看一下邮件服务器的配置,谢谢
      

  2.   

    iis和现在的apache的端口有冲突,都是80,如果再装上iis的话我想肯定会有问题,怎么看书上写的装邮件服务器还要装iis呢
      

  3.   

    $email_from = "[email protected]"; 
    $email_to = "[email protected]"; 
    等等这些东西换成可用的邮箱了吗
      

  4.   

    已经发布了吗。网页现在可以用吗。
    如果可用的话,可能还得需要配置smtp服务器吧。
    好像在什么php.ini
    http://www.phpchina.com/html/21/32421-12005.html
    你看看吧。
      

  5.   

    http://topic.csdn.net/u/20080306/10/74a3222a-f57c-49cb-b246-d3e0f8d54563.html用你现在的方法发送邮件不是太好。网上有很多已经封装好的类可以用。那样不用配置服务器什么的了。
    如果你用现在的方法,你看一下上面的连接吧。
      

  6.   


    <?
    $to = "[email protected]";
    $subject = "ZDNet Developer article";
    $msg = "I completely understand SMTP servers now!";
    $headers = "From:[email protected]\r\nReply-To:[email protected]";
    mail("$to", "$subject", "$msg", "$headers");
    echo "finished!";
    ?>
    用这段代码试着发了下,应该是服务器的事Warning: mail() [function.mail]: Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in F:\PHPSystem\myphp\mail1.php on line 6
    finished
      

  7.   


    因为不只是发邮件,是发outlook的日历约会,不能直接用网上那些发邮件的类,我发邮件是试着能不能发出去,如果邮件能发出去,发日历形式的应该也没问题了
      

  8.   

    你设置邮件服务器,是设置 你们现在用的邮件服务器。不是lloccthost
      

  9.   

    我把php.ini文件里的localhost改掉了怎么还会出那句话呢?Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in F:\PHPSystem\myphp\mail1.php on line 8
    finished! 
      

  10.   

    你用php做的网站吗,需要发布吗。如果需要的话,把发布的站点重启一起。web服务器重启。
      

  11.   

    没有什么发布不发布的,我只是做一个小工程测试一下,apache重启了下
      

  12.   

    那就apache重启一下,邮件服务器不可能重启。别的都重启一下。
    你 ini文件改了以后,需要重新读。所以可能需要重启。
      

  13.   

    怎么重新读,修改后没运行程序没任何影响,还localhost,根本没这个词了都到底怎么个重启法?我不知道到底该重启什么?怎么去重启!崩溃
      

  14.   

    我真是个大笨蛋,原来改的ini文件不是用到的那个,
    <?php
    phpinfo();
    ?>
    找到了用到的那个ini文件
    谢谢!
      

  15.   

    看来不是这方面的原因。可能发邮件需要本地有smtp服务器。
    你用别的邮件服务器不行。
    难道需要建本地smtp服务器吗。
      

  16.   

    以上这段只能把日历文件以附件的形式发过去,并且对方不打开这个文件的话在日历里就不显示,每打开一次,日历里就多一项内容,重复的。怎么直接发到outlook的日历里呢