简体   繁体   中英

file attachment mail in php

hello I am working on html form submission via email. Server side scripting is PHP . my html form is

<form action='sendmail.php' method='post'>
   Name:<input type="text" name="name">
    File:<input type='file' name='attach'>
<input type="submit">

My php file for sending email is

<?php 
   $strTo = "xyz@gmail.com";
     $strHeader .= "From: ".$_POST["name"]."<".$_POST["name"].">";  
     $strHeader .= "MIME-Version: 1.0\n";
     $strHeader .= "MIME-Version: 1.0\n"; 
   $strHeader .= "Content-Type: multipart/mixed; boundary=\"".$strSid."\"\n\n";  
    $strHeader .= "This is a multi-part message in MIME format.\n";
   $strHeader .= "--".$strSid."\n"; 
    $strHeader .= "Content-type: text/html; charset=utf-8\n"; 
    $strHeader .= "Content-Transfer-Encoding: 7bit\n\n";  
    $strHeader .= $strMessage."\n\n";  
 if($_FILES["attach"]["name"] != "")
 {
        $strFilesName = $_FILES["fileAttach"]["name"];  
        $strContent = chunk_split(base64_encode(file_get_contents($_FILES["fileAttach"]["tmp_name"])));  
       $strHeader .= "--".$strSid."\n";  
         $strHeader .= "Content-Type: application/octet-stream; name=\"".$strFilesName."\"\n";  
       $strHeader .= "Content-Transfer-Encoding: base64\n";  
        $strHeader .= "Content-Disposition: attachment; filename=\"".$strFilesName."\"\n\n";  
       $strHeader .= $strContent."\n\n";  
       }
 $flgSend = @mail($strTo,"no-subjet",null,$strHeader); 

but its not going in if condition

Try replacing your form with this:

<form action="sendmail.php" method="post" enctype="multipart/form-data">
    Name:<input type="text" name="name">
    File:<input type="file" name="attach" id="attach">
    <input type="submit">
</form>

The enctype attribute in the form is important.

See following link for more details: http://www.w3schools.com/php/php_file_upload.asp

Also ensure all the names in PHP match with HTML, such as the name of file input is attach , so it should be the same in PHP.

if($_FILES["attach"]["name"] != "")
{
    $strFilesName = $_FILES["attach"]["name"];
    ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM