PHP - 为邮件添加ical附件

时间:2015-09-17 08:43:51

标签: php html icalendar

我正在制作一个基本的PHP预订系统。 (见下面的代码) 我想预约+做一个iCal附件并发送邮件。 但由于某种原因,我的附件没有制作,只是在电子邮件中输出为普通文本。

仅供参考:例如,某些标签仅用于删除我的邮件或电话。 不打算在这里贴出来:))

PHP代码

<?php

    $message = "";
    $datum = "";

    //email adres
    $email = "<email>";

    $required = array('dag', 'maand', 'jaar', 'uur', 'minuten', 'naam','email', 'telefoon', 'aantal', 'bericht', 'type');

    if (isset($_POST['reserveren']))
    {
        $data = $_POST['reservatie'];

        $headers = "";

        $message .= "Dag Lode en Eva, via de website kregen jullie een nieuwe reservatie, gelieve de persoon zo snel mogelijk een bevestigingsmail te sturen!";
        $message .= "\n\n";
        $message .= $data['bericht'];
        $message .= "\n\n";

        $message .= "Telefoon: " . $data['telefoon'];
        $message .= "\n\n";
        $message .= "Lunch/Diner: " . $data['type'];
        $message .= "\n\n"; 
        $message .= "Aantal personen: " . $data['aantal'];
        $message .= "\n\n";
        $message .= "Email: " . $data['email'];
        $message .= "\n\n";

        $message .= "Datum: " . $data['dag'];
        $message .= "\n\n";
        $message .= "Maand: " . $data['maand'];
        $message .= " " . $data['jaar'];
        $message .= "\n\n";
        $message .= "Tijdstip: " . $data['uur'] . " " . $data['minuten'];       
        $message .= "\n\n";

        $naam = $data['naam'];
        $datum = $data['dag'] ."/". "1" ."/". $data['jaar']; 
        $mail = $data['email'];
        $plaats = "Baronie";
        $omschrijving = "reservering";

        //Create ICAL Content
        $ical =    'BEGIN:VCALENDAR
        PRODID:-//Microsoft Corporation//Outlook 11.0 MIMEDIR//EN
        VERSION:2.0
        METHOD:PUBLISH
        BEGIN:VEVENT
        ORGANIZER:MAILTO:'.$mail.'
        DTSTART:'.$datum.'
        LOCATION:'.$plaats.'
        TRANSP:OPAQUE
        SEQUENCE:0
        DESCRIPTION:'.$omschrijving.'
        SUMMARY:'.$subject.'
        PRIORITY:5
        CLASS:PUBLIC
        END:VEVENT
        END:VCALENDAR';   

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

        $subject = 'Nieuwe reservatie via de website';
        $headers = 'From: '. $data['email']. "\r\n" .'Reply-To: '. $data['email']. "\r\n" .'X-Mailer: PHP/' . phpversion();

        mail($email, $subject, $message, $headers);

        echo "mail verstuurd!";

    }
?>

电子邮件输出

Dag Lode en Eva, via de website kregen jullie een nieuwe reservatie, gelieve de persoon zo snel mogelijk een bevestigingsmail te sturen!

met cal

Telefoon: <phone>

Lunch/Diner: Diner

Aantal personen: 10

Email: <mail>

Datum: 01

Maand: Januari 2015

Tijdstip: 12 00

Content-Type: text/calendar;name="reservering.ics";method=REQUEST\nContent-Transfer-Encoding: 8bit

BEGIN:VCALENDAR
        PRODID:-//Microsoft Corporation//Outlook 11.0 MIMEDIR//EN
        VERSION:2.0
        METHOD:PUBLISH
        BEGIN:VEVENT
        ORGANIZER:MAILTO:<my email address here>
        DTSTART:01/1/2015
        LOCATION:Baronie
        TRANSP:OPAQUE
        SEQUENCE:0
        DESCRIPTION:reservering
        SUMMARY:
        PRIORITY:5
        CLASS:PUBLIC
        END:VEVENT
        END:VCALENDAR

提前致谢!

亲切的问候!

1 个答案:

答案 0 :(得分:0)

假设PHPMailer没问题,那么应该完成以下工作。请注意我把它剥了一下,但我认为守则应该很容易理解它。

<?php

//Of course, you can even include PHPmailer class directly!
require 'vendor/autoload.php';
define('CRLF',"\r\n");

//do your processing here - assuming the variables are filled as follows:
$from='some@mail.address';
$to='some@persons.address';
$subject='someSubject';
$message='Hello World!';

$starttime=time();  //note that you need to supply a unix timestamp here!
$endtime=time()+3600;

//of course assuming the variables used in the attachments are accordingly set
$attachment='BEGIN:VCALENDAR'.CRLF.
            'VERSION:2.0'.CRLF.
            'PRODID:-//question_on//stackoverflow.com//EN'.CRLF.
            'BEGIN:VEVENT'.CRLF.
            'UID:'.md5(uniqid()).CRLF.
            'DTSTAMP:'.date('Ymd')."T".date('His').CRLF.
            'DTSTART:'.date('Ymd',$starttime)."T".date('His',$starttime).CRLF.
            'DTEND:'.date('Ymd',$endtime)."T".date('His',$endtime).CRLF.
            'LOCATION:somewhere'.CRLF.
            'DESCRIPTION:Some Description, if it should be multiline\,'.CRLF.
            '    the following lines need to be indentend by a single tab'.CRLF.
            'SUMMARY:test Event'.CRLF.
            'PRIORITY:5'.CRLF.
            'END:VEVENT'.CRLF.
            'END:VCALENDAR'.CRLF;
$mail = new PHPMailer;
$mail->From = $from;
$mail->addAddress($to);
$mail->isMail();
/*
If you later decide to use smtp instead of php's mail-function, just use the following lines
instead of 'isMail()'
$mail->isSMTP();
$mail->Host = 'mail.youmailprovider.com';
$mail->SMTPAuth = true;
$mail->Username = 'yourSMTPUser';
$mail->Password = 'password';
*/
$mail->Subject = $subject;
$mail->addStringAttachment($attachment,'filename_to_show.ics','base64','text/calendar');
$mail->Body=$message;

if(!$mail->send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    echo  "Message sent!";
}