i have problem sending mail attachments , when leave empty not including attachment. message comes mail extension .asc, has file size 3 bytes , stop including .asc file when attachment not chosen.
this form code
<?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 $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 extensions $ext not allowed"); } array_push($files,$file); } $to = "kontakt@lookslike.pl"; $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 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 $to! </p>"; } else { echo "<p>mail nie został wysłany!</p>"; } } ?>
i think if don't send attachment, don't want add hash. see simple tutorial here: http://webcheatsheet.com/php/send_email_text_html_attachment.php
maybe put:
if(count($files)) $message .= "--{$mime_boundary}\n";
though generally, agree if aren't doing learn how done, use open source library kind of thing suggested in comments.