Jump to content

Html/PHP Mail attachment issue


PHP_rookie

Recommended Posts

Hi

 

I've managed to put bit's and pieces of code together to create an online email form with attachment.

When I click on send the email gets sent and my email client doesn't tagg it as a spam, which is good,

BUT the attached file size is 0.0 Kb. The name of the attached file is correct and the format, but it's

empty.

 

If someone could help me with the missing code or faulty code I would be greatful.

 

 

 

This is HTML

---------------------------------------------------------------------------------------------------

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

 

 

<title>Mail</title>

</head>

 

<body style="padding:3px; margin:0px;" bgcolor="#FFFFFF">

<table cellpadding="0" cellspacing="0" border="0" width="440">

   

    <tr><td style="height:10px"></td></tr>

    <tr>

      <td colspan="2" style="text-align:justify; line-height:15px;" class="body">

       

      <form name="frm" method="POST" action="sendmail.php" enctype="multipart/form-data">

      <table cellpadding="0" cellspacing="0" border="0" width="100%">

          <tr>

            <td width="25%" class="body"> Your name</td>

            <td width="75%"><input type="text" name="myname" class="textfield"></td>

        </tr>

        <tr><td style="height:3px"></td></tr>

        <tr>

            <td width="25%" class="body"> Your e-mail</td>

            <td width="75%"><input type="text" name="myemail" class="textfield"></td>

        </tr>

        <tr><td style="height:3px"></td></tr>

        <tr>

            <td width="25%" class="body"> To e-mail</td>

            <td width="75%"><input type="text" name="toemail" class="textfield"></td>

        </tr>

        <tr><td style="height:3px"></td></tr>

        <tr>

            <td width="25%" class="body"> File</td>

            <td width="75%"><input type="file" name="myfile"></td>

        </tr>

        <tr><td style="height:10px"></td></tr>

        <tr>

            <td colspan="3" align="center">

                <input type="submit" value="Send" name="submit" onClick="return validate();"> <input type="reset" value="Reset" name="reset">

            </td>

        </tr>

       

      </table>   

    </form>

 

</td>

    </tr>

    <tr>

      <td colspan="2" align="center"> </td>

  </tr>

    </table>

 

</body>

</html>

 

---------------------------------------------------------------------------------------------------

 

 

 

 

This is the PHP code

---------------------------------------------------------------------------------------------------

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<title>Mail sender</title>

</head>

 

<body>

 

 

<?

     

    $eol="\r\n";                 

    $myname=ucfirst($_REQUEST["myname"]);

    $myemail=ucfirst($_REQUEST["myemail"]);

    $toemail=ucfirst($_REQUEST["toemail"]);

   

    $filename=$_FILES["myfile"]["name"];

    $filetype=$_FILES["myfile"]["type"];

    $filesize=$_FILES["myfile"]["size"];

    $filetemp=$_FILES["myfile"]["tmp_name"];

 

 

       

    if($filetype=="application/acad" or $filetype=="image/jpeg" or $filetype=="application/pdf")

    {

   

        $message= $myname.$myemail;

 

    // MAIL SUBJECT

 

    $subject = "Testing PHP Mail";

   

    // TO MAIL ADDRESS

   

   

    $to = $toemail;

 

    // MAIL HEADERS with attachment

 

    $fp = fopen($myfile, "rb");

    $file = fread($fp, $myfile_size);

 

    $file = chunk_split(base64_encode($file));

    $num = md5(time());

   

        //Normal headers

 

    $headers  = "From: ".$myname." <".$myemail.">\r\n";

      $headers  .= "MIME-Version: 1.0\r\n";

      $headers  .= "Content-Type: multipart/related; ";

      $headers  .= "boundary=".$num."\r\n";

      $headers  .= "--$num\r\n";

 

        // This two steps to help avoid spam   

 

    $headers .= "Message-ID: <".$now." TheSystem@".$_SERVER['SERVER_NAME'].">\r\n";

    $headers .= "X-Mailer: PHP v".phpversion()."\r\n";         

 

        // With message

       

    $headers .= "Content-Type: text/html; charset=iso-8859-1\r\n";

      $headers .= "Content-Transfer-Encoding: 8bit".$eol.$eol;

      $headers .= "".$message."\n";

      $headers .= "--".$num."\n"; 

 

        // Attachment headers

 

    $headers  .= "Content-Type:".$filetype." ";

      $headers  .= "name=\"".$filename."\"".$eol;

      $headers  .= "Content-Transfer-Encoding: base64".$eol;

      $headers  .= "Content-Description: ".$filename.$eol;

      $headers  .= "Content-Disposition: attachment; ";

      $headers  .= "filename=\"".$filename."\"".$eol.$eol;

      $headers  .= "".$file."\r\n";

      $headers  .= "--".$num."--";

 

   

   

    // SEND MAIL

       

      @mail($to, $subject, $message, $headers);

   

 

    fclose($fp);

 

    echo '<font style="font-family:Verdana, Arial; font-size:11px; color:#333333; font-weight:bold">Attachment has been sent Successfully.<br /></font>';

}

else

    {

        echo '<font style="font-family:Verdana, Arial; font-size:11px; color:#F3363F; font-weight:bold">Wrong file format. Mail was not sent.</font>';

    }

 

?>

 

 

 

</body>

</html>

 

---------------------------------------------------------------------------------------------------

 

 

 

Thanks!

 

 

 

 

Link to comment
Share on other sites

add an extra end of line here:

// from :
$headers  .= "".$file."\r\n";

// to:
$headers  .= "".$file."\r\n\r\n";

 

you also need to change the following:

// from :

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

// to:
       $message  .= "--".$num."--"; 
       $message  .= "Content-Type:".$filetype." ";
       $message  .= "name=\"".$filename."\"".$eol;
       $message  .= "Content-Transfer-Encoding: base64".$eol;
       $message  .= "Content-Description: ".$filename.$eol;
       $message  .= "Content-Disposition: attachment; ";
       $message  .= "filename=\"".$filename."\"".$eol.$eol;
       $message  .= "".$file."\r\n\r\n";
       $message  .= "--".$num."--"; 

 

line breaks are usually the cause for incorrect sends. the trick is to find a solution that works for you, write a function / class and only enhance it.

 

Good luck

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.