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.0\n";
$mailheaders .= "Content-Type: multipart/mixed;\n";
$mailheaders .= "boundary = $boundary\r\n"; //Defining the Boundary
//attachment
$formcontent = "--$boundary\r\n";
$formcontent .= "De: $name - $email\nTel: $phone\nCita: $dateStart\nAcaba: $dateEnd\nMensaje:\n\n$message\n\n";
$formcontent .= "--$boundary\r\n";
$encoded_content = chunk_split(base64_encode($ical));
$formcontent .="Content-Disposition: attachment";
$formcontent .="Content-Transfer-Encoding: base64";
//$formcontent .= "Content-Transfer-Encoding: 8bit\n\n";
//$formcontent .='Content-Type: text/calendar; name="cita.ics"; method=REQUEST\n';
$formcontent .="Content-Type: application/octet-stream; name='cita.ics'; method=REQUEST\n";
$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 - [email protected]
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!
My Answer:
'ical.php');
// Create new iCal object
$ical = new iCal();
// Set event details
$ical->setEventDetails("Meeting", "2022-01-01 09:00:00", "2022-01-01 10:00:00", "Location");
// Generate .ics content
$ical_content = $ical->getiCalContent();
// Encode .ics content
$encoded_content = chunk_split(base64_encode($ical_content));
// Set email headers
$headers = "From: sender@example.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/calendar; method=REQUEST\r\n";
$headers .= "Content-Disposition: inline\r\n";
// Send email with .ics attachment
$to = "recipient@example.com";
$subject = "Meeting Invitation";
$message = "Please see attached meeting invitation";
$attachment = "Content-Type: text/calendar; name=meeting.ics\r\n";
$attachment .= "Content-Transfer-Encoding: base64\r\n";
$attachment .= "Content-Disposition: attachment; filename=meeting.ics\r\n\r\n";
$attachment .= $encoded_content;
// Send email
$mail_sent = mail($to, $subject, $message, $headers, $attachment);
if($mail_sent) {
echo "Meeting invitation sent successfully";
} else {
echo "Failed to send meeting invitation";
}
Rate this post
5 of 5 based on 5097 votesComments