0

I would like to offer people to send an attachement together with the rest of their info in the form. Using this Stackoverflow answer and a lot of googling I'm almost happy with what I've got now. However there's a problem: When the user does not attach a file (which is ok) then I get errors that the Filename cannot be empty (the form gets submitted though).

EDIT: I don't want to enforce a file upload. On the contrary, I'm quite happy without a file. But (I think?) join(file($_FILES['f']['tmp_name'])) expects a file and if it doesn't get one will throw an error (while the form is still sent). So I'd like to change it in a way that this line is also ok without a file being uploaded. /EDIT

Unfortunately most of my knowledge comes via trial and error (and recognizing things I've done before). And no matter what I try or google I cannot find a way to avoid the error. Could anyone give me a hand?

    if( empty($errors))
    {
    $s = md5(rand());
    mail('email@example.com', 'attachment', "--$s
    Hello World!
    --$s
    Content-Type: application/octet-stream; name=\"f\"
    Content-Transfer-Encoding: base64
    Content-Disposition: attachment

    ".chunk_split(base64_encode(join(file($_FILES['f']['tmp_name']))))."
    --$s--", "MIME-Version: 1.0\r\nContent-Type: multipart/mixed; boundary=\"$s\"");
    }

(The $errors makes sure the user provided an e-mail address in the form.)

Thanks a lot!

Community
  • 1
  • 1
  • 1
    How are you validating the form data before sending? Please post any related javascript/jQuery code. – cssyphus Dec 13 '13 at 17:11
  • 1
    Hi gibberish, thanks for helping. In php (in case people have deactivated JS) I just do `if(empty($_POST['email']) { $errors .= "\n Error: Please enter your e-mail address"; }` On JS side I would use [this Form Validator](http://www.javascript-coder.com/html-form/javascript-form-validation.phtml) – user3099735 Dec 13 '13 at 17:19
  • Take your attachment-related code out of the encapsulated `if(empty($errors))` and use another method, such as `die("Cannot be empty.");` – Funk Forty Niner Dec 13 '13 at 17:29
  • I think I may have been a bit unclear: I don't want to enforce a file upload. On the contrary, I'm quite happy without a file. But (I think?) `join(file($_FILES['f']['tmp_name']))` expects a file and if it doesn't get one will throw an error (while the form is still sent). So I'd like to change it in a way that this line is also ok without a file being uploaded. Thanks for helping! – user3099735 Dec 13 '13 at 17:35
  • Have you tried removing `(join(file` ? This is part of code I have in one of my files `chunk_split(base64_encode($fileContents))` – Funk Forty Niner Dec 13 '13 at 17:43
  • Would that mean `".chunk_split(base64_encode($_FILES['f']['tmp_name']))."`? While that gets rid of the error when there is no file, it doesn't transfer an upload if there is one. – user3099735 Dec 13 '13 at 17:53
  • I couldn't say for 100%. It was merely a suggestion. I take it you tried it then and it didn't work? – Funk Forty Niner Dec 13 '13 at 18:02
  • Unfortunately it didn't. – user3099735 Dec 13 '13 at 18:06
  • I can post something below that you can try. It's what I've used before which works whether a file is attached or not, if you're interested I can post it below for you to try. – Funk Forty Niner Dec 13 '13 at 18:38
  • OK, I've solved this now. It's probably the ugliest solution possible but it does the trick: I wrapped the whole `mail();` into another `if( empty($_FILES['f']['tmp_name']))`. And then just leave out all the messy parts when there is no file present. EDIT: Thanks for helping, Fred. Unless you see any security issues with my fix I don't want to trouble you for another solution. Again, thanks and all the best! – user3099735 Dec 13 '13 at 18:58
  • You're welcome and it's no trouble. I will post it below and give it a try. – Funk Forty Niner Dec 13 '13 at 19:03
  • Did you try what I posted below, by any chance? – Funk Forty Niner Dec 17 '13 at 13:51

1 Answers1

0

For testing purposes only

You're welcome to use this. You will have to do certain modifications, but it works whether a file is attached or not.

For single file attachment.

HTML FORM

<html>
<head>
<title></title>
</head>
<body>
<form action="php_sendmail_upload.php" method="post" name="form1" enctype="multipart/form-data">
<table width="343" border="1">
<tr>
<td>To</td>
<td><input name="txtTo" type="text" id="txtTo"></td>
</tr>
<tr>
<td>Subject</td>
<td><input name="txtSubject" type="text" id="txtSubject"></td>
</tr>
<tr>
<td>Description</td>
<td><textarea name="txtDescription" cols="30" rows="4" id="txtDescription"></textarea></td>
</tr>
<tr>
<td>Form Name</td>
<td><input name="txtFormName" type="text"></td>
</tr>
<tr>
<tr>
<td>Form Email</td>
<td><input name="txtFormEmail" type="text"></td>
</tr>
<tr>
<td>Attachment</td>
<td><input name="fileAttach" type="file"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input type="submit" name="Submit" value="Send"></td>
</tr>
</table>
</form>
</body>
</html>

PHP handler (php_sendmail_upload.php)

<?php
// $strTo = $_POST["txtTo"];
$strTo = "email@example.com";
$strSubject = "Email attach subject";
$strMessage = nl2br($_POST["txtDescription"]);

//*** Uniqid Session ***//
$strSid = md5(uniqid(time()));

$strHeader = "";
$strHeader .= "From: ".$_POST["txtFormName"]."<".$_POST["txtFormEmail"].">\nReply-To: ".$_POST["txtFormEmail"]."";

$strHeader .= "MIME-Version: 1.0\n";
$strHeader .= "Content-Type: multipart/mixed; boundary=\"".$strSid."\"\n\n";
$strHeader .= "This is a multi-part message in MIME format.\n";

$strHeader .= "--".$strSid."\n";
$strHeader .= "Content-type: text/html; charset=utf-8\n";
$strHeader .= "Content-Transfer-Encoding: 7bit\n\n";
$strHeader .= $strMessage."\n\n";

//*** Attachment ***//
if($_FILES["fileAttach"]["name"] != "")
{
$strFilesName = $_FILES["fileAttach"]["name"];
$strContent = chunk_split(base64_encode(file_get_contents($_FILES["fileAttach"]["tmp_name"])));
$strHeader .= "--".$strSid."\n";
$strHeader .= "Content-Type: application/octet-stream; name=\"".$strFilesName."\"\n";
$strHeader .= "Content-Transfer-Encoding: base64\n";
$strHeader .= "Content-Disposition: attachment; filename=\"".$strFilesName."\"\n\n";
$strHeader .= $strContent."\n\n";
}

$flgSend = @mail($strTo,$strSubject,null,$strHeader);  // @ = No Show Error //

if($flgSend)
{
echo "Mail send completed.";
}
else
{
echo "Cannot send mail.";
}
?>
Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141