Form to sending emails with file attachment - problem

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
polak2468
New php-forum User
New php-forum User
Posts: 3
Joined: Sat Jun 02, 2018 8:25 am

Sat Jun 02, 2018 8:43 am

Bardzo proszę o wskazówki/pomoc.

Hi all. I have a problem with my script pasted below. Script from assume should do:
1. After click "Send" should send an email to me.
2. Subject schema: Problem/$counter/$date, where $counter will be incremented after each email send.
3. Adding attachment/ graphic file to form and sending.
4. Validation - protect my mailbox to get spam messages.
5. Size of the attachment can not be more than 2MB.
6. Saving informations which put in form to database.

Currently after click "Send" mail does not send. Please give me some advices/help.

Code: Select all

<!DOCTYPE html>
<html>
	<head>
		<title>HelpDesk - send your problem</title>
		<style>
			.error {
				color: #FF0000;
			}
			.mailmsg {
 				color: #00ddff;
			}
		</style>
	</head>
<body>

<?php


	//serwer
	//$server = "localhost";
	//admin
	//$admin = "root";
	//hasło
	//$pass = "ZAQ!2wsx";
	//nazwa bazy
	//$base = "test";
	//$dbconnection = mysqli_connect($server, $admin, $pass, $base) or die ("There is no connection to the server.");

	
	$nameErr = $surnameErr = $companyErr = $emailErr = $commentErr = $upload_fileErr = "";
	$name = $surname = $company = $email = $comment = $upload_file = $mailMsg = $headers = $body = $content = "";
	$counter = 0;
	
	if ($_SERVER["REQUEST_METHOD"] == "POST"){

		if (empty($_POST["name"])) {
			$nameErr = "Please write your name.";
		} else {
			$name = prepare_data($_POST["name"]);
			#check that name contains only letters and length from 3 - 20 signs
			if (!preg_match("/^[A-Z]([a-z]{3,20})$/", $name)){
				$nameErr = "You have to write letters. Name's length 3 - 20 signs.";
			}
		}
		
		if (empty($_POST["surname"])) {
			$surnameErr = "Please write your surname.";
		} else {
			$surname = prepare_data($_POST["surname"]);
			if (!preg_match("/^[A-Z]([a-z]{3,20})$/", $surname)){
				$surnameErr = "You have to write letters. Surname's length 3 - 20 signs.";
			}
		}
		
		if (empty($_POST["company"])) {
			$companyErr = "Please write your company name.";
		} else {
			$company = prepare_data($_POST["company"]);
			if (!preg_match("/^[A-Z]([a-z])$/", $company)){
				$companyErr = "You have to write letters.";
			}
		}
		
		if (empty($_POST["email"])) {
			$emailErr = "Please provide your contact email.";
		} else {
			$email = prepare_data($_POST["email"]);
			if (!filter_var($email, FILTER_VALIDATE_EMAIL)){
				$emailErr = "You have to write proper email, because it will be used to contact with you.";
			}
		}
			
		if (empty($_POST["comment"])) {
			$commentErr = "Please, write your problem here.";
		} else {
			$comment = prepare_data($_POST["comment"]);
		}
		
		#$upload_file = $_POST["upload_file"];
		$file = ($_FILES["upload_file"]["name"]);
		$fileType = strtolower(pathinfo($file, PATHINFO_EXTENSION));
		
		#file was uploaded without errors
		if ($file && (!$_FILES["upload_file"]["error"])){
			#allow for some file formats
			if ($fileType != "jpg" && $fileType != "png" && $fileType != "jpeg"){
				$upload_fileErr = "There are allowed only files with extensions JPG, PNG, JPEG.";
			}
			
			#file can not be higher 2MB
			if ($file > (2048000)) {
				$upload_fileErr = "File's size if greater than 2MB.";
			}
			$content = chunk_split(base64_encode(file_get_contents($file)));
		} //else {
			//$upload_fileErr = "There is a problem with attaching the file:  code".$_FILES["upload_file"]["error"];
		//}
			
			
		$counter += 1;
		$toEmail = "some@email.com"; #here set email, who get the message with attachment
		$subject = "Problem/$counter/date('Y/m/d h:i:sa')"; #subject format -> Problem/next_number_incremented_after_each_send_of_email/data Y/M/D H:M:S
		
		
		$strSid = md5(uniqid(time()));
		
		$eol = "\r\n";
		
		#main header
		$headers .= "From: $email" . $eol;
		$headers .= "MIME-version: 1.0" . $eol;
		$headers .= "Content-Type: multipart/mixed; boundary=\"" . $strSid . "\"" . $eol;
		$headers .= "Content-Transfer-Encoding: 7bit" . $eol;
		$headers .= "This is a MIME encoded message." . $eol;
		
		#body of email
		$body .= "--" . $strSid . $eol;
		$body .= "Content-Type: text/plain; charset=utf-8" . $eol;
		$body .= "Content-Transfer-Encoding: 8bit" . $eol;
		$body .= $comment . $eol;
		
		#attachment
		$body .= "--" . $strSid . $eol;
		$body .= "Content-Type: application/octet-stream; name=\"" . $file . "\"" . $eol;
		$body .= "Content-Transfer-Encoding: base64" . $eol;
		$body .= "Content-Disposition: attachement" .$eol;
		$body .= $content . $eol;
		$body .= "--" . $strSid . "--";

		#sending email
		if (mail($toEmail, $subject, $body, $headers)) {
			$mailMsg = "Mail is send successfully.";
			echo '<span class="mailmsg"><?php echo $mailMsg; ?></span>"';
		} else {
			$mailMsg = "There is a problem with sending your email.";
			echo '<span class="mailmsg"><?php echo $mailMsg; ?></span>"';
		}
	
	}	
	
	#
	function prepare_data($form_data) {
		$form_data = trim($form_data); #skasowanie białych znaków
		$form_data = stripslashes($form_data); #kasowanie backslashy
		#return $form_data; #I am curious does this function return data automatically or need write it explicitely
	}
