猿问

如何附加两个或多个文件并使用PHP发送邮件

如何附加两个或多个文件并使用PHP发送邮件

下面的代码仅发送一个附件,但是我需要附加并发送两个文件(一个rar文件和pdf)


$email_to = "$email"; // The email you are sending to (example)

$email_from = "online@example.co.in"; // The email you are sending from (example)

$email_subject = "subject line"; // The Subject of the email

$email_txt = "text body of message"; // Message that the email has in it

$fileatt = "files/example.rar"; // Path to the file (example)

$fileatt_type = "application/x-rar-compressed"; // File Type

$fileatt_name = "example.rar"; // Filename that will be used for the file as the attachment

$file = fopen($fileatt,'rb');

$data = fread($file,filesize($fileatt));

fclose($file);

$semi_rand = md5(time());

$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

$headers="From: $email_from"; // Who the email is from (example)

$headers .= "\nMIME-Version: 1.0\n" .

"Content-Type: multipart/mixed;\n" .

" boundary=\"{$mime_boundary}\"";

$email_message .= "This is a multi-part message in MIME format.\n\n" .

"--{$mime_boundary}\n" .

"Content-Type:text/html; charset=\"iso-8859-1\"\n" .

"Content-Transfer-Encoding: 7bit\n\n" . $email_txt;

$email_message .= "\n\n";

$data = chunk_split(base64_encode($data));

$email_message .= "--{$mime_boundary}\n" .

"Content-Type: {$fileatt_type};\n" .

" name=\"{$fileatt_name}\"\n" .

"Content-Transfer-Encoding: base64\n\n" .

$data . "\n\n" .

"--{$mime_boundary}--\n";


mail($email_to,$email_subject,$email_message,$headers);


浮云间
浏览 672回答 3
3回答

湖上湖

