0

可能重复:
使用 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中实现这一目标吗?

4

1 回答 1

0

我之前编译过这个。希望能帮助到你。

<?php
$eol = PHP_EOL;
$separator = md5(time());


 function mime_content_type($filename) {

        $mime_types = array(

            'txt' => 'text/plain',
            'htm' => 'text/html',
            'html' => 'text/html',
            'php' => 'text/html',
            'css' => 'text/css',
            'js' => 'application/javascript',
            'json' => 'application/json',
            'xml' => 'application/xml',
            'swf' => 'application/x-shockwave-flash',
            'flv' => 'video/x-flv',

            // images
            'png' => 'image/png',
            'jpe' => 'image/jpeg',
            'jpeg' => 'image/jpeg',
            'jpg' => 'image/jpeg',
            'gif' => 'image/gif',
            'bmp' => 'image/bmp',
            'ico' => 'image/vnd.microsoft.icon',
            'tiff' => 'image/tiff',
            'tif' => 'image/tiff',
            'svg' => 'image/svg+xml',
            'svgz' => 'image/svg+xml',

            // archives
            'zip' => 'application/zip',
            'rar' => 'application/x-rar-compressed',
            'exe' => 'application/x-msdownload',
            'msi' => 'application/x-msdownload',
            'cab' => 'application/vnd.ms-cab-compressed',

            // audio/video
            'mp3' => 'audio/mpeg',
            'qt' => 'video/quicktime',
            'mov' => 'video/quicktime',

            // adobe
            'pdf' => 'application/pdf',
            'psd' => 'image/vnd.adobe.photoshop',
            'ai' => 'application/postscript',
            'eps' => 'application/postscript',
            'ps' => 'application/postscript',

            // ms office
            'doc' => 'application/msword',
            'rtf' => 'application/rtf',
            'xls' => 'application/vnd.ms-excel',
            'ppt' => 'application/vnd.ms-powerpoint',

            // open office
            'odt' => 'application/vnd.oasis.opendocument.text',
            'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
        );

        $ext = strtolower(array_pop(explode('.',$filename)));
        if (array_key_exists($ext, $mime_types)) {
            return $mime_types[$ext];
        }
        elseif (function_exists('finfo_open')) {
            $finfo = finfo_open(FILEINFO_MIME);
            $mimetype = finfo_file($finfo, $filename);
            finfo_close($finfo);
            return $mimetype;
        }
        else {
            return 'application/octet-stream';
        }
    }

$file = array('mime' => mime_content_type($filename), 'name' => $filename, 'data' => chunk_split(base64_encode($filename)));


// Basic Header Input

$to='';
$cc='';
$bcc='';
$your_email = 'John@doe.com';
$to = 'Jane@doe.com';
$subject='A subject';
$from = 'The Matrix';
$message='What ever dot.com';
$headers  = "From: ".$your_email." <$from>".$eol;

$headers .= "Cc: $cc" . "\r\n";

$headers .= "Bcc: $bcc" . "\r\n"; 

// main header

$headers .= "MIME-Version: 1.0".$eol; 
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"";
$headers_to_rep .= "MIME-Version: 1.0".$eol; 
$headers_to_rep .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"";

// no more headers after this, we start the body! //

$body = "--".$separator.$eol;
$body .= "Content-Transfer-Encoding: 7bit".$eol.$eol;


// message
$body .= "--".$separator.$eol;
$body .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$body .= $message.$eol;

// attachment

$body .= "--".$separator.$eol;
$body .= "Content-Type: ".$file['mime']."; name=\"".$file['name']."\"".$eol; 
$body .= "Content-Transfer-Encoding: base64".$eol;
$body .= "Content-Disposition: attachment".$eol.$eol;
$body .= $file['data'].$eol;
$body .= "--".$separator.$eol;

if(!mail($to, $subject, $body,$headers)) {
  echo "Mailer Error: ";
} else {

  echo "Message sent!";
}
?>
于 2012-10-12T06:22:49.403 回答