If geeks love it, we’re on it

Howdy, Stranger!

You found the friendliest gaming & tech geeks around. Say hello!

PHP email form with attachments?

AuthorityActionAuthorityAction Missouri Member
edited Oct 2011 in Internet & Media
Could you make a php email form that can send attachments?
«1345678

Comments

  • kanezfankanezfan sunny south florida Member
    edited Dec 2003
    i don't know how to do it, but i'll tell you this; in programming, anything is possible. someone here will set you straight
  • EMTEMT Seattle, WA Icrontian
    edited Dec 2003
    Should be pretty easy. I'm not familiar with the code for this but the box for "file" is pretty simple. Just a text box with browse and when you POST it should send whatever file you wrote in the box. You could look it up as an input type, I guess.
  • KwitkoKwitko Sheriff of Banning (Retired) By the thing near the stuff Icrontian
    edited Dec 2003
    Fill me in on what you need.
  • AuthorityActionAuthorityAction Missouri Member
    edited Dec 2003
    well a teacher approached me with the question. he wanted kids to be able to send attachments to themselves since our tech dept blocked hotmail, yahoo, etc. if someone can just get the attachment part of the php for me i can do the rest of the form. i don't know enough about php to be able to do it yet. i _think_ i know the html to get a box for finding the file but from there i don't have the slightest clue.

    the form would look something like this:

    to: < addy box >
    subject: < box >
    message: <bigger box >
    attachment: < yet another box >
    < send >

    something like that...
  • KwitkoKwitko Sheriff of Banning (Retired) By the thing near the stuff Icrontian
    edited Dec 2003
    Sure, just give me some time to write it up. Just need to know what kind of server it will be on.
  • AuthorityActionAuthorityAction Missouri Member
    edited Dec 2003
    html code for getting the file with max file size...

    edit: oops forgot to add some spaces...
    < form action="upload.php" method="post" enctype="multipart/form-data" >
    < input type="hidden" name="MAX_FILE_SIZE" value="10000000" >Select a file to upload! 
    < input type="file" name="userfile" >
    < input type="submit" value="Upload!" >
    < /form >
    
  • AuthorityActionAuthorityAction Missouri Member
    edited Dec 2003
    Mr. Kwitko had this to say
    Sure, just give me a little time to do it.

    awesome! thanks a lot. I'm in no hurry so just whenever you have time.

    :respect:
  • KwitkoKwitko Sheriff of Banning (Retired) By the thing near the stuff Icrontian
    edited Dec 2003
    Well, it appears you don't need help on it after all! That's the right method.
  • AuthorityActionAuthorityAction Missouri Member
    edited Dec 2003
    i don't know how do get it sent with the email though
  • KwitkoKwitko Sheriff of Banning (Retired) By the thing near the stuff Icrontian
    edited Dec 2003
    I'll do it tomorrow for you.
  • a2jfreaka2jfreak
    was just thinking about this exact thing earlier today. I probably wouldn't have asked, though. Thanks for asking for me, MyNewName! :)
    Houston, TX Member
    edited Dec 2003
    was just thinking about this exact thing earlier today. I probably wouldn't have asked, though. Thanks for asking for me, MyNewName! :)
  • CyrixInsteadCyrixInstead Stoke-on-Trent, England Icrontian
    edited Dec 2003
    Will you be posting how to do it Mr. Kwitko?

    ~Cyrix
  • a2jfreaka2jfreak Houston, TX Member
    edited Dec 2003
    Cyrix: It was after 11pm EST when he made that post, so it will probably be sometimes later today (as it is just after 6:30am EST now) before he posts it.
  • Josh-Josh- Royal Oak, MI
    edited Dec 2003
    Just modify my mailform and add that in...not too hard.
    [php]
    <?php
    if ($do != "sendmail") {
    ?>
    <form name="sendmail" action="$PHP_SELF" method="post">
    <font face="Verdana,Arial" size="2">
    <input type="hidden" name="do" value="sendmail">
    E-Mail: <input type="text" name="email" size="60">
    Subject: <input type="text" name="subject" size="60">
    Comments:<textarea name="comments" rows="10" cols="30"></textarea>
    <input type="submit" value="submit">
    </font>
    </form>
    <?php
    } elseif ($do == "sendmail") {
    mail("your@email.com",$subject,$email,$comments) or die("Error: the message could not be sent");
    } // endif
    ?>
    [/php]
    or http://www.short-media.com/forum/showthread.php?s=&threadid=6803.
  • KwitkoKwitko Sheriff of Banning (Retired) By the thing near the stuff Icrontian
    edited Dec 2003
    CyrixInstead had this to say
    Will you be posting how to do it Mr. Kwitko?

    ~Cyrix
    ]

    Sure!
  • KwitkoKwitko Sheriff of Banning (Retired) By the thing near the stuff Icrontian
    edited Dec 2003
    Sending attachments through PHP's mail() function is difficult because the headers must be correct and the attachment has to be encoded into base64 before sending.
  • EMTEMT Seattle, WA Icrontian
    edited Dec 2003
    oo.. interesting. I'm waiting to see what you make too :)
  • KwitkoKwitko Sheriff of Banning (Retired) By the thing near the stuff Icrontian
    edited Dec 2003
    As promised. Now I didn't add any error checking or formatting. My goal was simply to make sure the MIME encoding was done right. MIME headers are *very* finicky. If you want to add more attachments, you could foreach or while the chunk routine.

    Without further ado, I give you:

    [php]
    <?php
    /* Mailer with Attachments */

    $action = $_REQUEST;
    global $action;

    function showForm() {
    ?>

    <form enctype="multipart/form-data" name="send" method="post" action="<?=$_SERVER?>">
    <input type="hidden" name="action" value="send" />
    <input type="hidden" name="MAX_FILE_SIZE" value="10000000" />
    <p>Recipient Name: <input name="to_name" size="50" /><br />
    Recipient Email: <input name="to_email" size="50" /><br />
    From Name: <input name="from_name" size="50" /><br />
    From Email: <input name="from_email" size="50" /><br />
    Subject: <input name="subject" size="50" /><br />
    Message: <textarea name="body" rows="10" cols="50"></textarea><br />
    Attachment: <input type="file" name="attachment" size="50" /><br />
    <input type="submit" value="Send Email" /></p>

    <?php
    }

    function sendMail() {
    if (!isset ($_POST)) { //Oops, forgot your email addy!
    die ("<p>Oops! You forgot to fill out the email address! Click on the back arrow to go back</p>");
    }
    else {
    $to_name = stripslashes($_POST);
    $from_name = stripslashes($_POST);
    $subject = stripslashes($_POST);
    $body = stripslashes($_POST);
    $to_email = $_POST;
    $attachment = $_FILES;
    $attachment_name = $_FILES;
    if (is_uploaded_file($attachment)) { //Do we have a file uploaded?
    $fp = fopen($attachment, "rb"); //Open it
    $data = fread($fp, filesize($attachment)); //Read it
    $data = chunk_split(base64_encode($data)); //Chunk it up and encode it as base64 so it can emailed
    fclose($fp);
    }
    //Let's start our headers
    $headers = "From: $from_name<" . $_POST . ">\n";
    $headers .= "Reply-To: <" . $_POST . ">\n";
    $headers .= "MIME-Version: 1.0\n";
    $headers .= "Content-Type: multipart/related; type=\"multipart/alternative\"; boundary=\"----=MIME_BOUNDRY_main_message\"\n";
    $headers .= "X-Sender: $from_name<" . $_POST . ">\n";
    $headers .= "X-Mailer: PHP4\n";
    $headers .= "X-Priority: 3\n"; //1 = Urgent, 3 = Normal
    $headers .= "Return-Path: <" . $_POST . ">\n";
    $headers .= "This is a multi-part message in MIME format.\n";
    $headers .= "
    =MIME_BOUNDRY_main_message \n";
    $headers .= "Content-Type: multipart/alternative; boundary=\"----=MIME_BOUNDRY_message_parts\"\n";

    $message = "
    =MIME_BOUNDRY_message_parts\n";
    $message .= "Content-Type: text/plain; charset=\"iso-8859-1\"\n";
    $message .= "Content-Transfer-Encoding: quoted-printable\n";
    $message .= "\n";
    /* Add our message, in this case it's plain text. You could also add HTML by changing the Content-Type to text/html */
    $message .= "$body\n";
    $message .= "\n";
    $message .= "
    =MIME_BOUNDRY_message_parts--\n";
    $message .= "\n";
    $message .= "
    =MIME_BOUNDRY_main_message\n";
    $message .= "Content-Type: application/octet-stream;\n\tname=\"" . $attachment_name . "\"\n";
    $message .= "Content-Transfer-Encoding: base64\n";
    $message .= "Content-Disposition: attachment;\n\tfilename=\"" . $attachment_name . "\"\n\n";
    $message .= $data; //The base64 encoded message
    $message .= "\n";
    $message .= "
    =MIME_BOUNDRY_main_message--\n";

    // send the message
    mail("$to_name<$to_email>", $subject, $message, $headers);
    print "Mail sent. Thank you for using the MyNewName5333 Mailer.";
    }
    }

    ?>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">;
    <html xmlns="http://www.w3.org/1999/xhtml"; xml:lang="en" lang="en">
    <head>
    ****** http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <style="css" type="text/css">
    <!--
    body {
    margin: 0px;
    font-family: Verdana, Arial, Helvetica, sans-serif;
    font-size: 12px;
    }
    a {color: #0000ff}
    -->
    </style>
    </head>
    <body>

    <?php
    switch ($action) {
    case "send":
    sendMail();
    showForm();
    break;
    default:
    showForm();
    }
    ?>

    </body>
    </html>
    [/php]//EDIT: added some additional comments. I realized after posting that it's kind of silly to add the MIME boundary if you're not neccessarily sending an attachment, so the encoding routine plus the boundaries for the attachment should really be conditional. Then again, the whole purpose of this script is so you can send attachments, right?
  • KwitkoKwitko Sheriff of Banning (Retired) By the thing near the stuff Icrontian
    edited Dec 2003
    You're welcome, MMN.
  • AuthorityActionAuthorityAction Missouri Member
    edited Dec 2003
    Awesome!!! Thanks a lot Mr. Kwitko!
  • a2jfreaka2jfreak Houston, TX Member
    edited Dec 2003
    Notice: Undefined index: action in F:\apache\Apache2\htdocs\blah\email.php on line 6

    Offending line: $action = $_REQUEST;
  • KwitkoKwitko Sheriff of Banning (Retired) By the thing near the stuff Icrontian
    edited Dec 2003
    Hmmm. Okay, try this:

    [php]
    $action = "";
    $action = $_REQUEST;
    [/php]
  • a2jfreaka2jfreak Houston, TX Member
    edited Dec 2003
    Notice: Undefined index: action in F:\apache\Apache2\htdocs\blah\email.php on line 8

    7: $action = "";
    8: $action = $_REQUEST;
  • a2jfreaka2jfreak Houston, TX Member
    edited Dec 2003
    mail() uses $subject, which happens to not exist.

    Where you see
      else {
    
        $to_name = $_POST['to_name'];
    
        $to_email = $_POST['to_email'];
    

    add
        $subject = $_POST['subject'];
    


    Still haven't fixed the $action error, but I'm not very handy w/ PHP. I guess I should check to see if $_REQUEST exists before trying to use it.
  • EnverexEnverex Worcester, UK Icrontian
    edited Dec 2003
    If you set action then it needs to be present. I.e. if the line "action=something" isn't present in the URL then you are going to get an error....

    NS
  • KwitkoKwitko Sheriff of Banning (Retired) By the thing near the stuff Icrontian
    edited Dec 2003
    a2jfreak had this to say
    Notice: Undefined index: action in F:\apache\Apache2\htdocs\blah\email.php on line 8

    7: $action = "";
    8: $action = $_REQUEST;

    Okay, I think the problem is that error_reporting is tweaked too high. Go to your php.ini and look for error_reporting, then set it to
    error_reporting  =  E_ALL & ~E_NOTICE
    

    It's a warning, not an error, right? The script still works?

    a2jfreak had this to say
    mail() uses $subject, which happens to not exist.

    Where you see
      else {
    
        $to_name = $_POST['to_name'];
    
        $to_email = $_POST['to_email'];
    

    add
        $subject = $_POST['subject'];
    


    Still haven't fixed the $action error, but I'm not very handy w/ PHP. I guess I should check to see if $_REQUEST exists before trying to use it.

    OMG, how did I miss $subject? :doh: The best thing to do would be to make it
    [php]
    $subject = stripslashes($_POST);
    [/php]

    So you get Mr. Kwitko's Script instead of Mr. Kwitko\'s Script

    It would be a good idea to "massage" the names, message body, and subject with stripslashes. I think I'll go do that now!
    NightShade737 had this to say
    If you set action then it needs to be present. I.e. if the line "action=something" isn't present in the URL then you are going to get an error....

    NS

    That's if it were a $_GET. All the form variables here are $_POST.
  • a2jfreaka2jfreak Houston, TX Member
    edited Dec 2003
    I just used:
    if (isset($_REQUEST['action'])) {
    	$action = $_REQUEST_['action'];
    } else {
    	$action = "";
    }
    

    and it no longer gripes. :)
  • KwitkoKwitko Sheriff of Banning (Retired) By the thing near the stuff Icrontian
    edited Dec 2003
    Yeah, but you shouldn't have to. Try doing that php.ini fix I mentioned then set the script back to the way I had it originally and report your results.
  • KwitkoKwitko Sheriff of Banning (Retired) By the thing near the stuff Icrontian
    edited Dec 2003
    I made a few changes in the script.
  • a2jfreaka2jfreak Houston, TX Member
    edited Dec 2003
    I don't want to. That's like supressing warnings when compiling a C/C++ app . . . it warns for a reason. Perhaps in this particular case there is no harm, but I'd rather see the warning so I can fix it.
    Mr. Kwitko had this to say
    Yeah, but you shouldn't have to. Try doing that php.ini fix I mentioned then set the script back to the way I had it originally and report your results.
«1345678
Sign In or Register to comment.