【问题标题】:Attaching / Sending .ics event via email in php在php中通过电子邮件附加/发送.ics事件
【发布时间】:2021-07-10 11:12:14
【问题描述】:

我在尝试通过电子邮件发送 .ics 文件“会议邀请”时遇到问题。

我正在使用这个类来快速生成 .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、SwiftMailer 或类似的。它们比低级别的 mail()-function 更容易使用(我假设你正在使用它,即使你没有发布你实际发送邮件的方式)。
  • 请分享更多细节 - 你是如何发送邮件的?
  • 谢谢 Magnus,我还尝试将它与包含 PHPMailer.php 和 Exception.php 的 PHPMailer 一起附加,但这似乎无法正常工作。我想我不能只使用 PHPMailer 的附件部分,而用常规 mail.php 发送它也许我会回去重试 PHPMailer 代替整个邮件的创建、附加和发送过程。
  • 已加入邮件发送功能。有 2 封电子邮件正在发送,但相关的是第一封,需要包含 .ics 文件。我还在编码之前包含了 ics 内容。我对这里失败的原因感到非常迷茫......许多在线示例只是将 .ics 内容附加到电子邮件中......其中一些正在使用我需要了解更多信息的 MIME 东西,其中一些示例说base64对其进行编码,有人说您需要将ics内容放入文件而不是附加该文件......不确定该走哪条路。
  • 为什么不使用任何已经知道如何构建邮件的库呢?如果您想使用附件,这不是一项简单的任务

标签: php email-attachments icalendar


【解决方案1】:

最终实现了 PHPMailer,它的工作就像一个魅力。

【讨论】:

    猜你喜欢
    • 2015-09-30
    • 1970-01-01
    • 1970-01-01
    • 2010-12-13
    • 2011-05-25
    • 2020-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多