ezemosho
ezemosho

Reputation: 5

How to create PHP email form with optional file attachment field

I was testing a PHP Form for my client, when I realized that file attachment wasn´t optional, I have the full code here (it´s pretty long). Form works fine, but if you don´t choose any file, the e-mail will never arrive to it´s destination. I want to send the mail anyway and receive it without the attach.

I´m using PHP and jForm for validation.

Here is the input tag for file attaching (there to many input tags at the form.php file to list them here)

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

And here is the send.php file (the true form is too long to publish here, i´m writing a shorter version of it)

    <?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("[email protected]", "".$busqueda." ".$cargo."", $mensaje, $headers); 

        fclose($fp); 
    } 

// IF NOT, JUST SEND MAIL

else

@mail("[email protected]", "".$busqueda." ".$cargo."", $mensaje); 

}          
    ?>

Please I need a hand with this, I´m doing my best (i´ve already search here and other forums) and I think this maybe a problem many people had before!

Thank you!

Upvotes: 0

Views: 2383

Answers (2)

sandy
sandy

Reputation: 1158

you need to give one more condition on top

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("[email protected]", "".$busqueda." ".$cargo."", $mensaje, $headers); 
     }


}
else

@mail("[email protected]", "".$busqueda." ".$cargo."", $mensaje); 

now the mail will go on both conditions.

UPDATED WITH YOUR CODE

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("[email protected]", "".$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("[email protected]", "".$busqueda." ".$cargo."", $mensaje);

}

Upvotes: 1

karthzDIGI
karthzDIGI

Reputation: 403

Put the else statement for your first if condition, in your code you are using else for the condition2.

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

}

Upvotes: 0

Related Questions