php - Send image attachment using phpmailer -
i'm using code upload , attach image through form, , send mail (as html not smtp). once enter (the code) page in web, can see output fine, can click "choose file" , choose file directory. but, can see right away when enterind page echo "invalid file" because image didn't upload yet me, code runs , not waiting me choose something. missing triger submit choise , send it?
when configuring sent html did, $mail->send() enough , when code reaches command mail attach sent?? or need other triger send it?
thank you,
<?php include_once("functions.php"); // process $action = isset($_post["action"]) ? $_post["action"] : ""; if (empty($action)) { $output = "<form action='#'> <h1>header: </h1> <label for='image'>upload: </label> <input type='file' id='image' name='image' maxlength=50 >"; } echo $output; $image = $_post["image"]; uploadimage($image); require("class.phpmailer.php"); $email_to = "some@gmail.com"; // 1 recieves email $email_from = "someone@someone.net"; $dir = "uploads/$filename"; chmod("uploads",0777); function uploadimage($image){ if ((($_files["image"]["type"] == "image/gif") || ($_files["image"]["type"] == "image/jpeg") || ($_files["image"]["type"] == "image/pjpeg") || ($_files["image"]["type"] == "image/jpg") || ($_files["image"]["type"] == "image/png")) && ($_files["image"]["size"] < 2097152) && (strlen($_files["image"]["name"]) < 51)){ if ($_files["image"]["error"] > 0){ echo "return code: " . $_files["image"]["error"]; } else{ echo "upload: " . $_files["image"]["name"] . "<br />"; echo "type: " . $_files["image"]["type"] . "<br />"; echo "size: " . ($_files["image"]["size"] / 1024) . " kb<br />"; echo "temp file: " . $_files["image"]["tmp_name"] . "<br />"; if (file_exists("images/" . $_files["image"]["name"])){ echo $_files["image"]["name"] . " exists. "; } else{ move_uploaded_file($_files["image"]["tmp_name"], "images/" . $_files["image"]["name"]); } } }else{ echo "invalid file"; } $filename = $_files["image"]["type"]; $dir = "uploads/$filename"; chmod("uploads",0777); $success = copy($_files[images][tmp_name], $dir); if ($success) { echo " files uploaded successfully<br>"; sendit(); } } function sendit() { // global $attachments,$email_to,$email_msg,$email_subject,$email_from; // $mail = new phpmailer(); //$mail->issmtp();// send via smtp //$mail->host = "localhost"; // smtp servers //$mail->smtpauth = false; // turn on/off smtp authentication $mail->from = $email_from; $mail->addaddress($email_to); $mail->addreplyto($email_from); $mail->wordwrap = 50;// set word wrap //now attach files submitted $mail->addattachment("uploads"."/".$_files["image"]["type"]); // $mail->ishtml(true);// send html if(!$mail->send()) { echo 'message not sent.'; echo 'mailer error: ' . $mail->errorinfo; exit; } } ?>
you should firing uploadimage function when there $action:
...previous code... if (empty($action)) { ?> <form action=''> <h1>header: </h1> <label for='image'>upload: </label> <input type='file' id='image' name='image' maxlength=50 > </form> <?php exit; // stop upload script running } $image = $_post["image"]; uploadimage($image); require("class.phpmailer.php"); ... rest of code ...
Comments
Post a Comment