我有一个问题,使用PHP发送带附件的邮件

时间:2014-08-20 14:01:25

标签: php email

我在使用php发送带附件的邮件时遇到问题。如果我尝试发送大小为example.doc的{​​{1}},则会在收到2 Mb尺寸为noname的邮件中发送包含损坏附件的邮件。 我使用两个文件php。

第一个1Kb包含:

mailClass.php

第二个文件<?php class mail { function emailWithAttach($fromAdress,$toAdress,$mailSubject,$mailMessageHead, $mailMessageMain,$mailMessageSign,$filePath,$fileName) { $fileatt_name = $fileName; $fileatt = $filePath.$fileName; $fileatt_type = "application/doc"; $email_from = $fromaddress; $email_subject = $mailSubject; $email_message = $mailMessageHead. "<br>"; $email_message .= $mailMessageMain. "<br>"; $email_message .= $mailMessageSign; $email_to = $toAdress; $headers = "From: " .$email_from; $file = fopen ($fileatt."rb"); $data = fread ($file, filesize($fileatt)); fclose($file); $semi_rand = md5(time()); $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; $headers .= "\nMIME-Version: 1.0 \n". "Content-Type : multipart/mixed:\n". "boundary=\"{$mime_boundary}\""; $email_message .= "This is a multip-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_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"; if(@mail($email_to,$email_subject,$email_message,$headers)) { return true; } } } ?> 包含:

index.php

1 个答案:

答案 0 :(得分:1)

使用phpmailer发送邮件:Get it here

它还有一个优点,就是你可以发送很多邮件而不必每次都像php的mail()函数那样打开和关闭连接。