簡體   English   中英

加載文件,通過電子郵件發送並留在同一頁面上

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

我有這段代碼實際上無法完全正常工作,我收到了包含所有信息的電子郵件,但是電子郵件中沒有包含我要附加的文件...(我唯一的方法是刪除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;
});

您不能僅通過在窗體上序列化就可以輕松地使用AJAX上傳文件。 您將需要一個新的FormData對象,該對象已在此處進行了說明: 如何使用FormData進行Ajax文件上傳

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM