如何使用可选的文件附件字段创建PHP电子邮件表单

时间:2013-04-16 10:29:37

标签: php forms attachment email-attachments

我正在为我的客户端测试PHP表单,当我意识到文件附件不是可选的时,我在这里有完整的代码(很长)。表单工作正常,但如果您不选择任何文件,电子邮件将永远不会到达目的地。无论如何我想发送邮件并在没有附件的情况下收到邮件。

我使用PHP和jForm进行验证。

这是文件附加的输入标记(在form.php文件中有很多输入标记,在这里列出它们)

<input type="file" name="adjunto" id="adjunto">

这是send.php文件(真正的形式太长了,不能在这里发表,我正在写一个较短的版本)

    <?php

    // THE USER CHOOSES FROM A JOB LIST

    $busqueda = $_POST["busqueda-list"];
    $cargo = $_POST["cargo-list"];

    // FILLS PERSONAL INFORMATION

    $nombre = $_POST["nombre"];
    $apellido =  $_POST["apellido"];
    $sexo = $_POST["sexo-list"];       

    // WE STYLE A MESSAGE WITH LOTS OF INPUTS

    $mensaje = 'In the real form here is a table with lots of input styled'; 

    // AND THEN WE ATTACH (in spanish spells adjunto)         

    $adjunto_name=$_FILES["adjunto"]["name"]; 
    $adjunto_type=$_FILES["adjunto"]["type"]; 
    $adjunto_size=$_FILES["adjunto"]["size"]; 
    $adjunto_temp=$_FILES["adjunto"]["tmp_name"]; 

    // IF THERE IS A FILE ATTACHED

if (is_uploaded_file($adjunto_temp)) 

{if($adjunto_type=="application/octet-stream" or $adjunto_type=="text/plain" or $adjunto_type=="application/msword" or $adjunto_type=="application/vnd.openxmlformats-officedocument.wordprocessingml.document" or $adjunto_type=="application/pdf" ) 
        { 

        // MEET THE HEADERS 

        $headers  = "MIME-Version: 1.0\n"; 
        $headers .= "Content-type: text/html; charset=iso-8859-1\n"; 
        $headers .= "From: ".$email."\n";

        // MAIL HEADERS with attachment 

        $fp = fopen($adjunto_temp, "rb"); 
        $file = fread($fp, $adjunto_size); 

        $file = chunk_split(base64_encode($file)); 
        $num = md5(time()); 

        // Normal headers 

        $headers  = "From: ".$email."\r\n"; 
        $headers  .= "MIME-Version: 1.0\r\n"; 
        $headers  .= "Content-Type: multipart/mixed; "; 
        $headers  .= "boundary=".$num."\r\n"; 
        $headers  .= "--$num\r\n"; 

        // This two steps to help avoid spam    

        $headers .= "Message-ID: <".gettimeofday()." TheSystem@".$_SERVER['SERVER_NAME'].">\r\n"; 
        $headers .= "X-Mailer: PHP v".phpversion()."\r\n";          

            // With mensaje 

        $headers .= "Content-Type: text/html; charset=iso-8859-1\r\n"; 
        $headers .= "Content-Transfer-Encoding: 8bit\r\n"; 
        $headers .= "".$mensaje."\n"; 
        $headers .= "--".$num."\n";  

            // Attachment headers 

        $headers  .= "Content-Type:".$adjunto_type." "; 
        $headers  .= "name=\"".$adjunto_name."\"r\n"; 
        $headers  .= "Content-Transfer-Encoding: base64\r\n"; 
        $headers  .= "Content-Disposition: attachment; "; 
        $headers  .= "filename=\"".$adjunto_name."\"\r\n\n"; 
        $headers  .= "".$file."\r\n"; 
        $headers  .= "--".$num."--"; 

        // WE SEND MAIL WITH HEADERS

        @mail("name@example.com", "".$busqueda." ".$cargo."", $mensaje, $headers); 

        fclose($fp); 
    } 

// IF NOT, JUST SEND MAIL

else

@mail("ezemosho@gmail.com", "".$busqueda." ".$cargo."", $mensaje); 

}          
    ?>

