Skip to content
Advertisement

Attaching / Sending .ics event via email in php

I am having trouble trying to send an .ics file ‘meeting invitation’ via email.

I am using this class to quickly generate the contents of the .ics file: https://gist.github.com/jakebellacera/635416

The content generates correctly but I cannot get it to show as an attachment… so far only as string.

I have even tried to encode it but it just shows the encoded .ics content as a string.

Here is my code currently:

// CREATE ICAL INVITE
include("ICS.php");

//header('Content-Type: text/calendar; charset=utf-8');
//header('Content-Disposition: attachment; filename=cita.ics');

$location = "Barcelona";
$mdesc = "Cita Masaje";
$mname= "$name - $phone";
$meetURL = "redacted";

$props = array(
    'location' => $location,
    'description' => $mdesc,
    'dtstart' => $dateStart,
    'dtend' => $dateEnd,
    'summary' => $mname,
    'url' => $meetURL
);

$ics = new ICS($props);
$ical = $ics->to_string();
$boundary = md5("random"); // define boundary with a md5 hashed value

$recipient = "redacted";
$subject = "Nuevo contacto de $name - $email";
//header
$mailheaders = "From: ".$name." <".$email.">n";
$mailheaders .= "Reply-To: ".$name." <".$email.">n";
$mailheaders .= "MIME-Version: 1.0n";
$mailheaders .= "Content-Type: multipart/mixed;n";
$mailheaders .= "boundary = $boundaryrn"; //Defining the Boundary

//attachment
$formcontent = "--$boundaryrn";
$formcontent .= "De: $name - $emailnTel: $phonenCita: $dateStartnAcaba: $dateEndnMensaje:nn$messagenn";

$formcontent .= "--$boundaryrn";
$encoded_content = chunk_split(base64_encode($ical));

$formcontent .="Content-Disposition: attachment";
$formcontent .="Content-Transfer-Encoding: base64";
//$formcontent .= "Content-Transfer-Encoding: 8bitnn";
//$formcontent .='Content-Type: text/calendar; name="cita.ics"; method=REQUESTn';
$formcontent .="Content-Type: application/octet-stream; name='cita.ics'; method=REQUESTn";
$formcontent .="X-Attachment-Id: ".rand(1000, 99999)."n";
$formcontent .= $encoded_content; // Attaching the encoded file with email

// Send email notification
if(mail($recipient, $subject, $formcontent, $mailheaders)) {
    // send auto-reply
    mail($email, $rplysubject, $reply, $rplyheader) or die("Error!");
} else {
    die("Error!");
}
echo "Muchas gracias $name! Tu mensaje ha sido enviado.";

Commented out are some of the things I have tried… below is the current result from the autoreply email:

boundary = 7ddf32e17a6ac5ce04a8ecbf782ca509

--7ddf32e17a6ac5ce04a8ecbf782ca509
De: Testuser - test@mail.com
Tel: 654396757
Cita: 21-04-2021 10:00
Acaba: 21-04-2021 11:00
Mensaje:

test message

--7ddf32e17a6ac5ce04a8ecbf782ca509
Content-Disposition: attachmentContent-Transfer-Encoding: base64Content-Type: application/octet-stream; name='cita.ics'; method=REQUEST
X-Attachment-Id: 8311
QkVHSU46VkNBTEVOREFSDQpWRVJTSU9OOjIuMA0KUFJPRElEOi0vL2hhY2tzdy9oYW5kY2FsLy9O
T05TR01MIHYxLjAvL0VODQpDQUxTQ0FMRTpHUkVHT1JJQU4NCkJFR0lOOlZFVkVOVA0KTE9DQVRJ
T046QmFyY2Vsb25hDQpERVNDUklQVElPTjpDaXRhIE1hc2FqZQ0KRFRTVEFSVDoyMDIxMDQyMVQx
MDAwMDBaDQpEVEVORDoyMDIxMDQyMVQxMTAwMDBaDQpTVU1NQVJZOktpbGxpbm15dmliZXogLSA2
NTQzOTY3NTcNClVSTDtWQUxVRT1VUkk6aHR0cHM6Ly93d3cudGltZTJtZS5lcw0KRFRTVEFNUDoy
MDIxMDQxNVQxMzMzMjJaDQpVSUQ6NjA3ODI0ODI0Y2Y0MQ0KRU5EOlZFVkVOVA0KRU5EOlZDQUxF
TkRBUg==

In the past I also tried mail->addAttachment() or ->addStringAttachment() but both of those calls break my code.

Here is what the non-encoded ics event looks like:

BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//hacksw/handcal//NONSGML v1.0//EN
CALSCALE:GREGORIAN
BEGIN:VEVENT
LOCATION:Barcelona
DESCRIPTION:Cita
DTSTART:20210428T090000Z
DTEND:20210428T100000Z
SUMMARY:Testuser - 672346435
URL;VALUE=URI:https://www.redacted.com
DTSTAMP:20210409T221423Z
UID:6070b59fe043d
END:VEVENT
END:VCALENDAR

Any idea what the issue could be?? Many thanks ahead of time!

Advertisement

Answer

Ended up implementing PHPMailer and it worked liked a charm.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement