如何在php mail()中附加文档文件?

时间:2018-07-15 07:14:31

标签: php

我已经在mail()中附加了doc文件,但文件未通过邮件发送

$file = $apply_data['doc'];
    $job_email = $mail['email'];
    $email = $apply_data['email'];
    $to = "help@jnvhelp.com,".$job_email.",".$email."";
    $url = "kamla.nigam@outlook.com";
    $time = date("Y/m/d");
    $message = "JNV JOB REQUEST"; 

$header = "From: JNV HELP\r\n";
        $header .= "Cc: $url\r\n";
        $header .= "MIME-Version: 1.0\r\n";
        $header .= "Content-Type: multipart/mixed; boundary=\"".$time."\"\r\n\r\n";
        $header .= "This is a multi-part message in MIME format.\r\n";
        $header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
        $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
        $header .= "Content-Type: application/octet-stream";
        $header .= "Content-Transfer-Encoding: base64\r\n";
        $header .= "Content-Disposition: attachment";  
        $attachment = chunk_split(base64_encode(file_get_contents($View_Path.$file)));
        $header .= $attachment."\r\n\r\n";
    //echo $header; exit;
    //echo ($message);exit;
        $retval = mail($to,$subject,$message,$header); //line 1936
            if( $retval == true )
            {
              //echo "Message sent successfully...";
            }
            else
            {
              //echo "Message could not be sent..."; exit;
            }

但是问题是我尝试发送邮件然后在下面显示该消息

时出现的问题
Warning: mail(): Multiple or malformed newlines found in additional_header in /home/jnvhelpc/public_html/index.php on line 1936

请任何人帮助我??

1 个答案:

答案 0 :(得分:0)

几年前,我写了一个用于发送带有附件的电子邮件的类-花了一段时间使它起作用,因此尽管这些函数是从该原始类派生的,但以下内容最初或可能不起作用。我目前无法对此进行测试,但希望它将对解决您的问题有用。

/* utility functions ripped from the class */

function getmimetype( $filepath ){
    $finfo = finfo_open( FILEINFO_MIME_TYPE );
    $mimetype = finfo_file( $finfo, $filepath );
    finfo_close( $finfo );
    return $mimetype;
}

function multipartBoundary() { return "==Multipart_Boundary_x".md5( microtime() )."x"; }

function htmlmessage( $message, $title ){
    $stream=array();
    $stream[]="\r\n";
    $stream[]="<html>\r\n";
    $stream[]="<head>\r\n";
    $stream[]="<title>$title</title>\r\n";
    $stream[]="</head>\r\n";
    $stream[]="<body>\r\n";
    $stream[]="$message\r\n";
    $stream[]="</body>\r\n";
    $stream[]="</html>\r\n\r\n";
    return implode( '', $stream );
}


function addattachment( $file=false, $i=1 ){
   try{
        if( $file && file_exists( realpath( $file ) ) ){
            $filename = pathinfo( realpath( $file ),PATHINFO_BASENAME );
            $data = file_get_contents( realpath( $file ) );
            $chunkeddata = chunk_split( base64_encode( $data ) );
            $filesize=filesize( realpath( $file ) );
            $filelastmod=date( "Y-m-d G:i:s", filemtime( realpath( $file ) ) );
            $contenttype=getmimetype( realpath( $file ) );
            $contentmd5=md5( $file );
            $contentid="part{$i}.".substr( md5( $file ),0,8 ).".".substr( sha1( $file ),0,8 ).".".substr( sha1( $filesize ), 1,8 ).".".crc32( $file );
            $stream=array();


            $stream[]="Content-Type: \"{$contenttype}\"; name=\"{$filename}\"\n";
            $stream[]="Content-Description: \"{$filename}\"\n";
            $stream[]="Content-ID: <{$contentid}>\n";
            $stream[]="Content-MD5: \"{$contentmd5}\"\n";
            $stream[]="Content-features: \"{$contenttype}, {$filename}\"\n";
            $stream[]="Content-Disposition: attachment; filename=\"{$filename}\"; size={$filesize};\n modification-date=\"{$filelastmod}\";\n";
            $stream[]="Content-Transfer-Encoding: base64\n\n";
            $stream[]="{$chunkeddata}\n\n";

            clearstatcache();
            return implode( '', $stream );
        } else {
            throw new Exception('does the file exist?');
        }
        return 'error';
   }catch( Exception $e ){
      exit( $e->getMessage() );
   }
}   







$file = $apply_data['doc'];
$job_email = $mail['email'];
$email = $apply_data['email'];
$to = "help@jnvhelp.com,".$job_email.",".$email."";
$url = "kamla.nigam@outlook.com";
$time = date("Y/m/d");
$message = "JNV JOB REQUEST";


$attachment = $View_Path . $file;
$charset = 'iso-8859-1';
$boundary = multipartBoundary();



$header = "From: JNV HELP\r\n";
$header .= "Cc: $url\r\n";
$header .= "MIME-Version: 1.0\r\n";

$header.="Content-Type: multipart/mixed; boundary=\"".$boundary."\";\r\n\r\n";
$header.="This is a multi-part message in MIME format.\r\n";
$header.="--".$boundary."\r\n";

$header.="Content-Type: text/html; charset=\"".$charset."\"\r\n";
$header.="Content-Transfer-Encoding: 7bit\r\n\r\n";
$header.= htmlmessage( $message, 'HELP' ) . "\r\n\r\n";
$header.="--".$boundary."\r\n";

$header.="Content-type: text/plain; charset=\"".$charset."\"\r\n\r\n";
$header.=strip_tags( $message )."\r\n\r\n";
$header.="--".$boundary."\r\n";

/* try to add the attachment */
$header.=addattachment( $attachment, 1 )."\r\n\r\n";
$header.="--".$boundary."--";                       


/* try to send email */
$retval = @mail( $to, $subject, $message, $header );


/* further processing if $retval is true */

您可以轻松地使用它通过数组来发送多个附件,就像这样

/* assumed that $files is a populated array of filepaths */
foreach( $files as $index => $file ){
    $header.=addattachment( $file, ( $index + 1 ) )."\r\n\r\n";
    $header.="--".$boundary."\r\n";
}

祝你好运。