在 php 中通过电子邮件附加/发送 .ics 事件
Attaching / Sending .ics event via email in php
我在尝试通过电子邮件发送 .ics 文件时遇到问题 'meeting invitation'。
我正在使用这个 class 来快速生成 .ics 文件的内容:
https://gist.github.com/jakebellacera/635416
内容生成正确,但我无法将其显示为附件...目前仅显示为字符串。
我什至尝试对其进行编码,但它只是将编码后的 .ics 内容显示为字符串。
这是我目前的代码:
// 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.";
注释掉了一些我尝试过的东西...下面是自动回复电子邮件的当前结果:
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==
过去我也尝试过 mail->addAttachment() 或 ->addStringAttachment() 但这两个调用都破坏了我的代码。
这是非编码 ics 事件的样子:
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
知道问题出在哪里吗??非常感谢!
最终实现了 PHPMailer,它的工作非常棒。
我在尝试通过电子邮件发送 .ics 文件时遇到问题 'meeting invitation'。
我正在使用这个 class 来快速生成 .ics 文件的内容: https://gist.github.com/jakebellacera/635416
内容生成正确,但我无法将其显示为附件...目前仅显示为字符串。
我什至尝试对其进行编码,但它只是将编码后的 .ics 内容显示为字符串。
这是我目前的代码:
// 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.";
注释掉了一些我尝试过的东西...下面是自动回复电子邮件的当前结果:
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==
过去我也尝试过 mail->addAttachment() 或 ->addStringAttachment() 但这两个调用都破坏了我的代码。
这是非编码 ics 事件的样子:
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
知道问题出在哪里吗??非常感谢!
最终实现了 PHPMailer,它的工作非常棒。