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.
So my first question is, how can i get the content of the attachment in other php page by get?
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:
//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
?>
--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>"
--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
Hello World!!!
This is simple text email message.
--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
<h2>Hello World!</h2>
<p>This is something with <b>HTML</b> formatting.</p>
--PHP-alt-<?php echo $random_hash; ?>--
--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: application/zip; name="attachment.zip"
Content-Transfer-Encoding: base64
Content-Disposition: attachment
<?php echo $attachment; ?>
--PHP-mixed-<?php echo $random_hash; ?>--
<?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";
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?
Thanks in advance!
Forgetting the part above:
this is my declaration and submit button on the form:
<form id="formElem" name="formElem" enctype="multipart/form-data" action="" method="post">
<button name='enviar_candidatura' id='enviar_candidatura' value='enviar_candidatura' onclick='return false;' type='submit'>
when i click on the button above, i enter on the following jquery function:
$('#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);
}
}
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”.
Once inside the dadosFormularios(…), it’s there i want to call the
form.action = 'index.php?pagina=candidaturasB&'+ qstringA;
to redirect to the php page where i will send the email with the attachment.
Hope i was clear and sorry for some variables in a foreign language, hope it’s not a problem.
The form you are completing needs to have
enctype="multipart/form-data"and needs to havemethod="post"for upload to work.like this:
After that in your send mail you can get the file with
$_FILES['filename']['tmp_name'], notice thatfilenamehas to be same as input’s name:for function
Check JSFiddle.