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

Related Questions

Imagine that HTML page is a game surface (see picture). User can have n
Imagine an ASP.NET website which displays stock prices when a user logs in. Can
Imagine I have an existing process running under windows as a particular user. Would
The Setup: Imagine a 'twitter like' service where a user submits a post, which
Imagine a simple page with a list of users. Selecting a user displays a
I imagine this has a rather simple answer <% for user in @users %>
I would like to know whether it is possible to write code that would
Imagine the following scenario. I have a checkbox that the user can check to
I would like to build (or find) a php reporting library that I can
I have a case here that I would like to have some opinions from

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.