我创建了一个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." rn";
$header .= "Reply-To: ".$reply_to."rn";
$header .= "MIME-Version: 1.0rn";
//Multipart包装电子邮件内容和附件
$header .= "Content-Type: multipart/mixed;rn";
$header .= " boundary="".$boundary.""";
$message .= "This is a multi-part message in MIME format.rnrn";
$message .= "--".$boundary."rn";
//电子邮件内容//内容类型可以是text/plain或text/html/
$message .= "Content-Type: text/plain; charset="iso-8859-1"rn";
$message .= "Content-Transfer-Encoding: 7bitrn";
$message .= "rn";
$message .= "$message_bodyrn";
$message .= "--".$boundary."rn";
//附件//编辑不同文件扩展名的内容类型
$message .= "Content-Type: application/php;rn";
$message .= " name="".$file_name.""rn";
$message .= "Content-Transfer-Encoding: base64rn";
$message .= "Content-Disposition: attachment;rn";
$message .= " filename="".$file_name.""rn";
$message .= "rn".$content."rn";
$message .= "--".$boundary."--rn";
//发送电子邮件
if (mail($mail_to, $subject, $message, $header)) {
echo "Sent";
} else {
echo "Error";
}
?>
如果使用PhpMailer库,则使用以下函数添加文件。
$mail->AddAttachment("filename");
PHPMailer库链接:https://github.com/PHPMailer/PHPMailer