Trying to add attachment to PHP email from html form -



Trying to add attachment to PHP email from html form -

i struggling , have tried many different methods form attach file email sent through php.

this html form:

<form id="form1" enctype="multipart/form-data" action="submit/applicationscript.php" method="post" name="form1"> <input checked type="radio" name="school" value="english martyres"/> <input type="radio" name="stop" value="stop1" /> <input type="radio" name="stop" value="stop2" /> <input type="radio" name="stop" value="stop3" /> <input type="radio" name="stop" value="stop4" /> <input type="checkbox" name="mon" value="monday" /> <input type="checkbox" name="tue" value="tuesday" /> <input type="checkbox" name="wed" value="wednesday" /> <input type="checkbox" name="thu" value="thursday" /> <input type="checkbox" name="fri" value="friday" /> <input type="text" class="text" name="name" required placeholder="first name" /></div> <input type="text" class="text" name="surname" required placeholder="surname" /></div> <input type="text" class="text" name="dob" required maxlength="10" placeholder="date of birth" /> <input type="file" name='uploaded_file' required /> <input type="submit" id="form1" name="submit" value="submit" onclick="document.form1.submit()"> </form>

this php:

<?php // read post request params global vars $to = $_post['my@email.com']; $from = $_post['from@email.co.uk']; $subject = $_post['subject']; $message = $_post['message']; // obtain file upload vars $fileatt = $_files['fileatt']['tmp_name']; $fileatt_type = $_files['fileatt']['type']; $fileatt_name = $_files['fileatt']['name']; $headers = "from: $from"; if (is_uploaded_file($fileatt)) { // read file attached ('rb' = read binary) $file = fopen($fileatt,'rb'); $data = fread($file,filesize($fileatt)); fclose($file); // generate boundary string $semi_rand = md5(time()); $mime_boundary = "==multipart_boundary_x{$semi_rand}x"; // add together headers file attachment $headers .= "\nmime-version: 1.0\n" . "content-type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; // add together multipart boundary above plain message $message = "this multi-part message in mime format.\n\n" . "--{$mime_boundary}\n" . "content-type: text/plain; charset=\"iso-8859-1\"\n" . "content-transfer-encoding: 7bit\n\n" . $message . "\n\n"; // base64 encode file info $data = chunk_split(base64_encode($data)); // add together file attachment message $message .= "--{$mime_boundary}\n" . "content-type: {$fileatt_type};\n" . " name=\"{$fileatt_name}\"\n" . //"content-disposition: attachment;\n" . //" filename=\"{$fileatt_name}\"\n" . "content-transfer-encoding: base64\n\n" . $data . "\n\n" . "--{$mime_boundary}--\n"; } // send message $ok = @mail($to, $subject, $message, $headers); if ($ok) { echo "<p>mail sent! yay php!</p>"; } else { echo "<p>mail not sent. sorry!</p>"; } ?>

it either sends , not attach attachment or unable send or $message empty , hence not send.

cannot please help me, need work , don't have great knowledge of php, have looked @ many articles cannot head around it.

i utilize phpmailer , haven't had issues.

get here: https://github.com/phpmailer/phpmailer

here example:

<?php require 'phpmailerautoload.php'; $mail = new phpmailer; $mail->issmtp(); // set mailer utilize smtp $mail->host = 'smtp1.example.com;smtp2.example.com'; // specify main , backup smtp servers $mail->smtpauth = true; // enable smtp authentication $mail->username = 'user@example.com'; // smtp username $mail->password = 'secret'; // smtp password $mail->smtpsecure = 'tls'; // enable encryption, 'ssl' accepted $mail->from = 'from@example.com'; $mail->fromname = 'mailer'; $mail->addaddress('joe@example.net', 'joe user'); // add together recipient $mail->addaddress('ellen@example.com'); // name optional $mail->addreplyto('info@example.com', 'information'); $mail->addcc('cc@example.com'); $mail->addbcc('bcc@example.com'); $mail->wordwrap = 50; // set word wrap 50 characters $mail->addattachment('/var/tmp/file.tar.gz'); // add together attachments $mail->addattachment('/tmp/image.jpg', 'new.jpg'); // optional name $mail->ishtml(true); // set email format html $mail->subject = 'here subject'; $mail->body = 'this html message body <b>in bold!</b>'; $mail->altbody = 'this body in plain text non-html mail service clients'; if(!$mail->send()) { echo 'message not sent.'; echo 'mailer error: ' . $mail->errorinfo; } else { echo 'message has been sent'; }

php html forms email email-attachments

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -