如何将.ics附加到电子邮件?

时间:2016-09-27 15:59:30

标签: php email phpmailer email-attachments

我需要将Outlook日历文件(.ics)附加到具有mail功能的电子邮件中。

这就是我现在所拥有的,但它不起作用。

<?php

$firstname = $_POST["firstname"];
$lastname = $_POST["lastname"];
$email = $_POST["email"];
$companyname = $_POST["company"];

$file = "webinar-041016.ics";
$content = file_get_contents($file);
$content = chunk_split(base64_encode($content));

$EmailTo = "mail@mail.com";
$Subject = "New  Webinar Registration";

// prepare email body text
$Body .= "Firstname: ";
$Body .= $firstname;
$Body .= "\n";

$Body .= "Lastname: ";
$Body .= $lastname;
$Body .= "\n";

$Body .= "Email: ";
$Body .= $email;
$Body .= "\n";

$Body .= "Company Name: ";
$Body .= $companyname;
$Body .= "\n";

$headers = 'From: mail@mail.com' . "\r\n" .
        'Reply-To: mail@mail.com' . "\r\n" .
        'X-Mailer: PHP/' . phpversion();

$conf_subject = "Registration Complete ...";

$conf_body = "Dear " . $firstname . ",\n\nThank you..." ;
$conf_body .= "Content-Type: text/calendar; name=\"".$file."\"\r\n" ;
$conf_body .= "Content-Transfer-Encoding: quoted-printable\r\n";
$conf_body .= "Content-Disposition: attachment; filename=\"".$file."\"\r\n\r\n";
$conf_body.= $content."\r\n\r\n";

$conf_headers = 'From: web@mail.com' . "\r\n" .
        'Reply-To: web@mail.com' . "\r\n" .
        'X-Mailer: PHP/' . phpversion();

// send email
$success = mail($EmailTo, $Subject, $Body, $headers);
$conf_mail = mail($email, $conf_subject, $conf_body, $conf_headers);
// redirect to success page
if ($success){
   echo "success";
}else{
    echo "invalid";
}
//Attached is an Outlook calendar invite so that you can plan your day accordingly.\n\n 
?>

我该怎么办?我的编码错了吗?

1 个答案:

答案 0 :(得分:0)

您可能已经看过这个:

PHP Sending Mail

作为一个建议你可能想看看PHPMailer,它将你必须做的工作缩小到一行代码:

$mail->addAttachment('/var/tmp/file.tar.gz');