简体   繁体   English

加载文件,通过电子邮件发送并留在同一页面上

[英]Load a file, send it by email and stay on the same page

I have this code which actually is not working completely, I get the email with all the info, but the email does not include the file that I'm trying to attach... (the only way I get it is deleting the jquery ajax function) Any idea why? 我有这段代码实际上无法完全正常工作,我收到了包含所有信息的电子邮件,但是电子邮件中没有包含我要附加的文件...(我唯一的方法是删除jquery ajax函数)知道为什么吗? (I did this because I want to get an alert in the same page after submit the form) Thanks for your help! (我这样做是因为我想在提交表单后在同一页面中收到警报)谢谢您的帮助!

HTML 的HTML

<form class="ajax_nueva_idea" action="php/send_idea.php" method="post" enctype="multipart/form-data">


  <div>
    <input required="" id="nombreproyecto" name="nombreproyecto" type="text" class="validate">
  </div>

  <div>
    <input required="" id="sector_for" name="sector_for" type="text" class="validate">
  </div>

  <div>
    <input required="" id="ubigeo_for" type="text" name="ubigeo_for" class="validate">
  </div>

  <div>
    <input required="" id="videoex_for" name="videoex_for" type="text" class="validate">
  </div>

  <div>
   <select required="" id="etapa_for" name="etapa_for">
    <option value="0"</option>
    <option value="1">1/4</option>
    <option value="2">2/4</option>
    <option value="3">3/4</option>
    <option value="4">4/4</option>
  </select>
  </div>

  <div>
    <input name="attachment" type="file">
  </div>


  <div>
    <textarea required="" id="info_for" name="info_for"></textarea>
  </div>

  <div>
    <button type="submit" name="action" id="enviar_proyecto">Guardar
    </button>
  </div>

</form>

PHP 的PHP

<?php

    $to = 'example@msn.com';
    $nombreproyecto = $_POST['nombreproyecto']; 
    $sector = $_POST['sector_for'];
    $ciudad = $_POST['ubigeo_for'];
    $video = $_POST['videoex_for'];
    $etapa = $_POST['etapa_for'];
    $subject = "'gobig.com.co' Nuevo proyecto "."'".$nombreproyecto."'"; 

    $message = 

    "Cargo: ".$nombreproyecto."\n".
    "Sector o industria: ". $sector ."\n". 
    "Ciudad: ".$ciudad ."\n".
    "Link video:". $video ."\n". 
    "Etapa del proyecto:".$etapa."\n" . 
    "Frase convencer: ".$_POST['info_for'];

    $tmpName = $_FILES['attachment']['tmp_name']; 
    $fileType = $_FILES['attachment']['type']; 
    $fileName = $_FILES['attachment']['name']; 

    $headers = "From: $nombreproyecto"; 

    if (file($tmpName)) { 

      $file = fopen($tmpName,'rb'); 
      $data = fread($file,filesize($tmpName)); 
      fclose($file); 


      $randomVal = md5(time()); 
      $mimeBoundary = "==Multipart_Boundary_x{$randomVal}x"; 


      $headers .= "\nMIME-Version: 1.0\n"; 
      $headers .= "Content-Type: multipart/mixed;\n" ;
      $headers .= " boundary=\"{$mimeBoundary}\""; 


      $message = "This is a multi-part message in MIME format.\n\n" . 
      "--{$mimeBoundary}\n" . 
      "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . 
      "Content-Transfer-Encoding: 7bit\n\n" . 
      $message . "\n\n"; 


      $data = chunk_split(base64_encode($data)); 


      $message .= "--{$mimeBoundary}\n" . 
      "Content-Type: {$fileType};\n" . 
      " name=\"{$fileName}\"\n" . 
      "Content-Transfer-Encoding: base64\n\n" . 
      $data . "\n\n" . 
      "--{$mimeBoundary}--\n"; 
    } 

    $flgchk = mail ("$to", "$subject", "$message", "$headers"); 

    if($flgchk){
      //echo "A email has been sent to: $to";
     }
    else{
      //echo "Error in Email sending";
    }


?>

JAVASCRIPT JAVASCRIPT

jQuery('.ajax_nueva_idea').submit( function() {

    $.ajax({
        url     : $(this).attr('action'),
        type    : $(this).attr('method'),
        data    : $(this).serialize(),

        success : function( data ) {
                    alert('Idea guardada exitosamente!');
                    $('#enviar_proyecto').html('Guardado <i class="material-icons right">check</i>');

                  },
        error   : function(){
                     alert('Something wrong');

                  }
    });

    return false;
});

You cannot upload files that easily using AJAX by simply using serialize on form. 您不能仅通过在窗体上序列化就可以轻松地使用AJAX上传文件。 You'll need new FormData object, which is already explained here: How to use FormData for ajax file upload 您将需要一个新的FormData对象,该对象已在此处进行了说明: 如何使用FormData进行Ajax文件上传

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM