【发布时间】:2014-12-31 04:13:45
【问题描述】:
我创建了一个 XML,我想将其作为电子邮件的附件发送。如何使用 PHP 做到这一点?我的代码不工作
我有这个
<?php
$mail_to = "";
$from_mail = "";
$from_name = "";
$reply_to = "";
$subject = "";
$message = "";
/* 附件文件 */ // 附件位置
$file_name = "only1.php";
$path = "http://66.147.244.92/~homecre1/public_html/Test/only1.php;
//读取文件内容
$file = $path.$file_name;
$file_size = filesize($file);
$handle = fopen($file, "r");
$content = fread($handle, $file_size);
fclose($handle);
$content = chunk_split(base64_encode($content));
/* 设置邮件头 */ // 生成边界
$boundary = md5(uniqid(time()));
// 邮件标题
$header = "From: ".$from_name." \r\n";
$header .= "Reply-To: ".$reply_to."\r\n";
$header .= "MIME-Version: 1.0\r\n";
// 多部分包装电子邮件内容和附件
$header .= "Content-Type: multipart/mixed;\r\n";
$header .= " boundary=\"".$boundary."\"";
$message .= "This is a multi-part message in MIME format.\r\n\r\n";
$message .= "--".$boundary."\r\n";
// 邮件内容 // 内容类型可以是 text/plain 或 text/html
$message .= "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n";
$message .= "Content-Transfer-Encoding: 7bit\r\n";
$message .= "\r\n";
$message .= "$message_body\r\n";
$message .= "--".$boundary."\r\n";
// 附件 // 编辑不同文件扩展名的内容类型
$message .= "Content-Type: application/php;\r\n";
$message .= " name=\"".$file_name."\"\r\n";
$message .= "Content-Transfer-Encoding: base64\r\n";
$message .= "Content-Disposition: attachment;\r\n";
$message .= " filename=\"".$file_name."\"\r\n";
$message .= "\r\n".$content."\r\n";
$message .= "--".$boundary."--\r\n";
// 发送电子邮件
if (mail($mail_to, $subject, $message, $header)) {
echo "Sent";
} else {
echo "Error";
}
?>
【问题讨论】:
标签: php xml email email-attachments