我有一個問題,我想通過電子郵件發送一個文件(Photoshop,.gif等)文件與PHP。 我將數據從HTML文檔發佈到以下PHP文件。 問題:我收到電子郵件但文件已損壞。 任何想法,爲什麼這不起作用?通過郵件傳遞文件()PHP
$to = '[email protected]';
$subject = 'Order';
$name = strip_tags($_POST['name']);
$email = strip_tags($_POST['email']);
$plz = strip_tags($_POST['plz']);
$city = strip_tags($_POST['city']);
$street = strip_tags($_POST['street']);
$nr = strip_tags($_POST['nr']);
$plzdelivery = strip_tags($_POST['plz-delivery']);
$citydelivery = strip_tags($_POST['city-delivery']);
$streetdelivery = strip_tags($_POST['street-delivery']);
$nrdelivery = strip_tags($_POST['nr-delivery']);
$buttonsize = strip_tags($_POST['button-size']);
$count = strip_tags($_POST['count']);
$message = "Name: ".$name." Email: ".$email." Plz: ".$plz." Stadt: ".$city." Strasse: ".$street." Hnr: ".$nr." Button: ".$buttonsize." Anzahl: ".$count;
$type = $_FILES['file']['type'];
$attachment = chunk_split(base64_encode(file_get_contents($_FILES['file']['tmp_name'])));
$filename = $_FILES['file']['name'];
$boundary =md5(date('r', time()));
$headers = "From: [email protected]\r\nReply-To: [email protected]";
$headers .= "\r\nMIME-Version: 1.0\r\nContent-Type: multipart/mixed; boundary=\"_1_$boundary\"";
$message="This is a multi-part message in MIME format.
--_1_$boundary
Content-Type: multipart/alternative; boundary=\"_2_$boundary\"
--_2_$boundary
Content-Type: text/plain; charset=\"iso-8859-1\"
Content-Transfer-Encoding: 7bit
$message
--_2_$boundary--
--_1_$boundary
Content-Type: application/octet-stream; name=\"$filename\"
Content-Transfer-Encoding: base64
Content-Disposition: attachment
$attachment
--_1_$boundary--";
mail($to, $subject, $message, $headers);
如果你堅持使用這種繁瑣的代碼發送郵件,那麼請自己搞清楚。我們不在這裏修補互聯網上的隨機郵件/ MIME工藝代碼片段。其他人都可以使用PHPMailer或SwiftMailer,它有內置的支持。 – mario 2013-02-24 20:41:42
'mail()'是無用的垃圾。如果可能的話,千萬不要使用它。 – 2013-02-24 20:44:30
所以你只發送字符串,而不是文件。你可以在這裏閱讀關於發送郵件通過PHP http://webcheatsheet.com/PHP/send_email_text_html_attachment.php – Eugen 2013-02-24 20:48:46