php - 通过php邮件发送时无法将名称设置为ics文件
问题描述
当我使用 mail() 函数发送文件时,我试图设置文件的名称,我发送它但在 gmail 中我收到的是没有扩展名的无名文件。
有人可以帮我吗?
<?php
$temp = tmpfile();
fwrite($temp, $ical);
rewind($temp);
$to_email = 'test@gmail.com';
$subject = 'Testing PHP Mail';
$message = 'Hola caracola';
$from_email = 'test@test.com';
$path = stream_get_meta_data($temp)['uri'];
$num = md5(time());
$headers = "MIME-Version: 1.0\r\n"; // Defining the MIME version
$headers .= "From:".$from_email."\r\n"; // Sender Email
$headers .= "Reply-To: ".$from_email."\r\n"; // Email addrress to reach back
$headers .= "Content-Type: multipart/mixed;\r\n"; // Defining Content-Type
$body ="Content-Type: text/calendar; name=invite.ics\r\n";
$body .="Content-Disposition: attachment; filename=\"invite.ics\"\r\n";
$body .= $content; // Attaching the encoded file with email
mail($to_email,$subject,$body,$headers);
fclose($temp);
} ?>
谢谢
解决方案
我刚刚成功,我粘贴了下面的代码,问题是 php 将新策略应用于新行标题,所以我只是更改了一些代码以避免此限制并解决了问题:
<?php
$temp = tmpfile();
fwrite($temp, $ical);
rewind($temp);
$to_email = 'test@gmail.com';
$subject = 'Testing PHP Mail';
$message = 'Hello';
$from_email = 'test@test.com';
$path = stream_get_meta_data($temp)['uri'];
$eol = PHP_EOL;
$filename = "invite.ics";
$mailto = $to_email;
$from_mail = $from_email;
$from_name = "Events";
$replyto = $from_email;
$file = $path;
$file_size = filesize($path);
$handle = fopen($file, "r");
$content = fread($handle, $file_size);
fclose($handle);
$content = chunk_split(base64_encode($content));
$uid = md5(uniqid(time()));
$header = "From: ".$from_name." <".$from_mail.">".$eol;
$header .= "Reply-To: ".$replyto.$eol;
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"";
$message = "--".$uid.$eol;
$message .= "Content-Type: application/pdf; name=\"".$filename."\"".$eol; // use different content types here
$message .= "Content-Transfer-Encoding: base64".$eol;
$message .= "Content-Disposition: attachment; filename=\"".$filename."\"".$eol;
$message .= $content.$eol;
$message .= "--".$uid."--";
mail($mailto, $subject, $message, $header);
fclose($temp);
?>
推荐阅读
- python - 如何比较具有相同数量元素的多个列表
- c# - 从视频中创建 Dicom 文件
- php - Laravel "logging.php" - 了解它是如何使用的
- python - Beautiful Soup KeyError:返回 self.attrs[key],但保留发生错误之前(和之后)的值
- windows - 比较文件夹中的文件并删除不存在的文件 - 第 2 部分
- r - 如何在 bam() 通用加性模型中提取单独“按因素”的拟合值?
- c++ - 指向作为类成员的全局变量的指针
- postgresql - 触发以阻止 UPDATE 并将其转换为 INSERT
- c# - 从 IQueryable 创建自定义对象而不将所有内容加载到内存中
- arrays - Natvis 可视化器将成员指针类型视为数组