How to upload then send a document as attachment via email?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tharden3
    Contributor
    • Jul 2008
    • 916

    How to upload then send a document as attachment via email?

    Hi Bytes Community,

    My problem can best be described in steps my website visitors will go through:
    1. Someone visits my webpage
    2. They click a "browse" button that allows them to select a file from their own computer
    3. Once they have selected a file, they click a "submit" button
    4. The file is then sent to a single email address as an attachment


    Basically, I have people who need to submit volunteer sheets (Excel or Word documents) and I would like them to submit it via my website rather than open an email platform.

    Would this be handled by JavaScript, PHP, or HTML? Do the files have to be saved on my own server before they are sent to the email address?

    I think I can figure out how to do this myself, I just need a push in the right direction.

    Kind regards,
    Tim
  • drhowarddrfine
    Recognized Expert Expert
    • Sep 2006
    • 7434

    #2
    You want to use a form with the input type as 'file'. For the form 'action' you want to do: "action=mailto: tim@timswebpage .com"

    Comment

    • Sudaraka
      New Member
      • Jan 2011
      • 55

      #3
      You will need to use PHP for this.
      Set the enctype of your HTML form to "multipart/form-data" and post the form to your PHP script.
      PHP will create the a temporary file and place the content of the file you uploaded into that. You can access this file parameters from the $_FILES super global.

      Then you have to construct a MIME message and send it using the PHP mail function. For more details, google something like "send email attachment in php".

      Comment

      • tharden3
        Contributor
        • Jul 2008
        • 916

        #4
        Hi Bytes,

        I have gotten pretty far, and it looks like I am very close to getting it working.

        I have run into a problem that I do not understand how to fix. When I submit the form, I get this message:

        Fatal error: Call to undefined method Mail_mimePart:: encodeHeader() in /home/sowegaca/public_html/mime.php on line 1322

        Here is the code snippet from the php file it is referencing to:
        Code:
         /**
             * Encodes a header as per RFC2047
             *
             * @param string $name     The header name
             * @param string $value    The header data to encode
             * @param string $charset  Character set name
             * @param string $encoding Encoding name (base64 or quoted-printable)
             *
             * @return string          Encoded header data (without a name)
             * @access public
             * @since 1.5.3
             */
            function encodeHeader($name, $value, $charset, $encoding)
            {
               [B] return Mail_mimePart::encodeHeader([/B]
                    $name, $value, $charset, $encoding, $this->_build_params['eol']
                );
            }
        I bolded line 1322. I have Googled the issue, but I am not sure how to go about solving the problem. I think the problem is that it is not accepting characters like "@" (which is required in this form for a return email address).

        UPDATE: Here is an article I found that I think addresses my problem, how do I fix it with this information?:

        Comment

        • tharden3
          Contributor
          • Jul 2008
          • 916

          #5
          Hi Bytes Community,

          The "Fatal Error" message I received in the post above was due to the PHP file calling for a method that was not available to it. I simply needed to put another PHP file that this one referred to in the same folder. Problem solved.

          In case anyone has this issue in the future, that is: "Creating a form that allows visitors to upload a file and then 'submit' or email the uploaded file within a webpage," I came across a very handy PHP script and tutorial that helped me. It would be inappropriate to post that here because external links usually fall under the "no advertising" rule on Bytes, so if anyone has this problem in the future, please PM me and I would be happy to point you to the tutorial and some helpful scripts.

          Cheers,
          Tim

          Comment

          Working...