Php获取电子邮件附件


Php Get Email Attachment

我有一个表单,其中有一个用于放置附件的输入文件。碰巧我想通过电子邮件发送表格中的所有数据。然而,在执行此操作之前,我将所有输入的数据重定向到另一个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"; 

但我有一个疑问,哪里有"附件.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);
    }
}

这是我在这个函数中遇到的困难,因为如果我是对的,我应该在这里给form元素分配一个var,这样我就可以把它传递给函数"dadosFormarios"。

一旦进入dadosFormarios(…),它就在那里,我想称之为

form.action = 'index.php?pagina=candidaturasB&'+ qstringA;

重定向到php页面,在那里我将发送带有附件的电子邮件。

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

您正在填写的表单需要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。