PHP email form with attachments?

135678

Comments

  • LincLinc Owner Detroit Icrontian
    edited April 2007
    It's also possible the server is sending them as a different account now, which could cause it to get blocked by their spam filter. Do you have the "From:" header defined?
  • edited April 2007
    a2jfreak, you have the process correct, but thats not the form in question...thats another seperate form on the website. The only reason I talked about that is because they are recieving those emails. And it's done using the same php mail function. So that would lead me to believe that the php.ini may not be the culprit or none of the emails would go through...right?

    The form that doesn't work is the one I copied from this thread. Which I only changed a couple misc. labels and the email address (which I'm 100% sure is correct)...I didn't modify any of the functionality.

    General Keebler, I have a feeling that's what it could be...How else would it work on both my work and home email accounts, but not her email? I'm trying to get a hold of her now to see if there's 20 emails sitting in her junk mail folder...

    Again, I copied the exact code from this thread so, yes the From: header is being defined.

    [php] //Let's start our headers
    $headers = "From: $from_name <" . $_POST . ">\n"; [/php]



    Thanks again for the replies guys, I wasn't too sure if I'd get any help and I certainly appreciate your time.
  • edited May 2007
    Whats up guys, revisiting this same issue as still hasn't been fixed.

    Here's what I know:

    Their site was hosted through liquidweb; and the code (emails) worked fine. In an effort to get more secure, they requested that our network guy switch their mail server from liquidweb to an exchange server within their building.

    They still recieve emails in general, but nothing that's generated from a php form; just your typical day to day emails.

    (Earlier I had spoken about how they were recieving emails from another php form but I finally figured out that's incorrect).

    So I called liquidweb (who has access to their php.ini) and they said ""it still looks good, besides nothings changed in regards to the php.ini file since they switched to an exchange server".

    And it's not any spam filter/junk mail, they said there aren't any emails in there.

    So what could it be? Any ideas? It seems like it has to be something with the switch to an exchange server but nothing comes to mind...
  • edited May 2007
    UPDATE!
    Alright I called liquid web today and finally got a solution. I don't understand all the terms but I get the basic idea. If anyone sees this and wants to go back and correct the terminology go for it.

    When our network guy setup the new exchange server he had to get some new MX records (DNS stuff), in addition he had to specify whether the exchange connection was remote or local.

    When he called liquidweb to do this, there was a misunderstanding and the MX records weren't setup correctly. (I believe it was remote when it was supposed to be local). So the emails would go to the liquidweb mail server, but never get sent to the exchange server, they were just ignored.

    As I said, if anyone reads this and knows what I'm really trying to say go ahead and correct me...either way problem solved.

    Thanks again.
  • a2jfreaka2jfreak Houston, TX Member
    edited May 2007
    Glad you were able to find the solution.
  • edited May 2007
    I'm trying to use the code on a page that would let people send their cv's (doc or pdf) along with freeform application text. The problem is that when I send the email to my Yahoo address, I get the attachment but not the text part, but when I send it to my work address, which uses Horde, I get the text but not the attachment.. I wonder what I should change in the code?

    Here's the code I'm using:
    function sendMail() {
        $to_name = "";
        $from_name = "webrekry";
        $subject = stripslashes($_POST['jobtitle']);
        $body = stripslashes($_POST['applicationtext']);
        $to_email = "my email";
    
        $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";
        $message .= "$body\n";
        $message .= "\n";
        $message .= "------=MIME_BOUNDRY_message_parts--\n";
        $message .= "\n";
    
        foreach($_FILES as $file => $value) {
            $_tmpname = $_FILES[$file]['tmp_name'];
            $_filename = $_FILES[$file]['name'];
            if (is_uploaded_file($_tmpname)) {
                $pathinfo = pathinfo($_filename);
                $ext = $pathinfo['extension'];
                if ($ext=="doc" || $ext=="pdf") { 
                $fp = fopen($_tmpname, "rb"); 
                $data = fread($fp, filesize($_tmpname)); 
                $data = chunk_split(base64_encode($data)); 
                $message .= "------=MIME_BOUNDRY_main_message\n";
                $message .= "Content-Type: application/octet-stream;\n\tname=\"" . $_filename . "\"\n";
                $message .= "Content-Transfer-Encoding: base64\n";
                $message .= "Content-Disposition: attachment;\n\tfilename=\"" . $_filename . "\"\n\n";
                $message .= $data;
                $message .= "\n\n";
                fclose($fp);
                }
                else { 
                echo "(Attachment file type not supported. Attachment was not sent.)
    ";
                }
            }
        }
        $message .= "------=MIME_BOUNDRY_main_message--\n";
        $ok = mail("$to_name <$to_email>", $subject, $message, $headers);
        if ($ok == 1) print "We've received your application.
    ";
        else print "Application could not be sent.
    ";
    }
    
  • edited May 2007
    Hmmm...that an interesting one.

    I've experienced the problem where I get the attachment but not the text...happened on my gmail account. I guess thats why alot of forms request that you not use the free email accounts as they're not as robust as others.

    But I've never experienced not getting the attachment. Are you using the code copied from this thread? After a quick review of your code it looks like you're not...Try copying this code into a new test page, and see if that works. Then at least you'll know that it's some sort of error in your current code, and not that your inbox is globally blocking the attachment(s) for some reason.
  • edited May 2007
    Thanks for your reply! The code I'm using above is actually the one from this thread, but I've just changed its appearance a bit (had the same problems with the attachments even before tweaking the code).

    But anyways, I got it to work with a different bit of code that requires the attached file to be on the server already.. Not a very good solution, I know, but it works for now. I check the files in several ways to try and make sure they are what they're supposed to be, before letting them into the server, then send the MIME mail, and then discard the uploaded file.
  • edited May 2007
    Hi All

    New to this site, found this thread using Google (of course :D) I needed exactly what this code does... in fact for the same thing as kuuttilainen is using it for to get CV's uploaded. I am a reasonable PHP modifier, but not a programmer yet. I have one question that is probably quite easy to add to this code, basically I would like the form to clear from the page and then display the thank you text and perhaps a link back to my home page. This would tidy things up a bit so they user doesn't need to use their browser back button or click on a menu item to navigate away from the form page. Is this possible?

    Cheers :cool:
  • a2jfreaka2jfreak Houston, TX Member
    edited May 2007
    ltdan wrote:
    Hi All

    New to this site, found this thread using Google (of course :D) I needed exactly what this code does... in fact for the same thing as kuuttilainen is using it for to get CV's uploaded. I am a reasonable PHP modifier, but not a programmer yet. I have one question that is probably quite easy to add to this code, basically I would like the form to clear from the page and then display the thank you text and perhaps a link back to my home page. This would tidy things up a bit so they user doesn't need to use their browser back button or click on a menu item to navigate away from the form page. Is this possible?

    Cheers :cool:

    Yes, it's quite possible. After the form is submitted and processed, have the form return a link to your homepage. If you don't know where to add this, I will try to remember to post a modification tomorrow for you.
  • edited May 2007
    Ok thanks a2jfreak, I will keep looking around for snippets, I have found the standard code for redirect, but I can't seem to work out how to include it with the existing code<style="css" type="text/css">. I tried to post my code and some code that I found, but even though I wrapped it in code tags it said I was trying to link and I had to take it out.:confused:

    Thanks in advance, LtDan :bigggrin:
    </style="css">
  • a2jfreaka2jfreak Houston, TX Member
    edited May 2007
    You should include code inside php tags, not code tags. (I know it doesn't always make sense because not all code is php...)

    Anyway, in the sendMail() function of your php file, you will see:
    [php]
    if ($ok == 1) {
    print "Mail sent to $to_name ($to_email).";
    } else {
    print "Mail did not send. Please press the <b>back</b> button and re-submit the e-mail.";
    }
    [/php]

    All you need to do it add a link to the "Mail sent" or "Mail did not send" lines.

    If you want it to redirect, then you need to print the headers with the redirect located there. Simplest way is to just put a link on the page.
    ltdan wrote:
    Ok thanks a2jfreak, I will keep looking around for snippets, I have found the standard code for redirect, but I can't seem to work out how to include it with the existing code<style="css" type="text/css">. I tried to post my code and some code that I found, but even though I wrapped it in code tags it said I was trying to link and I had to take it out.:confused:

    Thanks in advance, LtDan :bigggrin:
    </style="css">
  • edited May 2007
    Just out of interest, is there are reason we are using the 'print' command and not an 'echo' command in this code? I am used to using echo in php but have not seen the print command used. Sorry, bit off topic, but this is why I think I am finding it difficult to put the redirect code into the print command.

    Thanks again
  • a2jfreaka2jfreak Houston, TX Member
    edited May 2007
    I don't recall all the differences, but I think the only difference is that print actually returns a value when it prints (like a function) whereas echo does not, however for this usage there is no difference. Change it to echo if you prefer.
    ltdan wrote:
    Just out of interest, is there are reason we are using the 'print' command and not an 'echo' command in this code? I am used to using echo in php but have not seen the print command used. Sorry, bit off topic, but this is why I think I am finding it difficult to put the redirect code into the print command.

    Thanks again
  • edited May 2007
    With regard to your earlier reply, rather than a link / redirect back to home, is there a way to make the form disappear and just show the thank you message on a blank screen, like some kind of cls command in php. At the moment it just shows the thank you message at the top of the form after sending it, it just doesn't seem finished. I am thinking like when you post on a phpbb forum, the screen clears and says thank you for your post on a seperate screen and then puts you back to the thread to view your post.
  • a2jfreaka2jfreak Houston, TX Member
    edited May 2007
    Yes. It will not be difficult to do. Not to be rude, but you should try to figure it out yourself. If you haven't figured it out I will post the code when I go to work (Tuesday).
    ltdan wrote:
    With regard to your earlier reply, rather than a link / redirect back to home, is there a way to make the form disappear and just show the thank you message on a blank screen, like some kind of cls command in php. At the moment it just shows the thank you message at the top of the form after sending it, it just doesn't seem finished. I am thinking like when you post on a phpbb forum, the screen clears and says thank you for your post on a seperate screen and then puts you back to the thread to view your post.
  • edited July 2007
    I stumple upon this script via Google today. The script seems to be what I am looking for, except is it possible to attach files that are located on your server rather than desktop? So, when the Browse button is clicked by an end user, the script lists files that are located within a directory and allows the users to select the file to send? I realize HTML form does not support such function thus, I will need to create some sort of graphic browse button that links to a script. So far, I can only open server directory via opendir() function in PHP. Can't seem to figure out the attachment portion... Please help.
  • a2jfreaka2jfreak Houston, TX Member
    edited July 2007
    It can definitely be done. You just have to ask yourself whether it's worth your time to write it or not. Unfortunately, I do not have the time to assist you with it; perhaps someone else will assist you if you are unable to figure it out yourself.
  • edited July 2007
    It's worth my time, but I need pointers.
  • LincLinc Owner Detroit Icrontian
    edited July 2007
    You'll need to make a list of the available files and present them to the user. Then, based on which one the user selected on the form, the script will need to pick the right file to send.

    The easiest way to do this is a drop down list, with each file having a numeric value, e.g. "My picture.jpg" is 1, "This file.txt" is 2, etc.

    The form submits the values. So, if you wanted "This file.txt" the script would get the number 2.

    A switch statement would then tell the script to go get "This file.txt" if it's sent a '2'.

    This makes the whole process vastly easier than trying to directly link the files to the form, and it also makes the system more secure, so that people can't traverse your file system or know where the files are at, even.
  • edited August 2007
    Hi.

    I found this e-mail script through Google search and it looks great. I've tried testing it on a test-site of mine and am having a problem with it.

    When I send an e-mail it comes through with the attachment fine, however the body text is always missing. (if I add an attachment or not).

    I've tried looking to see what could be wrong (as I know a bit of php), butI am really stuck. Please can anyone help with a suggestion?

    I think maybe it could be the wrong info in $headers and $message ? Maybe more stuff needs to go in $message rather than $header? Agh, I really dont know. lol

    All help will be greatly appreciated :)

    ++ Ive tested the script on both recieving email through outlook and gmail. Both give same result. (although no paperclip symbol appears in outlook for some odd reason).
  • edited August 2007
    If it helps, I managed to create a script for just sending text. It works fine.

    So surely this must mean I have everything setup right?

    I just can't seem to recieve text with the attachment script provided in this forum. does anyone have any ideas? :(
  • a2jfreaka2jfreak Houston, TX Member
    edited August 2007
    You're positive you're using the last version that I posted on here? Which O/S are you using to run PHP?
  • edited September 2007
    im trying to post the code but i keep getting an error message saying i cant post links.. lol. Ive taken everything i can think of out and it still wont let me. I will try sending you a PM :)
  • LincLinc Owner Detroit Icrontian
    edited September 2007
    nodd wrote:
    i keep getting an error message saying i cant post links..
    Fixt, you can now. :)
  • edited September 2007
    Thankyou! :)

    Okay, so here is the source code I am using:

    [php]
    /* Mailer with Attachments */

    $action = $_REQUEST;
    global $action;

    function showForm() {
    ?>

    <form enctype="multipart/form-data" name="send" method="post" action="index.php?section=careers&page=application_form2">
    <input type="hidden" name="action" value="send" />
    <input type="hidden" name="MAX_FILE_SIZE" value="10000000" /><br />
    <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_text = stripslashes($_POST);
    $to_email = $_POST;

    //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";
    //
    //if (checkbox_form.checkbox[counter].checked)
    //Return-Receipt-To: x@x.com
    //X-Confirm-Reading-To: x@x.com
    //Disposition-Notification-To: x@x.com
    //
    $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 .= "Hi $to_name \n\n $body_text\n";
    $message .= "\n";
    $message .= "
    =MIME_BOUNDRY_message_parts--\n";
    $message .= "\n";

    foreach($_FILES as $file => $value) {
    $_tmpname = $_FILES[$file];
    $_filename = $_FILES[$file];
    if (is_uploaded_file($_tmpname)) { //Do we have a file uploaded?
    $fp = fopen($_tmpname, "rb"); //Open it
    $data = fread($fp, filesize($_tmpname)); //Read it
    $data = chunk_split(base64_encode($data)); //Chunk it up and encode it as base64 so it can emailed
    $message .= "
    =MIME_BOUNDRY_main_message\n";
    $message .= "Content-Type: application/octet-stream;\n\tname=\"" . $_filename . "\"\n";
    $message .= "Content-Transfer-Encoding: base64\n";
    $message .= "Content-Disposition: attachment;\n\tfilename=\"" . $_filename . "\"\n\n";
    $message .= $data; //The base64 encoded message
    $message .= "\n\n";
    fclose($fp);
    }
    }

    $message .= "
    =MIME_BOUNDRY_main_message--\n";

    // send the message

    $ok = mail("$to_name <$to_email>", $subject, $message, $headers);

    if ($ok == 1) {
    print "Mail sent to $to_name ($to_email).";
    } else {
    print "Mail did not send. Please press the <b>back</b> button and re-submit the e-mail.";
    }
    }
    }

    ?>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
    <html xmlns="http://www.w3.org/1999/xhtml&quot; 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]


    I've used other mailing scripts which work fine, but they lacked the attachment addition. When I use this script, I recieve the attachment but not the actual text.

    I'm not fussed about multiple attachments, only 1 :)
  • a2jfreaka2jfreak Houston, TX Member
    edited September 2007
    I just saw this message, but unfortunately I have to leave. I will try to go over this tomorrow and help you out if someone else doesn't come along before then.

    If I don't post tomorrow shoot me another PM as it means I probably forgot; it doesn't mean that I didn't want to help and just blew it off.
  • edited September 2007
    Thankyou :)
  • edited September 2007
    Is there a way to hide the recipient email and recipient name on my site it is only going to one email address?

    Thanks
    Andrew
  • KwitkoKwitko Sheriff of Banning (Retired) By the thing near the stuff Icrontian
    edited September 2007
    Get rid of recipient name email input boxes and change the $to_email variable to your email address and $to_name to your name.<code style="white-space: nowrap;"><code></code></code>
Sign In or Register to comment.