php - PHPMailer - Code seems to do nothing -
i need help, getting undefined error , being caused code deals attachments @ top of php file. know because if comment out don't error.
the next thing using xampp , followed couple tutorials sendmail in php.ini file , c://xampp/sendmail/senmail.ini setup hoping send email via gmail when tested form haven't gotten through yet.
i don't know begin, i've used 2 tutorials make php code, please take look.
<?php header('content-type: application/json'); $status = array( 'type'=>'success', 'message'=>'thank contact us. possible contact ' ); //added deal files require_once('../phpmailer/class.phpmailer.php') //get uploaded file information $name_of_uploaded_file = basename($_files['uploaded_file']['name']); //get file extension of file $type_of_uploaded_file = substr($name_of_uploaded_file, strrpos($name_of_uploaded_file, '.') + 1); $size_of_uploaded_file = $_files["uploaded_file"]["size"]/1024;//size in kbs //settings $max_allowed_file_size = 10240; // size in kb $allowed_extensions = array("jpg", "jpeg", "gif", "bmp","png"); //validations if($size_of_uploaded_file > $max_allowed_file_size ) { $errors .= "\n size of file should less $max_allowed_file_size (~10mb). file attempted upload large. reduce size, open file in image editor , change image size , resave file."; } //------ validate file extension ----- $allowed_ext = false; for($i=0; $i<sizeof($allowed_extensions); $i++) { if(strcasecmp($allowed_extensions[$i],$type_of_uploaded_file) == 0) { $allowed_ext = true; } } if(!$allowed_ext) { $errors .= "\n uploaded file not supported file type. ". " following file types supported: ".implode(',',$allowed_extensions); } //copy temp. uploaded file uploads folder - make sure folder exists on server , has 777 permission $path_of_uploaded_file = $upload_folder . $name_of_uploaded_file; $tmp_path = $_files["uploaded_file"]["tmp_name"]; if(is_uploaded_file($tmp_path)) { if(!copy($tmp_path,$path_of_uploaded_file)) { $errors .= '\n error while copying uploaded file'; } } //--end $name = @trim(stripslashes($_post['name'])); $clientemail = @trim(stripslashes($_post['email'])); $subject = @trim(stripslashes($_post['subject'])); $message = @trim(stripslashes($_post['message'])); $body = 'name: ' . $name . "\n\n" . 'email: ' . $clientemail . "\n\n" . 'subject: ' . $subject . "\n\n" . 'message: ' . $message; //$success = @mail($email_to, $subject, $body, 'from: <'.$email_from.'>'); //this old php (no file attachment allowed) $email = new phpmailer(); $email->from = $clientemail; $email->fromname = $name; $email->subject = $subject; $email->body = $body; $email->addaddress( 'badrush14@hotmail.com' ); //send email $file_to_attach = 'path_of_your_file_here'; $success = $email->addattachment( $path_of_uploaded_file , $name_of_uploaded_file ); return $email->send(); echo json_encode($status); die;
first; returning boolean flag before sending json response. once return value, statement below return keyword ignored. thus, might not getting result want. second; might need explicitly tell phpmailer how send email. third, forgot closing semi-colon ( ; ) on require_once line. code below has comments indicate may have missed:
<?php header('content-type: application/json'); // should assume email not sent @ first until know more. // add attachment key our status array indicate status of our attachment: $status = array( 'type' =>'error', 'message' =>'couldn\'t send email @ time. went wrong', 'attachement' =>'couldn\'t attach uploaded file email.' ); //added deal files // missed closing semi-colon here... ;-) require_once('/phpmailer/class.phpmailer.php'); //double check path //get uploaded file information $name_of_uploaded_file = basename($_files['uploaded_file']['name']); //get file extension of file $type_of_uploaded_file = substr($name_of_uploaded_file, strrpos($name_of_uploaded_file, '.') + 1); $size_of_uploaded_file = $_files["uploaded_file"]["size"]/1024;//size in kbs //settings $max_allowed_file_size = 10240; // size in kb $allowed_extensions = array("jpg", "jpeg", "gif", "bmp","png"); //validations if($size_of_uploaded_file > $max_allowed_file_size ) { $errors .= "\n size of file should less $max_allowed_file_size (~10mb). file attempted upload large. reduce size, open file in image editor , change image size , resave file."; } //------ validate file extension ----- $allowed_ext = false; for($i=0; $i<sizeof($allowed_extensions); $i++) { if(strcasecmp($allowed_extensions[$i],$type_of_uploaded_file) == 0) { $allowed_ext = true; } } if(!$allowed_ext) { $errors .= "\n uploaded file not supported file type. ". " following file types supported: ".implode(',',$allowed_extensions); } //copy temp. uploaded file uploads folder - make sure folder exists on server , has 777 permission $path_of_uploaded_file = $upload_folder . $name_of_uploaded_file; $tmp_path = $_files["uploaded_file"]["tmp_name"]; if(is_uploaded_file($tmp_path)) { if(!copy($tmp_path,$path_of_uploaded_file)) { $errors .= '\n error while copying uploaded file'; } } //--end $name = @trim(stripslashes($_post['name'])); $clientemail = @trim(stripslashes($_post['email'])); $subject = @trim(stripslashes($_post['subject'])); $message = @trim(stripslashes($_post['message'])); $body = 'name: ' . $name . "\n\n" . 'email: ' . $clientemail . "\n\n" . 'subject: ' . $subject . "\n\n" . 'message: ' . $message; //$success = @mail($email_to, $subject, $body, 'from: <'.$email_from.'>'); //this old php (no file attachment allowed) $email = new phpmailer(); $email->from = $clientemail; $email->fromname = $name; $email->subject = $subject; $email->body = $body; $email->addaddress( 'badrush14@hotmail.com' ); //send email // explicitly tell php-mailer how send email... in case using php built mail function $email->ismail(); // addattachment method returns boolean flag: true when attachment successful & false otherwise: // knowing this, may use within conditional block such // when true, update our status array... if($email->addattachment( $path_of_uploaded_file , $name_of_uploaded_file )){ $status['attachment'] = 'uploaded file attached email.'; } // now, try send email anyway: try{ $emailstatus = $email->send(); $status['type'] = 'success'; $status['message'] = 'thank contact us. possible contact you.'; }catch(exception $e){ $status['type'] ='error'; $status['message'] ='couldn\'t send email @ time. went wrong'; } // when return boolean flag of result of sending , email; program ends //and cannot respond json... thus, line not helpful you... //return $email->send(); // simply, return json data... die (json_encode($status));
Comments
Post a Comment