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

时间:2017-03-07 23:36:52

标签: javascript php jquery html ajax

我有这个代码实际上没有完全正常工作,我收到包含所有信息的电子邮件,但电子邮件不包含我要附加的文件...(我得到它的唯一方法是删除jquery ajax函数)知道为什么? (我这样做是因为我想在提交表格后在同一页面收到提醒)感谢您的帮助!

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

    $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

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;
});

1 个答案:

答案 0 :(得分:1)

只需在表单上使用序列化,就无法上传轻松使用AJAX的文件。您将需要新的FormData对象,此处已在此处说明:How to use FormData for ajax file upload