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?
«1345

Comments

  • kanezfankanezfan sunny south florida
    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:
    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
    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 Member
    edited Dec 2003
    Will you be posting how to do it Mr. Kwitko?

    ~Cyrix
  • a2jfreaka2jfreak Houston, TX
    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
    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
    ?>

    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
    /* Mailer with Attachments */

    $action = $_REQUEST['action'];
    global $action;

    function showForm() {
    ?>

    <form enctype=\"multipart/form-data\" name=\"send\" method=\"post\" action=\"<?=$_SERVER['PHP_SELF']?>\">
    <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['to_email'])) { //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['to_name']);
    $from_name = stripslashes($_POST['from_name']);
    $subject = stripslashes($_POST['subject']);
    $body = stripslashes($_POST['body']);
    $to_email = $_POST['to_email'];
    $attachment = $_FILES['attachment']['tmp_name'];
    $attachment_name = $_FILES['attachment']['name'];
    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['from_email'] . \">\n\";
    $headers .= \"Reply-To: <\" . $_POST['from_email'] . \">\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['from_email'] . \">\n\";
    $headers .= \"X-Mailer: PHP4\n\";
    $headers .= \"X-Priority: 3\n\"; //1 = Urgent, 3 = Normal
    $headers .= \"Return-Path: <\" . $_POST['from_email'] . \">\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>
    //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
    edited Dec 2003
    Notice: Undefined index: action in F:\apache\Apache2\htdocs\blah\email.php on line 6

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


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

    7: $action = "";
    8: $action = $_REQUEST['action'];
  • a2jfreaka2jfreak Houston, TX
    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['action'] 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['action'];
    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['action'] exists before trying to use it.
    OMG, how did I miss $subject? :doh: The best thing to do would be to make it

    $subject = stripslashes($_POST['subject']);


    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
    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
    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.
  • AuthorityActionAuthorityAction Missouri Member
    edited Dec 2003
    I can't get the emails to send, the script says the email was sent but my smtp (QK SMTP) shows nothing being sent. should i use a different smtp? any suggestions? it needs to be windows...
  • a2jfreaka2jfreak Houston, TX
    edited Dec 2003
    Works for me.

    Did you make sure your smtp server is properly set in PHP? You may have made a typo.
  • KwitkoKwitko Sheriff of Banning (Retired) By the thing near the stuff Icrontian
    edited Dec 2003
    In php.ini, look for the following lines:


    [mail function]
    ; For Win32 only.
    SMTP = localhost

    ; For Win32 only.
    sendmail_from = [email]me@localhost.com[/email]
  • AuthorityActionAuthorityAction Missouri Member
    edited Dec 2003
    I changed the sendmail_from line but now my SMTP is spitting out some error. a2jfreak, what smtp are you using?
  • KwitkoKwitko Sheriff of Banning (Retired) By the thing near the stuff Icrontian
    edited Dec 2003
    Did you change the SMTP line or do you still have it as localhost? And what's the error that is being reported?
  • a2jfreaka2jfreak Houston, TX
    edited Dec 2003
    I use the smtp for my ISP--RoadRunner.
  • Josh-Josh- Royal Oak, MI
    edited Dec 2003
    You guys might want to consider restricting file extentions and file sizes..Just as a security procedure, ofcourse. -.-
  • a2jfreaka2jfreak Houston, TX
    edited Jan 2004
    Nevermind.
    Resolved the errors.

    I have another error (though php doesn't gripe because it's not a syntax error) that I'll post later.
  • a2jfreaka2jfreak Houston, TX
    edited Jan 2004
    Here ya go. I took Mr. Kwitko's code and modified it to work with multiple attachments. Want 1 attachment? Want 2 attachments? Want 3 attachments? Fine! I'm sure it will work with more than 3, but that's as high as I've tested, thus far.

    I use Courier for my e-mail so I can't verify it will work for most client, but I doubt any of the popular mail clients will have any difficulty with the emails.


    function sendMail() {

    if (!isset ($_POST['to_email'])) { //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['to_name']);
    $from_name = stripslashes($_POST['from_name']);
    $subject = stripslashes($_POST['subject']);
    $body = stripslashes($_POST['body']);
    $to_email = $_POST['to_email'];

    $filecount = 0;
    foreach($_FILES as $file => $value) {
    $attachment[(int)$filecount] = $_FILES[$file]['tmp_name'];
    $attachment_name[(int)$filecount] = $_FILES[$file]['name'];
    if (is_uploaded_file($attachment[(int)$filecount])) { //Do we have a file uploaded?
    $fp = fopen($attachment[(int)$filecount], \"rb\"); //Open it
    $data[(int)$filecount] = fread($fp, filesize($attachment[(int)$filecount])); //Read it
    $data[(int)$filecount] = chunk_split(base64_encode($data[(int)$filecount])); //Chunk it up and encode it as base64 so it can emailed
    fclose($fp);
    $filecount++;
    }
    }

    //Let's start our headers

    $headers = \"From: $from_name<\" . $_POST['from_email'] . \">\n\";
    $headers .= \"Reply-To: <\" . $_POST['from_email'] . \">\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['from_email'] . \">\n\";
    $headers .= \"X-Mailer: PHP4\n\";
    $headers .= \"X-Priority: 3\n\"; //1 = Urgent, 3 = Normal
    $headers .= \"Return-Path: <\" . $_POST['from_email'] . \">\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\";

    for ($i = 0, $filecount = (int) count($data); $i < $filecount; $i++) {
    $message .= \"------=MIME_BOUNDRY_main_message\n\";
    $message .= \"Content-Type: application/octet-stream;\n\tname=\\"\" . $attachment_name[$i] . \"\\"\n\";
    $message .= \"Content-Transfer-Encoding: base64\n\";
    $message .= \"Content-Disposition: attachment;\n\tfilename=\\"\" . $attachment_name[$i] . \"\\"\n\n\";
    $message .= $data[$i]; //The base64 encoded message
    $message .= \"\n\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.\";

    }

    }


    You'll see some superfluous int casts. If that bothers you, take them out.
    I put them in when I got some errors in an attempt to make sure integers were being seen.
  • a2jfreaka2jfreak Houston, TX
    edited Jan 2004
    Ok, I got tired of seeing 3 places for attachments because:
    1) Most e-mails have no attachments, so they went unused but didn't go without wasting screen space.
    2) Sometimes more than 3 attachments are needed.

    So, I went digging around and found a way to dynamically alter tables and create INPUT elements. I've taken out the From name and From email because I've hard-coded that in. If you want to have to input that each time then feel free to add that back to the table . . . it's pretty simple to do.

    First, you'll need to edit the BODY tag to call a function onLoad.

    &lt;body onload=\"init()\"&gt;


    Here's the form and the script:


    &lt;form enctype=\"multipart/form-data\" name=\"send\" method=\"post\" action=\"&lt;?=$_SERVER['PHP_SELF']?&gt;\"&gt;
    &lt;input type=\"hidden\" name=\"action\" value=\"send_email\"&gt;
    &lt;input type=\"hidden\" name=\"from_name\" size=\"50\" value=\"YOUR_NAME GOES_HERE\"&gt;
    &lt;input type=\"hidden\" name=\"from_email\" size=\"50\" value=\"YOUR_EMAIL_ADDRESS@GOES_HERE.COM\"&gt;
    &lt;table class=\"ttop2\" border=\"3\" cellspacing=\"1\" cellpadding=\"2\" style=\"background-color: rgb(204, 204, 204);\" id=\"emailTable\"&gt;
    &lt;tbody id=\"emailTBody\"&gt;
    &lt;tr&gt;
    &lt;td class=\"gradient\" style=\"text-align: right;\"&gt;
    Recipient Name:
    &lt;/td&gt;
    &lt;td class=\"gradient\" style=\"text-align: left;\"&gt;
    &lt;input name=\"to_name\" size=\"25\"&gt;
    &lt;/td&gt;
    &lt;td class=\"gradient\" style=\"text-align: right;\"&gt;
    Recipient Email:
    &lt;/td&gt;
    &lt;td class=\"gradient\" style=\"text-align: left;\"&gt;
    &lt;input name=\"to_email\" size=\"25\"&gt;
    &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
    &lt;td class=\"gradient\" style=\"text-align: right;\"&gt;
    Subject:
    &lt;/td&gt;
    &lt;td class=\"gradient\" colspan=\"3\" style=\"text-align: left;\"&gt;
    &lt;input name=\"subject\" size=\"55\"&gt;
    &lt;input type=\"button\" name=\"add_attachment\" value=\"Add attachment\" onclick=\"addAttachment()\"&gt;
    &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
    &lt;td class=\"nogradient\" colspan=\"4\" style=\"text-align: left;\"&gt;
    &lt;textarea name=\"body\" rows=\"10\" cols=\"70\"&gt;&lt;/textarea&gt;
    &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
    &lt;td class=\"nogradient\" colspan=\"4\" style=\"text-align: left;\"&gt;
    &lt;textarea disabled name=\"signature\" rows=\"5\" cols=\"70\"&gt;&lt;/textarea&gt;
    &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
    &lt;td class=\"gradient\" height=\"65\" colspan=\"4\" style=\"text-align: left;\"&gt;
    &lt;div style=\"position: relative; top: -2px; left: 6px;\"&gt;
    Keep copy: &lt;input checked type=\"checkbox\" name=\"keepcopy\" value=\"keepcopy\"&gt;
    &nbsp;&nbsp;&nbsp;
    Return rcpt: &lt;input checked type=\"checkbox\" name=\"returnrcpt\" value=\"returnrcpt\"&gt;
    &nbsp;&nbsp;&nbsp;
    Opened rcpt: &lt;input checked type=\"checkbox\" name=\"openedrcpt\" value=\"openedrcpt\"&gt;
    &lt;br&gt;
    &lt;/div&gt;
    &lt;div style=\"position: relative; bottom: -6px; left: 6px;\"&gt;
    &lt;input type=\"submit\" value=\"Send Email\"&gt;
    &lt;/div&gt;
    &lt;/td&gt;
    &lt;/tr&gt;
    &lt;/tbody&gt;
    &lt;/table&gt;
    &lt;/form&gt;

    &lt;script type=\"text/javascript\" language=\"javascript\"&gt;
    var trow;

    function init()
    {
    trow = 2;
    }

    function addAttachment()
    {
    var tbodyElem = document.getElementById(\"emailTBody\");
    var trElem, tdElem, txtNode, tmpString;

    trElem = tbodyElem.insertRow(trow);

    tdElem = trElem.insertCell(0);
    tdElem.className = \"gradient\";
    tdElem.setAttribute(\"style\", \"text-align: right;\");
    txtNode = document.createTextNode(\"Attachment \" + (trow - 1) + \":\");
    tdElem.appendChild(txtNode);


    tdElem = trElem.insertCell(1);
    tdElem.className = \"gradient\";
    tdElem.setAttribute(\"style\", \"text-align: left;\");
    tdElem.setAttribute(\"colspan\", \"3\");

    var input = document.createElement(\"INPUT\");
    input.setAttribute(\"type\", \"file\");
    input.setAttribute(\"name\", \"attachment\" + (trow - 1));
    input.setAttribute(\"size\", \"60\");
    input.setAttribute(\"value\", \"\");
    input.setAttribute(\"id\", \"attachment\" + (trow - 1));
    tdElem.appendChild(input);

    trow++;
    }
    &lt;/script&gt;


    Now, all you'll have to do is click on the "Add attachment" button that shows up on the Subject line. Each time the button is clicked another row is added to the table with another file input element. Want 1 attachment? Click the button once. Want 37 attachments? Click it 37 times.

    Have fun!
  • edited Dec 2004
    Hi
    Just like to thank Kwitko for the script first of all and ask a few things about it.
    The script works good for me apart from it sends 3 copies of the attachement for some reason? Any idea why?
    Also how would i make it that the script will only accept certain extensions? would this help with people sending a malicious script or virus?
    thanks again
    Al
  • KwitkoKwitko Sheriff of Banning (Retired) By the thing near the stuff Icrontian
    edited Dec 2004
    I have some code to check for file extensions:
    $allowedext = new array('jpg', 'gif', 'png');
    $ext = substr($_FILES['file']['name'], strrpos($_FILES['file']['name'], '.') + 1);
    if (!in_array($ext, $allowedext)) {
    unlink($_FILES['file']['tmp_name']);
    exit(\"File type $ext is not allowed.\");
    }


    You would throw this in before the part that encodes the files to base64.
  • edited Dec 2004
    i keep getting parse error (i doubt im putting it in the right place)
    after which line exactly?
  • edited Dec 2004
    when i plug in the code:

    <?
    /* Mailer with Attachments */

    $action = $_REQUEST['action'];
    global $action;

    function showForm() {
    ?>

    <form enctype=\"multipart/form-data\" name=\"send\" method=\"post\" action=\"<?=$_SERVER['PHP_SELF']?>\">
    <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>

    <?
    }

    function sendMail() {
    if (!isset ($_POST['to_email'])) { //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['to_name']);
    $from_name = stripslashes($_POST['from_name']);
    $subject = stripslashes($_POST['subject']);
    $body = stripslashes($_POST['body']);
    $to_email = $_POST['to_email'];
    $attachment = $_FILES['attachment']['tmp_name'];
    $attachment_name = $_FILES['attachment']['name'];
    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['from_email'] . \">\n\";
    $headers .= \"Reply-To: <\" . $_POST['from_email'] . \">\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['from_email'] . \">\n\";
    $headers .= \"X-Mailer: PHP4\n\";
    $headers .= \"X-Priority: 3\n\"; //1 = Urgent, 3 = Normal
    $headers .= \"Return-Path: <\" . $_POST['from_email'] . \">\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.\";
    }
    }

    print <<< EOT
    <?xml version=\"1.0\" encoding=\"iso-8859-1\"?>

    EOT;?>

    <!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>
    <meta 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>

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

    </body>
    </html>

    that was the same up at the top. It has codes at the bottom that are still there, y is that. Plus it does not do anything when you press send. :confused:
  • KwitkoKwitko Sheriff of Banning (Retired) By the thing near the stuff Icrontian
    edited Dec 2004
    Does your server have PHP installed?
  • edited Dec 2004
    o no, i guess i need to have that then
  • edited Jun 2006
    a2jfreak

    I love your code for sending multiple attachments, but when I try to use it, the form works great, but no e-mail is sent?

    What do I have to do to fix this? Did I miss something?

    Thanks
  • a2jfreaka2jfreak Houston, TX
    edited Jun 2006
    You need to configure PHP to talk to your SMTP server.
  • edited Jun 2006
    Thanks!

    Wasn't sure if I would get a reply on a 3 year old thread :)

    I'm on it!
  • edited Jul 2006
    Hello Sir

    I am new to PHP world and I am trying to send mail attachment through your given code. But at the time of sending this mail to @yahoo.com or @gmail.com it's just sending attachment only. No Message with the mail. What should I do now. Also while I am sending this message to POP3 account it's getting done. Problem is with @yahoo.com and all only. Please send me reply asap. It's very urgent.
    KwitCo™ said:
    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:


    &lt;?
    /* Mailer with Attachments */

    $action = $_REQUEST['action'];
    global $action;

    function showForm() {
    ?&gt;

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

    &lt;?
    }

    function sendMail() {
    if (!isset ($_POST['to_email'])) { //Oops, forgot your email addy!
    die (\"&lt;p&gt;Oops! You forgot to fill out the email address! Click on the back arrow to go back&lt;/p&gt;\");
    }
    else {
    $to_name = stripslashes($_POST['to_name']);
    $from_name = stripslashes($_POST['from_name']);
    $subject = stripslashes($_POST['subject']);
    $body = stripslashes($_POST['body']);
    $to_email = $_POST['to_email'];
    $attachment = $_FILES['attachment']['tmp_name'];
    $attachment_name = $_FILES['attachment']['name'];
    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&lt;\" . $_POST['from_email'] . \"&gt;\n\";
    $headers .= \"Reply-To: &lt;\" . $_POST['from_email'] . \"&gt;\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&lt;\" . $_POST['from_email'] . \"&gt;\n\";
    $headers .= \"X-Mailer: PHP4\n\";
    $headers .= \"X-Priority: 3\n\"; //1 = Urgent, 3 = Normal
    $headers .= \"Return-Path: &lt;\" . $_POST['from_email'] . \"&gt;\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&lt;$to_email&gt;\", $subject, $message, $headers);
    print \"Mail sent. Thank you for using the MyNewName5333 Mailer.\";
    }
    }

    print &lt;&lt;&lt; EOT
    &lt;?xml version=\"1.0\" encoding=\"iso-8859-1\"?&gt;

    EOT;
    ?&gt;

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

    &lt;?
    switch ($action) {
    case \"send\":
    sendMail();
    showForm();
    break;
    default:
    showForm();
    }
    ?&gt;

    &lt;/body&gt;
    &lt;/html&gt;


    //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?
«1345
Sign In or Register to comment.