我有一个表单,我有一个输入文件来放置附件。碰巧我希望通过电子邮件发送表格中的所有数据。但是,在我这样做之前,我将所有数据重新定向到另一个php页面并使用get接收它。
所以我的第一个问题是,如何通过get获取其他php页面中的附件内容?
之后,在我验证新php页面中的所有数据后,我假装通过电子邮件发送它。我打算使用这段代码:
//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";
但我怀疑,哪里有“attachment.zip”我应该放什么?在这个新的php页面上获取附件数据的变量?
提前致谢!
忘记上面的部分:
这是我在表单上的声明和提交按钮:
<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'>
当我点击上面的按钮时,我输入以下jquery函数:
$('#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);
}
}
在这个函数中我很难,因为如果我是对的,我应该在这里分配一个var给表单元素,这样我就可以将她传递给函数“dadosFormularios”。
一旦进入dadosFormularios(...),就在那里我想打电话给
form.action = 'index.php?pagina=candidaturasB&'+ qstringA;
重定向到php页面,我将发送带附件的电子邮件。
希望我对外语中的一些变量很清楚并抱歉,希望这不是问题。
答案 0 :(得分:0)
您要填写的表单需要enctype="multipart/form-data"
,并且需要method="post"
才能上传。
<form method="post" action="" enctype="multipart/form-data" id="myform" onsubmit="return checkForm(this)";>
<input type="file" name="filename" />
// ...........
</form>
在您的发送邮件中,您可以使用$_FILES['filename']['tmp_name']
获取该文件,请注意filename
必须与输入的名称相同:
$attachment = chunk_split(base64_encode(file_get_contents($_FILES['filename']['tmp_name'])));
for function
<script type="text/javascript">
function checkForm(form){
// process fieds
return checkNextFunction(form);
}
function checkNextFunction(form){
form.action = 'myurl.php';
return true;
}
</script>
检查JSFiddle。