0

我正在为我的客户测试一个 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); 

}          
    ?>

请帮我解决这个问题,我正在尽我所能(我已经在这里和其他论坛搜索过),我认为这可能是很多人以前遇到的问题!

谢谢!

4

2 回答 2

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

}
于 2013-04-16T10:38:05.237 回答
0

将 else 语句用于您的第一个 if 条件,在您将 else 用于条件 2 的代码中。

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

}
于 2013-04-16T11:29:34.383 回答