treehouse : what would you like to learn today?
Web Design Web Development iOS Development

problem sending an attachment to email via php

  • Hey everyone,
    I'm pretty new to this php stuff, but I'm trying to create a page that the user uploads there resume and that gets sent to my email account. Right now it says its been sent, but I don't see it in my inbox at all. I don't know what I'm doing wrong

    Can anyone look at the code and tell me what I'm doing wrong TIA
    here's the link

    and my code:
    <?php
    $name = $_POST['name'];
    $email = $_POST['email'];
    $eligible = $_POST['eligible'];
    $type = $_POST['type'];

    if(isset($_POST['submit']))
    {
    //The form has been submitted, prep a nice thank you message
    $output = "<div id='succsess_page'>";
    "<h1>Email Sent Successfully.</h1>";
    "<p>Thank you <strong>$name</strong>, your resume has been submitted to us.</p>";
    "</div>";


    //Set the form flag to no display (cheap way!)
    $flags = 'style="display:none;"';

    //Deal with the email
    $to = 'andre@cssto.com';
    $subject = 'Magic Bus Company Employment Application Form';

    // $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: $email\r\nReply-To: $to";
    $headers .= "\r\nMIME-Version: 1.0\r\nContent-Type: multipart/mixed; boundary=\"_1_$boundary\"";

    $message="This is a multi-part message in MIME format.
    Name: $name;\r\n\n
    Email: $email;\r\n\n
    Are you eligible to work in Canada? $eligible;\r\n\n
    What type of drivers license do you have? $type;\r\n\n

    --_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"&gt;
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>MailFile</title>
    <link href="stylesheet.css" rel="stylesheet" type="text/css" />
    </head>

    <body>
    <div id="contact">
    <?php echo $output; ?>

    <form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" <?php echo $flags;?>>
    <label for=name accesskey=U><span class="required">*</span> Your Name</label>
    <input name="name" type="text" id="name" size="30" /><br>

    <label for=email accesskey=E><span class="required">*</span> Email</label>
    <input name="email" type="text" id="email" size="30" /><br />

    <label for=subject accesskey=S><span class="required">*</span> Are you eligible to work in Canada?</label>
    <select name="eligible" type="text" id="eligible">
    <option value="Yes">Yes</option>
    <option value="NO">No</option>
    </select><br />

    <label for=comments accesskey=C><span class="required">*</span> What type of dirvers license do you have?</label>
    <input name="type" type="type" id="type" size="30" /><br />



    <!-- <p><label for="message">Message</label> <textarea name="message" id="message" cols="20" rows="8"></textarea></p> -->

    <label>Upload your Resume</label>
    <input type="file" name="file" id="file"><br />
    <br>
    <br>


    <input type="submit" name="submit" id="submit" value="send">
    </form>
    </div>
    </body>
    </html>
  • I would put a condition on the mail() function and see what it returns.

    I think your error/success messages should really be moved down the code so they are dependent on the mail function succeeding rather than just the posting of the form itself.

    if (mail($to, $subject, $body, $headers)) {
    echo("Thank you for your submission.");
    }
    else {
    echo("We're sorry, but there was an error processing your request.");
    }

    This should point you as to whether your PHP system is actually sending the mail at all. I have had problems with the basic mail() function on certain ISP.
  • Thank a lot I'll give it a try and let you know :)