php - 如何在 php mail() 中附加 doc 文件?
问题描述
我已经在 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
请帮帮我吗?
解决方案
几年前,我编写了一个用于发送带有附件的电子邮件的类——它需要一段时间才能工作,所以尽管这些函数是从那个原始类派生的,但最初可能会或可能不会工作。我目前无法对此进行测试,但希望它对解决您的问题有用。
/* 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";
}
祝你好运..
推荐阅读
- android - 分支错误警告。会话初始化已经发生。(-118)
- python - 将 pygame 表面设置为圆角
- apache-spark - 在 EMR 上提交 Spark 作业
- sqlite - 用户如何在 Dotnet core 中下载应用程序的 SQLite 数据库?
- javascript - 如何在 JavaScript 中将字符串转换为对象数组
- amazon-web-services - 如何在 cdk 合成器期间为我的 S3 静态网站更新更新提交 Api 端
- reactjs - 如何在 node_modules 文件夹中编辑文件的代码?
- python - 如何使用scrapy获取元素的内部html
- azure - 在 Azure APIM 策略中验证多个颁发者
- java - Spring:如何覆盖 @Import 注释中使用的类