0

I've put together an HTML form a which sends its input to itself and proceeds to send an email with the form contents and an attached file. I took the accepted answer from this post and tried to integrate it into my form. However, my code is completing without errors but I don't get an email. Here is part of it:

<?php
    if(isset($_POST['submit2']))
    {
        //The form has been submitted, prep a nice thank you message
        $output = '<h1>Thanks for your file and message!</h1>';

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

        //Deal with the email
        $to = 'testing@example.com';
        $subject = 'Article Submission';

        $message = $_POST['name'].'\n'.$_POST['email'].'\n\n'.$_POST['title'].'\n'.$_POST['remarks'];
        $attachment = chunk_split(base64_encode(file_get_contents($_FILES['file']['tmp_name'])));
        $filename = $_FILES['file']['name'];

        $boundary =md5(date('r', time())); 

        $headers = "From: webmaster@example.com\r\nReply-To: webmaster@example.com";
        $headers .= "\r\nMIME-Version: 1.0\r\nContent-Type: multipart/mixed; boundary=\"_1_$boundary\"";

        $message="This is a multi-part message in MIME format.

--_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);
    }
?>


<h2> Article Submission </h2>
(*) Required Fields<br>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
<table>

<tr>
    <td> Name </td>
    <td> <input type="text" name="name" size="30"> * </td>
</tr>
<tr>
    <td> Email </td> 
    <td> <input type="text" name="email" size="30"> * </td>
</tr>

<tr> <td>&nbsp;</td><td>&nbsp;</td> </tr>

<tr>
    <td> Title of Article </td>
    <td> <input type="text" name="title" size="40"> * </td>
</tr>
<tr>
    <td> Program </td>
    <td></td>
</tr>
<tr>
    <td> Course </td>
    <td> </td>
</tr>
<tr>
    <td> File </td>
    <td> <input type="file" name="file" id="file"> </td>
</tr>

<tr> <td>&nbsp;</td><td>&nbsp;</td> </tr>

<tr>
    <td valign="top"> Remarks </td>
    <td> <textarea rows="6" cols="40" name="remarks"> </textarea>
</tr>
<tr>
    <td> <input type="submit" name="submit2" value="Submit" id="submit"> </td>
</tr>

</table>
</form>

</div>
<?php include('include/main_footer.php'); ?>  

The form isn't 100% complete, if you're wondering about the missing inputs.

Community
  • 1
  • 1
  • You don't get the email at all, or you get the email but the attachment isn't right? Because those are two very different questions. – Ariel Jul 22 '11 at 21:42
  • I don't get anything. Not even the email. –  Jul 22 '11 at 21:46
  • In that case simplify - get rid of all the mime stuff and just send a simple email and get that working first. I can't help with that though since you list no errors or logfiles. – Ariel Jul 22 '11 at 21:51

1 Answers1

1

Don't build your own MIME messages. It's too painful and error prone. Use the free Swiftmailer or PHPMailer. As well, mail() will return false if there was a problem handing your email over to the local mail server, so check that value as well.

Swiftmailer and PHPMailer have far better diagnostics and error handling than mail(), so while mail() will tell you that it failed, the other two will tell you WHY things failed.

Marc B
  • 356,200
  • 43
  • 426
  • 500
  • The issue I have with extra libraries is that I don't host my own server and I don't think I can install them. –  Jul 22 '11 at 21:47
  • @MaxMackie: Extra libraries can usually be "installed" by simply uploading the files, even in a shared environment. The ones in question are simply php classes. – Cyclone Jul 22 '11 at 21:52
  • Indeed. both Swift and PHPMailer are just a set of .php files you can stick into your webroot if need be. There's nothing to "install", just a require/include to load them into your script. – Marc B Jul 22 '11 at 22:01
  • Ohhhh! I thought they were like PHP modules that needed to be present at compile time. I'll give Swiftmailer a try and see if I can get something. Thanks for clearing that up. –  Jul 22 '11 at 22:36