forms - attachment in a mail php -
good evening all, writing because have problems attach file loaded using form email. did not understand if have save before attaching folder or not .... code, mail arrives, without attachment. tell me wrong?
$allegato=$_files['userfile']['tmp_name']; $allegato_name=$_files['userfile']['name']; $allegato_tipo=$_files['userfile']['type']; $uploaddir = '/uploads/'; $uploadfile = $uploaddir . basename($_files['userfile']['name']); $headers = 'from: '.$email.'' . "\r\n" . 'reply-to: pir.stefania@tiscali.it' . "\r\n" . 'x-mailer: php/' . phpversion(); ; if ($_files["userfile"]["error"] > 0){ echo "return code: " . $_files["userfile"]["error"] . "<br>"; }else{ if (file_exists("uploads/" . $_files["userfile"]["name"])){ echo $_files["userfile"]["name"] . " exists. "; }else{ move_uploaded_file($_files["userfile"]["tmp_name"], "uploads/" . $_files["userfile"]["name"]); echo "stored in: " . "uploads/" . $_files["userfile"]["name"]; } } if(is_uploaded_file($allegato)){ $file = fopen($allegato,'rb'); $data = fread($file, filesize($allegato)); fclose($file); $data = chunk_split(base64_encode($data)); $semi_rand = md5(time()); $mime_boundary = "==multipart_boundary_x{$semi_rand}x"; $headers .= "\nmime-version: 1.0\n"; $headers .= "content-type: multipart/mixed;\n"; $headers .= " boundary=\"{$mime_boundary}\""; $msg .= "this multi-part message in mime format.\n\n"; $msg .= "--{$mime_boundary}\n"; $msg .= "content-type: text/plain; charset=\"iso-8859-1\"\n"; $msg .= "content-transfer-encoding: 7bit\n\n"; $msg .= $messaggio . "\n\n"; $msg .= "--{$mime_boundary}\n"; $msg .= "content-disposition: attachment;\n"; $msg .= " filename=\"{$allegato_name}\"\n"; $msg .= "content-transfer-encoding: base64\n\n"; $msg .= $data . "\n\n"; $msg .= "--{$mime_boundary}--\n }else{ $msg = $messaggio; } if (mail($destinatario, $oggetto, $msg, $headers)){ echo "<p>mail inviata con successo!</p>"; }else{ echo "<p>errore!</p>"; }
/fine script/
mail($destinatario, $oggetto, $messaggio, $headers) ;
i'd recommend using phpmailer when you're getting attachments. simple attachment example:
<?php require_once ('../class.phpmailer.php'); $mail = new phpmailer(); // defaults using php "mail()" $mail -> issendmail(); // telling class use sendmail transport $body = file_get_contents('contents.html'); $body = preg_replace('/[\]/i', '', $body); $mail -> setfrom('name@yourdomain.com', 'first last'); $mail -> addreplyto("name@yourdomain.com", "first last"); $address = "whoto@otherdomain.com"; $mail -> addaddress($address, "john doe"); $mail -> subject = "phpmailer test subject via sendmail, basic"; $mail -> altbody = "to view message, please use html compatible email viewer!"; // optional, comment out , test $mail -> msghtml($body); $mail -> addattachment("images/phpmailer.gif"); // attachment $mail -> addattachment("images/phpmailer_mini.gif"); // attachment if (!$mail -> send()) { echo "mailer error: " . $mail -> errorinfo; } else { echo "message sent!"; } ?>
Comments
Post a Comment