使用PHP发送附带附件的电子邮件,仅显示没有附加文件的字符

apz*_*000 0 php email attachment

我在我的网页上遇到这个问题,我需要通过电子邮件将文件发送给某人,我遇到的问题是邮件到达时没有附件和很多奇怪的字符,我留下了我的代码:

$boundary='Datos Adjuntos';
$boundary.=" ".md5(time());
//Cabeceras del email               

$headers ="From: Example <name@example.com>\r\n";
$headers .= "Reply-To: <name@example.com>\r\n";
//   $headers .="MIME-Version: 1.0\r\n";   
$headers .="Content-Type: multipart/mixed; boundary=\"$boundary\"\r\n\n";

$body="--". $boundary ."\n";
$body .= "Content-Type: text/html; charset=ISO-8859-1\r\n\n";

$archivo=file_get_contents($dir."/".$handle->file_dst_name);

$archivo=chunk_split(base64_encode($archivo));

//Escritura del archivo adjunto
$body .= $body .$ContenidoString. "--" .$boundary. "\n";
//Content-Type: application/msword; name=\"nombre_archivo\"\r\n
$body .= "Content-Transfer-Encoding: base64\r\n";
$body .=  "Content-Disposition: attachment; filename=\"".$handle->file_dst_name."\"\r\n\n$archivo";

$body = $body . "--" . $boundary ."--";

Correo::Enviar("OPERACION","name@example.com", $body,$headers);
Run Code Online (Sandbox Code Playgroud)

$ContentString是电子邮件的html,我使用上传类将文件上传到服务器然后发送,我给你留下了一封我收到的电子邮件:这是其他所有的东西,比如名字和## e电子邮件的内容.

--Datos Adjuntos 1c436ca78c5925e7096267f0eae3a7d3 Content-Transfer-Encoding:base64 Content-Disposition:attachment; filename ="9cbdf187_3251_42e5_aeaa_84df343a227d_4.pdf"JVBERi0xLjQKJdP0zOEKMSAwIG9iago8PAovQ3JlYXRpb25EYXRlKEQ6MjAxMTA4MTYxNTEyNDIt MDUnMDAnKQovQ3JlYXRvcihQREZzaGFycCAxLjMuMTY4NC1nIFwod3d3LnBk

Dav*_*dom 5

虽然你会更好地使用预建的库,正如人们所说,如果由于某种原因你不能/不想,你可以试试这个:

EDITED!

// Make a MIME boundary
$boundary = 'Datos_Adjuntos_'.md5(time());

// Message headers
$headers  = "From: Example <name@example.com>\r\n";
$headers .= "Reply-To: <name@example.com>\r\n";
$headers .= "MIME-Version: 1.0\r\n";   
$headers .= "Content-Type: multipart/mixed; boundary=\"$boundary\"\r\n\r\n";

// For email clients that don't understand MIME messages
$body = "This is a multi-part message in MIME format. If you can read this, something went wrong or your mail client doesn't understand MIME messages.";

// HTML content
$body .= "\r\n--$boundary\r\n";
$body .= "Content-Type: text/html; charset=\"ISO-8859-1\"\r\n\r\n";
$body .= $ContenidoString;

// Attachment
$body .= "\r\n--$boundary\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n";
$body .= "Content-Type: application/msword\r\n";
$body .= "Content-Disposition: attachment; filename=\"$handle->file_dst_name\"\r\n\r\n";
$body .= chunk_split(base64_encode(file_get_contents("$dir/$handle->file_dst_name")));

// Finish the MIME message
$body .= "\r\n--$boundary--";

// (Presumably) send the email
Correo::Enviar("OPERACION","name@example.com",$body,$headers);
Run Code Online (Sandbox Code Playgroud)