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 431831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T20:00:50+00:00 2026-05-12T20:00:50+00:00

Imagine a user that would like to put a form on their website that

  • 0

Imagine a user that would like to put a form on their website that would allow a website visitor to upload a file and a simple message, which will immediately be emailed (ie, the file is not stored on the server, or if it is then only temporarily) as a file attachment with the note in the message body.

See more details at http://a2zsollution.com/php-secure-e-mail/

What is the simplest way to accomplish this?

Simplest in terms of:

  • Size (code golf)
  • Ease of implementation (ideally all in one file, needs few to no external resources)
  • Not obfuscated for the sake of obfuscation (tradeoffs for size are fine)
  • Self contained example (if called without a form post, it displays the form)

This is nearly the reverse of: How to get email and their attachments from PHP. It almost could have been answered in Compiling email with multiple attachments in PHP, but it doesn’t actually show code.

  • 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-05-12T20:00:50+00:00Added an answer on May 12, 2026 at 8:00 pm

    Just for fun I thought I’d knock it up. It ended up being trickier than I thought because I went in not fully understanding how the boundary part works, eventually I worked out that the starting and ending ‘–‘ were significant and off it went.

    <?php
        if(isset($_POST['submit']))
        {
            //The form has been submitted, prep a nice thank you message
            $output = '<h1>Thanks for your file and message!</h1>';
            //Set the form flag to no display (cheap way!)
            $flags = 'style="display:none;"';
    
            //Deal with the email
            $to = 'me@example.com';
            $subject = 'a file for you';
    
            $message = strip_tags($_POST['message']);
            $attachment = chunk_split(base64_encode(file_get_contents($_FILES['file']['tmp_name'])));
            $filename = $_FILES['file']['name'];
    
            $boundary =md5(date('r', time())); 
    
            $headers = "From: webmaster@example.com\r\nReply-To: webmaster@example.com";
            $headers .= "\r\nMIME-Version: 1.0\r\nContent-Type: multipart/mixed; boundary=\"_1_$boundary\"";
    
            $message="This is a multi-part message in MIME format.
    
    --_1_$boundary
    Content-Type: multipart/alternative; boundary=\"_2_$boundary\"
    
    --_2_$boundary
    Content-Type: text/plain; charset=\"iso-8859-1\"
    Content-Transfer-Encoding: 7bit
    
    $message
    
    --_2_$boundary--
    --_1_$boundary
    Content-Type: application/octet-stream; name=\"$filename\" 
    Content-Transfer-Encoding: base64 
    Content-Disposition: attachment 
    
    $attachment
    --_1_$boundary--";
    
            mail($to, $subject, $message, $headers);
        }
    ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>MailFile</title>
    </head>
    
    <body>
    
    <?php echo $output; ?>
    
    <form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" <?php echo $flags;?>>
    <p><label for="message">Message</label> <textarea name="message" id="message" cols="20" rows="8"></textarea></p>
    <p><label for="file">File</label> <input type="file" name="file" id="file"></p>
    <p><input type="submit" name="submit" id="submit" value="send"></p>
    </form>
    </body>
    </html>
    

    Very barebones really, and obviously the using inline CSS to hide the form is a bit cheap and you’d almost certainly want a bit more feedback to the user! Also, I’d probably spend a bit more time working out what the actual Content-Type for the file is, rather than cheating and using application/octet-stream but that part is quite as interesting.

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

Sidebar

Ask A Question

Stats

  • Questions 244k
  • Answers 245k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Very simple auto gallery script, photos.php: <?php function getDirTree($dir,$p=true) {… May 13, 2026 at 8:08 am
  • Editorial Team
    Editorial Team added an answer $x = $this->getResource('something'); its used to Initialize and retrieve a… May 13, 2026 at 8:08 am
  • Editorial Team
    Editorial Team added an answer Do you see anything obviously wrong with it? Nope, the… May 13, 2026 at 8:08 am

Related Questions

My company has recently put up a demo database on his remote servers to
I've seen two threads here about TDI & C#. Both of them didn't really
My interactive 32-bit Windows app (now moving from Delphi [Ent] 2007 to 2009) uses
(I'm using VB6 but I imagine this comes up in most other languages.) I've

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.