title: Php Get Email Attachment
body: <p>i have a form where i have an input file to place an attachment. It happens that all the data i have in the form i want to send by email. However, before i do that, i redirect all the data inputed to another php page and receive it with get. </p>
  
  <p>So my first question is, how can i get the content of the attachment in other php page by get?</p>
  
  <p>After that, after i verify all the data in the new php page, i pretend to send it by email. I plan to use this code:</p>
  
  <pre><code>    //define the receiver of the email 
  $to = 'myemail@something.com'; 
  //define the subject of the email 
  $subject = 'Test email with attachment'; 
  //create a boundary string. It must be unique 
  //so we use the MD5 algorithm to generate a random hash 
  $random_hash = md5(date('r', time())); 
  //define the headers we want passed. Note that they are separated with \r\n 
  $headers = "From: webmaster@example.com\r\nReply-To: webmaster@example.com"; 
  //add boundary string and mime type specification 
  $headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\""; 
  //read the atachment file contents into a string,
  //encode it with MIME base64,
  //and split it into smaller chunks
  $attachment = chunk_split(base64_encode(file_get_contents('attachment.zip'))); 
  //define the body of the message. 
  ob_start(); //Turn on output buffering 
  ?&gt; 
  --PHP-mixed-&lt;?php echo $random_hash; ?&gt;  
  Content-Type: multipart/alternative; boundary="PHP-alt-&lt;?php echo $random_hash; ?&gt;" 
  
  --PHP-alt-&lt;?php echo $random_hash; ?&gt;  
  Content-Type: text/plain; charset="iso-8859-1" 
  Content-Transfer-Encoding: 7bit
  
  Hello World!!! 
  This is simple text email message. 
  
  --PHP-alt-&lt;?php echo $random_hash; ?&gt;  
  Content-Type: text/html; charset="iso-8859-1" 
  Content-Transfer-Encoding: 7bit
  
  &lt;h2&gt;Hello World!&lt;/h2&gt; 
  &lt;p&gt;This is something with &lt;b&gt;HTML&lt;/b&gt; formatting.&lt;/p&gt; 
  
  --PHP-alt-&lt;?php echo $random_hash; ?&gt;-- 
  
  --PHP-mixed-&lt;?php echo $random_hash; ?&gt;  
  Content-Type: application/zip; name="attachment.zip"  
  Content-Transfer-Encoding: base64  
  Content-Disposition: attachment  
  
  &lt;?php echo $attachment; ?&gt; 
  --PHP-mixed-&lt;?php echo $random_hash; ?&gt;-- 
  
  &lt;?php 
  //copy current buffer contents into $message variable and delete current output buffer 
  $message = ob_get_clean(); 
  //send the email 
  $mail_sent = @mail( $to, $subject, $message, $headers ); 
  //if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 
  echo $mail_sent ? "Mail sent" : "Mail failed"; 
  </code></pre>
  
  <p>But i have a doubt, where there is "attachment.zip" what should i put? The variable that will get the data of the attachment on this new php page?</p>
  
  <p>Thanks in advance!</p>
  
  <p>Forgetting the part above:</p>
  
  <p>this is my declaration and submit button on the form:</p>
  
  <pre><code>&lt;form id="formElem" name="formElem" enctype="multipart/form-data" action="" method="post"&gt;
  
  &lt;button name='enviar_candidatura' id='enviar_candidatura' value='enviar_candidatura' onclick='return false;' type='submit'&gt;
  </code></pre>
  
  <p>when i click on the button above, i enter on the following jquery function:</p>
  
  <pre><code>$('#enviar_candidatura').bind('click',function(){
  
      var conta_Duplicates;
      conta_Duplicates=dadosImportantes();
      //alert("Deu");
      var preenchimentoForm=true;
      //alert("Contasssss"+conta1);
      //var eventos=$countEventos;
      var eventos=conta_Duplicates[2];
      //alert("Wiggins"+eventos);
      //var empregos=$countEmpregos;
      var empregos=conta_Duplicates[1];
      //var cursos=$countCursos;
      var cursos=conta_Duplicates[0];
  
      //alert($countEmpregos);
      if($('#formElem').data('errors')){
          preenchimentoForm=false;
  
          dadosFormularios(form, preenchimentoForm, cursos, empregos, eventos);
          return false;
      }
      else{
          dadosFormularios(form, preenchimentoForm, cursos, empregos, eventos);
      }
  }
  </code></pre>
  
  <p>That's in this function i'm having difficult, because if i'm right, i should have a var here assigned to the form element so that i can pass her to the function "dadosFormularios".</p>
  
  <p>Once inside the dadosFormularios(...), it's there i want to call the </p>
  
  <pre><code>form.action = 'index.php?pagina=candidaturasB&amp;'+ qstringA;
  </code></pre>
  
  <p>to redirect to the php page where i will send the email with the attachment.</p>
  
  <p>Hope i was clear and sorry for some variables in a foreign language, hope it's not a problem.</p>