user623990
user623990

Reputation:

Issues with a PHP script to mail with an attachment (no libraries)

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 = '[email protected]';
        $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: [email protected]\r\nReply-To: [email protected]";
        $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.

Upvotes: 0

Views: 508

Answers (1)

Marc B
Marc B

Reputation: 360672

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.

Upvotes: 1

Related Questions