我需要一手这个,我尽我所能(我已经在这里和其他论坛搜索),我想这可能是很多人以前遇到过的问题!

谢谢!

2 个答案:

答案 0 :(得分:1)

你需要再提供一个条件

if (is_uploaded_file($adjunto_temp)) {

if($adjunto_type=="application/octet-stream" or $adjunto_type=="text/plain" or $adjunto_type=="application/msword" or $adjunto_type=="application/vnd.openxmlformats-officedocument.wordprocessingml.document" or $adjunto_type=="application/pdf" ) 
    { 

    ........... // your other stuff with respect to headers
@mail("name@example.com", "".$busqueda." ".$cargo."", $mensaje, $headers); 
     }


}
else

@mail("name@example.com", "".$busqueda." ".$cargo."", $mensaje); 

现在邮件将同时发送。

使用您的代码更新

if (is_uploaded_file($adjunto_temp))
{
    if($adjunto_type=="application/octet-stream" or $adjunto_type=="text/plain" or $adjunto_type=="application/msword" or $adjunto_type=="application/vnd.openxmlformats-officedocument.wordprocessingml.document" or $adjunto_type=="application/pdf" )
    {

        // MEET THE HEADERS

        $headers  = "MIME-Version: 1.0\n";
        $headers .= "Content-type: text/html; charset=iso-8859-1\n";
        $headers .= "From: ".$email."\n";

        // MAIL HEADERS with attachment

        $fp = fopen($adjunto_temp, "rb");
        $file = fread($fp, $adjunto_size);

        $file = chunk_split(base64_encode($file));
        $num = md5(time());

        // Normal headers

        $headers  = "From: ".$email."\r\n";
        $headers  .= "MIME-Version: 1.0\r\n";
        $headers  .= "Content-Type: multipart/mixed; ";
        $headers  .= "boundary=".$num."\r\n";
        $headers  .= "--$num\r\n";

        // This two steps to help avoid spam

        $headers .= "Message-ID: <".gettimeofday()." TheSystem@".$_SERVER['SERVER_NAME'].">\r\n";
        $headers .= "X-Mailer: PHP v".phpversion()."\r\n";

        // With mensaje

        $headers .= "Content-Type: text/html; charset=iso-8859-1\r\n";
        $headers .= "Content-Transfer-Encoding: 8bit\r\n";
        $headers .= "".$mensaje."\n";
        $headers .= "--".$num."\n";

        // Attachment headers

        $headers  .= "Content-Type:".$adjunto_type." ";
        $headers  .= "name=\"".$adjunto_name."\"r\n";
        $headers  .= "Content-Transfer-Encoding: base64\r\n";
        $headers  .= "Content-Disposition: attachment; ";
        $headers  .= "filename=\"".$adjunto_name."\"\r\n\n";
        $headers  .= "".$file."\r\n";
        $headers  .= "--".$num."--";

        // WE SEND MAIL WITH HEADERS

        @mail("name@example.com", "".$busqueda." ".$cargo."", $mensaje, $headers);

        fclose($fp);
    }
}
// IF NOT, JUST SEND MAIL

else

{

    // MAIL HEADERS     

    $headers  = "MIME-Version: 1.0\n"; 
    $headers .= "Content-type: text/html; charset=iso-8859-1\n"; 
    $headers .= "From: ".$email."\n";

    // This two steps to help avoid spam    

    $headers .= "Message-ID: <".gettimeofday()." TheSystem@".$_SERVER['SERVER_NAME'].">\r\n"; 
    $headers .= "X-Mailer: PHP v".phpversion()."\r\n";

    @mail("ezemosho@gmail.com", "".$busqueda." ".$cargo."", $mensaje);

}

答案 1 :(得分:0)

将else语句放在第一个if条件中,在您使用else的代码中为condition2。

if (condition1) {
    if (condition2) {
        .........
        .........
    }
} else {

}