2015-04-27 160 views
0

當您留下不包含任何附件的空白郵件時,我遇到了發送郵件附件的問題。郵件的擴展名爲.asc,郵件的文件大小爲3個字節,如果未選擇附件,我想停止包含此.asc文件。多附件電子郵件問題

這是表單代碼

<?php 
/* 
error_reporting(E_ALL); 
ini_set('display_errors', 1); 
*/ 
if(isset($_FILES) && (bool) $_FILES) { 
    $allowedExtensions = array("pdf","doc","docx","gif","jpeg","jpg","png","rtf","txt",""); 
    $files = array(); 
    foreach($_FILES as $name=>$file) { 
     $file_name = $file['name']; 
     $temp_name = $file['tmp_name']; 
     $file_type = $file['type']; 
     $path_parts = pathinfo($file_name); 
     $ext = $path_parts['extension']; 
     if(!in_array($ext,$allowedExtensions)) { 
      die("File $file_name has the extensions $ext which is not allowed"); 
     } 
     array_push($files,$file); 
    } 



    $to = "[email protected]"; 
    $from = "www.jakasstrona.eu"; 
    $subject = $_POST['imie']; 
    $message = 'Imię: ' . $_POST['imie'] . "\r\n" . 
    $message = 'Nazwisko: ' . $_POST['nazwisko'] . "\r\n" . 
    $message = 'PESEL: ' . $_POST['pesel'] . "\r\n" . 
    $message = 'NIP: ' . $_POST['nip'] . "\r\n" . 
    $message = 'Data urodzenia: ' . $_POST['data'] . "\r\n" . 
    $message = 'Data urodzenia: ' . $_POST['data2'] . "\r\n" . 
    $message = 'Data urodzenia: ' . $_POST['data3'] . "\r\n" . 
    $message = 'Obywatelstwo: ' . $_POST['obywatelstwo'] . "\r\n" . 
    $message = 'Typ: ' . $_POST['typ'] . "\r\n" . 
    $message = 'Numer Dokumentu: ' . $_POST['nr'] . "\r\n" . 
    $message = 'Data ważności dokumentu: ' . $_POST['datawaz'] . "\r\n" . 
    $message = 'Adres, ulica, numer budynku, mieszkania: ' . $_POST['long'] . "\r\n" . 
    $message = 'Miejscowosc: ' . $_POST['miejscowosc'] . "\r\n" . 
    $message = 'Kod: ' . $_POST['kod'] . "\r\n" . 
    $message = 'Poczta: ' . $_POST['poczta'] . "\r\n" . 
    $message = 'Województwo: ' . $_POST['woj'] . "\r\n" . 
    $message = 'Telefon komórkowy: ' . $_POST['telefonphon'] . "\r\n" . 
    $message = 'Telefon stacjonarny: ' . $_POST['telefonstac'] . "\r\n" . 
    $message = 'E-mail: ' . $_POST['email'] . "\r\n" . 
    $message = 'Data Wyjazdu: ' . $_POST['datawyj'] . "\r\n" . 
    $message = 'Data Wyjazdu: ' . $_POST['datawyj2'] . "\r\n" . 
    $message = 'Data Wyjazdu: ' . $_POST['datawyj3'] . "\r\n" . 
    $message = 'Doświadczenie zawodowe: ' . $_POST['doswiadczenie'] . "\r\n" . 
    $message = 'Doświadczenie w pracy zagranicą: ' . $_POST['doswiadczenieza'] . "\r\n" . 
    $message = 'Uprawnienia na wózki widłowe: ' . $_POST['uprawnienia'] . "\r\n" . 
    $message = 'Język Angielski: ' . $_POST['jezykang'] . "\r\n" . 
    $message = 'Język Niemiecki: ' . $_POST['jezyknie'] . "\r\n" . 
    $message = 'Uwagi: ' . $_POST['uwagi'] . "\r\n" . 
    $headers = "Od: $from"; 



    // boundary 
    $semi_rand = md5(time()); 
    $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; // random 


    $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; 

    // multi boundary 
    $message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"utf-8\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n"; 
    $message .= "--{$mime_boundary}\n"; 


    for($x=0;$x<count($files);$x++){ 

     /* 
     if (filesize($files[$x] == 3)) 
     { 
      $y=5; 
     } 
     */ 
     $file = fopen($files[$x]['tmp_name'],"rb"); 
     $data = fread($file,filesize($files[$x]['tmp_name'])); 
     fclose($file); 
     $data = chunk_split(base64_encode($data)); 
     $name = $files[$x]['name']; 
     $message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$name\"\n" . 
     "Content-Disposition: attachment;\n" . " filename=\"$name\"\n" . 
     "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n"; 
     $message .= "--{$mime_boundary}\n"; 

    } 

    // sending 

    $ok = mail($to, $subject, $message, $headers); 
    if ($ok) { 
     echo "<p>Mail został wysłany do $to! </p>"; 
    } else { 
     echo "<p>Mail nie został wysłany!</p>"; 
    } 
} 

?> 
+3

不要推出自己的,使用phpmailer或swiftmailer –

回答

0

我想,如果你不發送附件,你不想增加額外的哈希值。看到一個簡單的教程在這裏:http://webcheatsheet.com/php/send_email_text_html_attachment.php

也許把:

if(count($files)) 
$message .= "--{$mime_boundary}\n"; 

雖然一般,我認爲,如果你不這樣做,以瞭解它是如何實現的,使用一個開源庫這樣的正如評論中所建議的那樣。