?>
	<p><span class="error">* required fields</span></p>
	
	<form action="helpdesk.php" method="post" enctype="multipart/form-data">
		<label for="name">Your name:</label>
		<input type="text" name="name"><span class="error">* <?php echo $nameErr; ?></span><br>
		<label for="surname">Your surname:</label> 
		<input type="text" name="surname"><span class="error">* <?php echo $surnameErr; ?></span><br>
		<label for="company">Company:</label> 
		<input type="text" name="company"><span class="error">* <?php echo $companyErr; ?></span><br>
		<label for="email">Email:</label> 
		<input type="text" name="email"><span class="error">* <?php echo $emailErr; ?></span><br>
		<label for="comment">Problem:</label> 
		<textarea name="comment" rows="5" cols="40"></textarea><span class="error">* <?php echo $commentErr; ?></span><br>
		<label for="upload_file">Attach the file</label><br> 
		<input type="file" name="upload_file"><span class="error"><?php echo $upload_fileErr; ?></span>
		<input type="submit" name="submit" value="Send">
	</form>
	
	

</body>
</html> 

User avatar
hyper
php-forum Fan User
php-forum Fan User
Posts: 728
Joined: Mon Feb 22, 2016 5:52 pm

Sat Jun 02, 2018 11:59 am

What looks to be working? (what is it doing?)

What PHP error messages are you getting?

polak2468
New php-forum User
New php-forum User
Posts: 3
Joined: Sat Jun 02, 2018 8:25 am

Sat Jun 02, 2018 12:25 pm

There is no effect after click "Send". I googled that I should configure sendmail in xampp. I have done it, mails are sending but I have some other problems. Mail, which I get contains attachment but it's size is even 0kb. In browser (where I used the form) I have Warning: file_get_contents(TRS 301 with full upgrades.jpg): failed to open stream: No such file or directory in D:\programy\xampp\htdocs\skrypt.php on line 108. That's right. This file isn't in that path. I choose this file, so it exists. ;)
By the way I don't understand why after click "Send" all fields are validated like they would be empty. There shouldn't be any information near each input field after click "Send".

User avatar
hyper
php-forum Fan User
php-forum Fan User
Posts: 728
Joined: Mon Feb 22, 2016 5:52 pm

Sun Jun 03, 2018 5:20 am

On form submission, your script attempts to send the email even where validation errors have occurred.

To debug, I would take out the file uploading until you have the basics worked out.

Your function comments out the return:

Code: Select all

#return $form_data;
I have to ask:

Code: Select all

$toEmail = some@email.com
I presume is just for posting?

I suggest that you put something like this:

Code: Select all

echo $name, '<br>', $nameErr;
following each check to make sure it is doing what you expect.

polak2468
New php-forum User
New php-forum User
Posts: 3
Joined: Sat Jun 02, 2018 8:25 am

Sun Jun 03, 2018 7:45 am

hyper wrote:
Sun Jun 03, 2018 5:20 am
On form submission, your script attempts to send the email even where validation errors have occurred.

To debug, I would take out the file uploading until you have the basics worked out.

Your function comments out the return:

Code: Select all

#return $form_data;
I have to ask:

Code: Select all

$toEmail = some@email.com
I presume is just for posting?

I suggest that you put something like this:

Code: Select all

echo $name, '<br>', $nameErr;
following each check to make sure it is doing what you expect.
Exactly, the form submission is executed even some input data are not valid. This function has commented out this return, because I am curious I should add it explicite or not. Do you know maybe how it should be? Of course mail is for posting purposes only. ;)
I am going to add now what did you suggest. ;)

PS
I did some test and when I put the line

Code: Select all

echo $name, '<br>', $nameErr;
in first check (name checking) before "else" as result $name is empty (like it should be) and $nameErr contains message that I should put my name. When I put the line in nested if statement and put in form some rubbish like "4bdfg2" I have got a message that I should put letters. This message is assigned to variable $nameErr like it should. Of course then $name is empty.

Post Reply