3

我有一个表格,我有一个输入文件来放置附件。碰巧我拥有的所有数据都以我想通过电子邮件发送的形式发送。但是,在我这样做之前,我将所有输入的数据重定向到另一个 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 页面。

希望我对外语中的一些变量感到清楚和抱歉,希望这不是问题。

4

1 回答 1

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'])));

功能

<script type="text/javascript">
    function checkForm(form){
        // process fieds
        return checkNextFunction(form);
    }

    function checkNextFunction(form){
         form.action = 'myurl.php';
         return true;
    }
</script>

检查JSFiddle

于 2012-08-29T09:32:38.617 回答