可能重复:
使用 php 将文件附加到电子邮件
我正在使用以下代码在电子邮件中发送附件
<?php
//to get the file extention
function getExtension($str)
{
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
//To Upload a file
function upload_image()
{
$newname="";
if ($_FILES["resume_upload"]["error"] == UPLOAD_ERR_OK)
{
$filename = $_FILES['resume_upload']['name'];
$extension = getExtension($filename);
$extension = strtolower($extension);
$image_name=uniqid('img').'.'.$extension;
$newname=$image_name;
$temp_name=$_FILES['resume_upload']['tmp_name'];
$copied = copy($temp_name, $newname);
return $newname;
}
}
$filename=upload_image();
echo "the file name :".$filename;
//$filename="";
$path ="";
$mailto="test@gmail.com";
$from_mail="test@gmail.com";
$from_name="test";
$replyto ="test@gmail.com";
$subject="test mail";
$message="test message";
mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message);
function mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message) {
// $file = $path.$filename;
$file=$filename;
$file_size = filesize($file);
$handle = fopen($file, "r");
$content = fread($handle, $file_size);
fclose($handle);
$content = chunk_split(base64_encode($content));
$uid = md5(uniqid(time()));
$name = basename($file);
$header = "From: ".$from_name." <".$from_mail.">\r\n";
$header .= "Reply-To: ".$replyto."\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
$header .= "This is a multi-part message in MIME format.\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$header .= $message."\r\n\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; // use different content types here
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
$header .= $content."\r\n\r\n";
$header .= "--".$uid."--";
echo "end ofmail";
if (mail($mailto, $subject, "", $header)) {
$msg = "mail send ... OK"; // or use booleans here
} else {
$msg = "mail send ... ERROR!";
}
}
?>
我正在将文件上传到服务器。我需要将上传的文件作为附件发送到 php 电子邮件中。
上面的代码无法发送附加邮件。任何人都可以建议在php中实现这一目标吗?