<form&nbsp;action="#"&nbsp;method="POST"&nbsp;enctype="multipart/form-data"&nbsp;&nbsp;> &nbsp;<input&nbsp;type="file"&nbsp;name="csv_file[]"&nbsp;/> &nbsp;<br/> &nbsp;<input&nbsp;type="file"&nbsp;name="csv_file[]"&nbsp;/> &nbsp;<br/> &nbsp;<input&nbsp;type="file"&nbsp;name="csv_file[]"&nbsp;/> &nbsp;<br/> &nbsp;<input&nbsp;type="submit"&nbsp;name="upload"&nbsp;value="Upload"&nbsp;/> &nbsp;<br/></form>&nbsp;<?phpif($_POST)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;for($i=0;&nbsp;$i&nbsp;<&nbsp;count($_FILES['csv_file']['name']);&nbsp;$i++){ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$ftype[]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=&nbsp;$_FILES['csv_file']['type'][$i]; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$fname[]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=&nbsp;$_FILES['csv_file']['name'][$i]; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;array&nbsp;with&nbsp;filenames&nbsp;to&nbsp;be&nbsp;sent&nbsp;as&nbsp;attachment &nbsp;&nbsp;&nbsp;&nbsp;$files&nbsp;=&nbsp;$fname; &nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;email&nbsp;fields:&nbsp;to,&nbsp;from,&nbsp;subject,&nbsp;and&nbsp;so&nbsp;on &nbsp;&nbsp;&nbsp;&nbsp;$to&nbsp;=&nbsp;"example@gmail.com"; &nbsp;&nbsp;&nbsp;&nbsp;$from&nbsp;=&nbsp;"example@gmail.com";&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;$subject&nbsp;="My&nbsp;subject";&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;$message&nbsp;=&nbsp;"My&nbsp;message"; &nbsp;&nbsp;&nbsp;&nbsp;$headers&nbsp;=&nbsp;"From:&nbsp;$from"; &nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;boundary&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;$semi_rand&nbsp;=&nbsp;md5(time());&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;$mime_boundary&nbsp;=&nbsp;"==Multipart_Boundary_x{$semi_rand}x";&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;headers&nbsp;for&nbsp;attachment&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;$headers&nbsp;.=&nbsp;"\nMIME-Version:&nbsp;1.0\n"&nbsp;.&nbsp;"Content-Type:&nbsp;multipart/mixed;\n"&nbsp;.&nbsp;"&nbsp;boundary=\"{$mime_boundary}\"";&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;multipart&nbsp;boundary&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;$message&nbsp;=&nbsp;"This&nbsp;is&nbsp;a&nbsp;multi-part&nbsp;message&nbsp;in&nbsp;MIME&nbsp;format.\n\n"&nbsp;.&nbsp;"--{$mime_boundary}\n"&nbsp;.&nbsp;"Content-Type:&nbsp;text/plain;&nbsp;charset=\"iso-8859-1\"\n"&nbsp;.&nbsp;"Content-Transfer-Encoding:&nbsp;7bit\n\n"&nbsp;.&nbsp;$message&nbsp;.&nbsp;"\n\n";&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;$message&nbsp;.=&nbsp;"--{$mime_boundary}\n"; &nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;preparing&nbsp;attachments &nbsp;&nbsp;&nbsp;&nbsp;for($x=0;$x<count($files);$x++){ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$file&nbsp;=&nbsp;fopen($files[$x],"rb"); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$data&nbsp;=&nbsp;fread($file,filesize($files[$x])); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fclose($file); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$data&nbsp;=&nbsp;chunk_split(base64_encode($data)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$message&nbsp;.=&nbsp;"Content-Type:&nbsp;{\"application/octet-stream\"};\n"&nbsp;.&nbsp;"&nbsp;name=\"$files[$x]\"\n"&nbsp;.&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"Content-Disposition:&nbsp;attachment;\n"&nbsp;.&nbsp;"&nbsp;filename=\"$files[$x]\"\n"&nbsp;.&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"Content-Transfer-Encoding:&nbsp;base64\n\n"&nbsp;.&nbsp;$data&nbsp;.&nbsp;"\n\n"; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$message&nbsp;.=&nbsp;"--{$mime_boundary}\n"; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;send &nbsp;&nbsp;&nbsp;&nbsp;$ok&nbsp;=&nbsp;@mail($to,&nbsp;$subject,&nbsp;$message,&nbsp;$headers);&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;($ok)&nbsp;{&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;"<p>mail&nbsp;sent&nbsp;to&nbsp;$to!</p>";&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;{&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;"<p>mail&nbsp;could&nbsp;not&nbsp;be&nbsp;sent!</p>";&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;}?>

富国沪深

在核心php邮件功能中检查以下多个文件附件。<?php/**&nbsp;Mail&nbsp;with&nbsp;attachment&nbsp;*/function&nbsp;mail_attachment($filename,&nbsp;$path,&nbsp;$mailto,&nbsp;$from_mail,&nbsp;$from_name,&nbsp;$replyto,&nbsp;$bcc,&nbsp;$subject,&nbsp;$message){&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;$uid&nbsp;=&nbsp;md5(uniqid(time())); &nbsp;&nbsp;&nbsp;&nbsp;$mime_boundary&nbsp;=&nbsp;"==Multipart_Boundary_x{$uid}x";&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;$header&nbsp;=&nbsp;"From:&nbsp;".$from_name."&nbsp;<".$from_mail.">\r\n"; &nbsp;&nbsp;&nbsp;&nbsp;$header&nbsp;.=&nbsp;"Bcc:&nbsp;".$bcc."\r\n"; &nbsp;&nbsp;&nbsp;&nbsp;$header&nbsp;.=&nbsp;"Reply-To:&nbsp;".$replyto."\r\n"; &nbsp;&nbsp;&nbsp;&nbsp;$header&nbsp;.=&nbsp;"MIME-Version:&nbsp;1.0\r\n"; &nbsp;&nbsp;&nbsp;&nbsp;$header&nbsp;.=&nbsp;"Content-Type:&nbsp;multipart/mixed;&nbsp;boundary=\"".$mime_boundary."\"\r\n\r\n"; &nbsp;&nbsp;&nbsp;&nbsp;$header&nbsp;.=&nbsp;"This&nbsp;is&nbsp;a&nbsp;multi-part&nbsp;message&nbsp;in&nbsp;MIME&nbsp;format.\r\n"; &nbsp;&nbsp;&nbsp;&nbsp;$header&nbsp;.=&nbsp;"--".$mime_boundary."\r\n"; &nbsp;&nbsp;&nbsp;&nbsp;$header&nbsp;.=&nbsp;"Content-type:text/html;&nbsp;charset=iso-8859-1\r\n"; &nbsp;&nbsp;&nbsp;&nbsp;$header&nbsp;.=&nbsp;"Content-Transfer-Encoding:&nbsp;7bit\r\n\r\n"; &nbsp;&nbsp;&nbsp;&nbsp;$header&nbsp;.=&nbsp;nl2br($message)."\r\n\r\n"; &nbsp;&nbsp;&nbsp;&nbsp;$header&nbsp;.=&nbsp;"--".$mime_boundary."\r\n"; &nbsp;&nbsp;&nbsp;&nbsp;foreach($filename&nbsp;as&nbsp;$k=>$v){ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$file&nbsp;=&nbsp;$path.$v; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$file_size&nbsp;=&nbsp;filesize($file); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$handle&nbsp;=&nbsp;fopen($file,&nbsp;"r"); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$content&nbsp;=&nbsp;fread($handle,&nbsp;$file_size); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fclose($handle); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$content&nbsp;=&nbsp;chunk_split(base64_encode($content)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$header&nbsp;.=&nbsp;"Content-Type:&nbsp;application/octet-stream;&nbsp;name=\"".$v."\"\r\n";&nbsp;//&nbsp;use&nbsp;different&nbsp;content&nbsp;types&nbsp;here &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$header&nbsp;.=&nbsp;"Content-Transfer-Encoding:&nbsp;base64\r\n"; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$header&nbsp;.=&nbsp;"Content-Disposition:&nbsp;attachment;&nbsp;filename=\"".$v."\"\r\n\r\n"; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$header&nbsp;.=&nbsp;$content."\r\n\r\n"; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$header&nbsp;.=&nbsp;"--".$mime_boundary."--"."\r\n"; &nbsp;&nbsp;&nbsp;&nbsp;}&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(mail($mailto,&nbsp;$subject,&nbsp;"",&nbsp;$header))&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//echo&nbsp;"mail&nbsp;send&nbsp;...&nbsp;OK";&nbsp;//&nbsp;or&nbsp;use&nbsp;booleans&nbsp;here &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;true; &nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//echo&nbsp;"mail&nbsp;send&nbsp;...&nbsp;ERROR!"; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;false; &nbsp;&nbsp;&nbsp;&nbsp;}}?>
随时随地看视频慕课网APP
我要回答