Simple PHP Form: Attachment to Email (Code Golf)

Simple PHP form: Attachment to email (code golf)

Just for fun I thought I'd knock it up. It ended up being trickier than I thought because I went in not fully understanding how the boundary part works, eventually I worked out that the starting and ending '--' were significant and off it went.

<?php
if(isset($_POST['submit']))
{
//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 = 'me@example.com';
$subject = 'a file for you';

$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: 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);
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>MailFile</title>
</head>

<body>

<?php echo $output; ?>

<form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" <?php echo $flags;?>>
<p><label for="message">Message</label> <textarea name="message" id="message" cols="20" rows="8"></textarea></p>
<p><label for="file">File</label> <input type="file" name="file" id="file"></p>
<p><input type="submit" name="submit" id="submit" value="send"></p>
</form>
</body>
</html>

Very barebones really, and obviously the using inline CSS to hide the form is a bit cheap and you'd almost certainly want a bit more feedback to the user! Also, I'd probably spend a bit more time working out what the actual Content-Type for the file is, rather than cheating and using application/octet-stream but that part is quite as interesting.

simple php form send attachment no mime/libraries

No unfortunately there is no simple way to do this. First of all you'll HAVE to find a way to upload the file to the server, otherwise your PHP script will not to able to access a file (on your desktop).

If you really want to avoid server side programming, you may want to consider a javascript /hosted solution like FilePicker.io that let's you upload files from your desktop without server-side code, but this still wont' solve your email problem.

How to get email and their attachments from PHP

What MTA are you using? If you use postfix + maildrop you can create a filtering rule that pipes certain messages through a PHP script that then handles the incoming mails. (google for maildrop and xfilter).

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

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.

Autogenerating attachment of content from contact form and sending with email

To generate a file using the form values, see this example.

If You want to send an e-mail using php mail() with text and attachment, You have to mess a little bit with the headers. Add Content-Type: multipart/mixed; to the headers, see example.

Compiling email with multiple attachments in PHP

Going from memory:

SaHPETExQ6HQ0BB4Z8FwTr/KHVc/AJ98jIf2BGdKAAAAAElFTkSuQmCC

--==Multipart_Boundary_x38e1b83d34375e183a2fdcd6a9c001f8x--
^^^ DELETE THIS LINE ^^^

--==Multipart_Boundary_x38e1b83d34375e183a2fdcd6a9c001f8x

You shouldn't have two boundaries next to each other, and the "--" at the end is only for the end of the last part.



Related Topics



Leave a reply



Submit