Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9044073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T10:53:43+00:00 2026-06-16T10:53:43+00:00

I have a function that is supposed to be attaching a file to an

  • 0

I have a function that is supposed to be attaching a file to an outgoing email. For some reason it is only sending blank files.

Can someone help? I have verified that the files themselves are being uploaded correctly, and are at the exact location needed for this function to work. Only .pdf, .doc, and .docx are allowed

Also, this is on a Windows Server… (I know, I know…YUCK!)

Here is the function:

function mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message) {
    $file = str_replace('/','\\',$path.$filename);
    $file_size = filesize($file);
    $handle = fopen($file, "rb");
    $contenta = fread($handle, $file_size);
    fclose($handle);
    $content = chunk_split(base64_encode($contenta));
    $uid = md5(uniqid(time()));
    $name = basename($file);
    $header = "From: ".$from_name." <".$from_mail.">\r\n";
    $header .= "Reply-To: ".$replyto."\r\n";
    $header .= "MIME-Version: 1.0\r\n";
    $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
    $header .= "This is a multi-part message in MIME format.\r\n";
    $header .= "--".$uid."\r\n";
    $header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
    $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
    $header .= $message."\r\n\r\n";
    $header .= "--".$uid."\r\n";
    $header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; // use different content types here
    $header .= "Content-Transfer-Encoding: base64\r\n";
    $header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
    $header .= $content."\r\n\r\n";
    $header .= "--".$uid."--";
    if (mail($mailto, $subject, "", $header)) {
        return true; // or use booleans here
    } else {
        return false;
    }
}

And here is the code using this:

//resume
$errors="";
$dbDir="/candidate-resources/files/temp/";
$baseDir=$_SERVER['DOCUMENT_ROOT'].$dbDir;
$validTypes=array(".doc",".pdf",".docx");
$filesToAdd=array();
$atLeastOne=false;
$valid=false;
$qs="";
if(count($_FILES)>0){
    foreach($_FILES as $k=>$v){
        if($v['size']>0){
            $ext=substr($v['name'],strrpos($v['name'],"."));
            if(!in_array($ext,$validTypes)){
                $errors='Only ".doc", ".docx", and ".pdf" files can be uploaded. "'.$ext.'" is not a valid file type.';
            }
        }
    }
}
$requireds=array("name","email","message");
foreach($_POST as $k=>$v){//check for injection and spammers
    if(preg_match("/(%0A|%0D|\\n+|\\r+)(content-type:|to:|cc:|bcc:)/i",$v) || strpos($v,"http://")!==false || strpos($v,"www.")!==false){
        $errors="HTML, website addresses, and scripting code are not allowed in any field.  Please check your entries and try again.";
    }
    $post[$k]=strip_tags(trim(htmlentities($v)));
}
unset($_POST);
foreach($requireds as $r){
    if(!strlen(trim($post[$r]))){
        $errors.="<li>".ucwords($r)."</li>";
    }
}
if(strlen(trim($errors))){
    $errors="These fields were left blank.  Please fix and resubmit.<ul>".$errors."</ul>";
}
else{
    if(ereg("([[:alnum:]\.\-]+)(\@[[:alnum:]\.\-]+\.+)",$post['email'])!=true){
        $errors="<p>You must enter a valid email address.</p>";
    }
    else{
        $filename = '';
        $ext = '';
        // upload the file, then attach it to the email, then delete it
        foreach($_FILES as $k=>$v){
            if($v['size']!=0){
                $atLeastOne=true;
                $ext=substr($v['name'],strrpos($v['name'],"."));                
                move_uploaded_file($v['tmp_name'], $baseDir . "/" . $v['name']);
                $filename = $v['name'];
            }
        }
        $to = 'avalid@emailaddress';
        $subject="Contact Form";
        $headers="From: ".$post["name"]." <".$post["email"].">\r\nReply-To: ".$post["email"]."\r\n";
        $message=$subject."\r\n=================================================\r\n\r\n";
        foreach($post as $k=>$v) {
            if(strlen(trim($v))){
                $message.=ucwords(str_replace("_"," ",$k)).": {$v}\r\n";
            }
        }
        if(strlen($filename) > 0) {
            mail_attachment($filename, $baseDir, $to, $post["email"], $post["name"], $post["email"], $subject, $message);
            //now delete the temp file
            if (file_exists(str_replace('/','\\',$baseDir.$filename))) { 
                unlink(str_replace('/','\\',$baseDir.$filename)); // delete it here only if it exists
            }
        }else{
            mail($to,$subject,$message,$headers);
        }
        $errors="true";
    }
}

please forgive… I just inherited this code (that is: #1 7 years old, #2 now they wanted the ability to attach a file to this email)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T10:53:44+00:00Added an answer on June 16, 2026 at 10:53 am

    I changed my function around some, and this works:

    function mail_attachment($from, $fromname, $to, $subj, $text, $filename){
        $f         = fopen($filename,"rb"); 
        $un        = strtoupper(uniqid(time())); 
        $head      = "From: $fromname <$from>\n"; 
        $head     .= "To: $to\n"; 
        $head     .= "Subject: $subj\n"; 
        $head     .= "X-Mailer: PHPMail Tool\n"; 
        $head     .= "Reply-To: $from\n"; 
        $head     .= "Mime-Version: 1.0\n"; 
        $head     .= "Content-Type:multipart/mixed;"; 
        $head     .= "boundary=\"----------".$un."\"\n\n"; 
        $zag       = "------------".$un."\nContent-Type:text/html;\n"; 
        $zag      .= "Content-Transfer-Encoding: 8bit\n\n$text\n\n"; 
        $zag      .= "------------".$un."\n"; 
        $zag      .= "Content-Type: application/octet-stream;"; 
        $zag      .= "name=\"".basename($filename)."\"\n"; 
        $zag      .= "Content-Transfer-Encoding:base64\n"; 
        $zag      .= "Content-Disposition:attachment;"; 
        $zag      .= "filename=\"".basename($filename)."\"\n\n"; 
        $zag      .= chunk_split(base64_encode(fread($f, filesize($filename))))."\n";
        return @mail("$to", "$subj", $zag, $head);
    }
    

    (without the need for a 3rd party include)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function that reads an input file and is supposed to modify
I have a function that is supposed to read from a file into a
So I have this function that is supposed make some changes to an image
I have the following function that is supposed to trigger anytime one of the
I have a function in a powershell script that is supposed to untar my
In my Java code I have function that gets file from the client in
I have a function that is supposed to calculate the number of times a
I have a function that is supposed to split my array into smaller, evenly
I have a function that is supposed to take a variable number of arguments
So I have the following function on a page that is supposed to be

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.