Hey all!
What I am working on is a contact form with file upload functionality. (e.g. user should be able to upload a screenshot.) Uploading a file should not be mandatory. So the mail which is produced from the entered information in the form should be sent without an attachment in this case. To do this, I have made this code:
if ($_FILES["upload"]["size"] == 0) {
//No file was uploaded. No problem: the mail is being sent without an attachment:
if ($mail->Send()) {
echo "Mail sent.<br />";
} else {
echo "Error with $mail->Send()";
}
} elseif ($_FILES["upload"]["size"] > 0) {
if(
( ($_FILES["upload"]["type"] == "image/gif")
|| ($_FILES["upload"]["type"] == "image/jpeg")
|| ($_FILES["upload"]["type"] == "image/pjpeg")
|| ($_FILES["upload"]["type"] == "image/png")
|| ($_FILES["upload"]["type"] == "image/x-png")
|| ($_FILES["upload"]["type"] == "application/pdf")
) && ($_FILES["upload"]["size"] < 2097152)
) {
// If a file was uploaded and meets the above conditions, do this:
move_uploaded_file($_FILES['upload']['tmp_name'], $target_path);
$mail->AddAttachment('uploads/'.basename($_FILES["upload"]["name"]).'', ''.basename( $_FILES["upload"]["name"]).'');
echo "File successfully uploaded!<br />";
// Send mail with added attachment:
if ($mail->Send()) {
echo "Mail sent with attachment.";
} else {
echo "Error sending mail (with attachment)";
}
} else {
echo "Your uploaded file did not meet our conditions. Therefore, the mail was not sent.";
}
}
My problem is that this code doesn't quite do what I want:
- When no file is being uploaded, the mail is sent. So this is OK.
- When a file is uploaded, which meets the conditions. The mail is sent WITH the attachment. This also is as wanted.
- When a file is being uploaded which /doesn't/ meet the conditions, the mail is still being sent (what is not what I want!) and without attachment... For some weird reason line 30 is not executed...
Does anybody know what I am doing wrong here? Google didn't give me much love today. :(
Thanks!