Hi there,

I am having trouble with a php mail attachment. I have never done this before. I have got to the point where the script sends a blank message with a title... but no attachment and no body of the email is seen. I have opened up the email as a text document and can see the output is all there (so it has sent it). So there must be a syntax error where the mail content-code info stuff is concerned?

Anyway, after hours and hours, I can't figure it out. Is there anyone who could help me out please?

Here is the code I have:

if(isset($_POST['submit_x']))
	{
		$name = $_POST['name'];
		$email = $_POST['email'];
		$drawingName = $_POST['drawingName'];
		$tempFile = $_FILES['attachment']['tmp_name']; //set name variable
		$filename = $_FILES['attachment']['name']; //set name variable
		
		$emailStatus = validate_email($email);
		
		
		
		if($name=="" || $email=="" || $drawingName=="" ||$name=="Your Name" || $email=="Your Email" || $drawingName=="Give your Drawing a Name")
		{
			header("location:competition.php?message=blank");
			exit();
		}
		
		if($emailStatus==false)
		{
			header("location:competition.php?message=email");
			exit();
		}
		
		if($tempFile=="")
		{
			header("location:competition.php?message=nofile");
			exit();
		}
		
		if ((($_FILES["attachment"]["type"] == "image/gif")
		|| ($_FILES["attachment"]["type"] == "image/png")
		|| ($_FILES["attachment"]["type"] == "image/jpeg")
		|| ($_FILES["attachment"]["type"] == "image/pjpeg"))
		&& ($_FILES["attachment"]["size"] < 20000))
  		{
			header("location:competition.php?message=file");
			exit();
		}
		
		
		//move file to intended folder
		$fileType = $_FILES["attachment"]["type"];
		$fullFileLocation = "tempuploads/".$filename;
		$success = move_uploaded_file($tempFile, $fullFileLocation);
		
		
		
		$to = "help@lala.co.uk";
		$subject = "A tester email";
		$random_hash = md5(date('r', time()));
		$headers = "From: $email\r\nReply-To: $email";
		$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
		$attachment = chunk_split(base64_encode(file_get_contents($fullFileLocation)));
		$output = "--PHP-mixed-$random_hash;
			Content-Type: multipart/alternative; boundary='PHP-alt-$random_hash'
			--PHP-alt-$random_hash
			Content-Type: text/plain; charset='iso-8859-1'
			Content-Transfer-Encoding: 7bit
			 
			Hello $name!
			This is the simple text version of the email message.
			 
			--PHP-alt-$random_hash
			Content-Type: text/html; charset='iso-8859-1'
			Content-Transfer-Encoding: 7bit
			 
			<h2>Hello World!</h2>
			<p>This is the <b>HTML</b> version of the email message.</p>
			 
			--PHP-alt-$random_hash--
			 
			--PHP-mixed-$random_hash
			Content-Type: application/zip; name=$fullFileLocation
			Content-Transfer-Encoding: base64
			Content-Disposition: attachment
			 
			$attachment
			--PHP-mixed-$random_hash--";
			
			
		 
		if(mail($to, $subject, $output, $headers))
		{
			 
			//unset file
			if (file_exists($fullFileLocation))
			{
				//unset file
				unset($fullFileLocation);
			}
		
			//header("location: competition.php?message=success");
			exit(); 
		}
		  
	}

(Apologies for the long code. I guess in full context it's easier to help.)

Any help would be greatly appreciated.

Cheers

Danny

Recommended Answers

All 3 Replies

I was also facing so many issues for mail attachment.
Finally i end up with this.
I have made this function, hope this helps you.

<?php

	//=======================================
	//   mail sending start
	//=======================================
		$to = 'vibha@domainname.com';
		$fromName = 'Administrator';
		$fromEmail = 'admin@domainname.com';
		$subject = 'Subjest is here';		
		$message = 'Message is here..Message is here..Message is here......Message is here';
		$template = 'demo.tpl';				
		$files = array('demo.txt','demo.xlsx');		
		if(sendEmail($to,$fromName,$fromEmail,$subject,$message,$cc='',$bcc='',$files))
			echo 'Mail successfully sent.';
		else
			echo 'Mail sending failed.';
	//=======================================
	//   mail sending end
	//=======================================	


function sendEmail($to,$fromName,$fromEmail,$subject,$message,$cc='',$bcc='',$files=array(),$debug=false)
{
	// starting of headers
	$headers = 'From: '.$fromName.' <'.$fromEmail.'>';
	if($cc != '')
		$headers .= "\r\nCc: ". $cc;
	if($bcc != '')
		$headers .= "\r\nBcc: ". $bcc;
		
	// boundary 
	$semi_rand = md5(time()); 
	$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 
	 
	// headers for attachment 
	$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; 
	 
	// multipart boundary 
	$message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/html; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n"; 
	if(count($files))
		$message .= "--{$mime_boundary}\n";
	 
	// preparing attachments
	for($x=0;$x<count($files);$x++)
	{
		$file = fopen($files[$x],"rb");
		$data = fread($file,filesize($files[$x]));
		fclose($file);
		$data = chunk_split(base64_encode($data));
		$message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$files[$x]\"\n" . 
		"Content-Disposition: attachment;\n" . " filename=\"$files[$x]\"\n" . 
		"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
		if($x < (count($files)-1))
			$message .= "--{$mime_boundary}\n";
		else
			$message .= "--{$mime_boundary}--\n";	
		
	}
		
	if($debug)
	{
		echo '<br />$to: '.$to;
		echo '<br />$fromName: '.$fromName;
		echo '<br />$fromEmail: '.$fromEmail;
		echo '<br />$subject: '.$subject;
		echo '<br />$message: '.$message;
		echo '<br />$cc: '.$cc;
		echo '<br />$bcc: '.$bcc;
		echo '<br />$file: '.print_r($file);		
	}
	
	if(mail($to, $subject, $message, $headers))
	{		
		return true;
	}
	else
	{		
		return false;
	}
}

?>

Thank you!!! I did have to put the function before it was called (for anyone else using this code), otherwise you get a 'HTTP Error 500 Internal server' error.

Works perfect other than that. I didn't really have to change anything other than the obvious; email address, title and message etc... Exactly what I was after.

Much appreciated!

Cheers

Danny

This function was a life saver for me as I was also struggling with blank attachments. Thanks so much.

The one thing I'd like to adjust is: For the 'files' parameter, I am passing the function an href from a web page with a linked document:

C:\directory\files\PPR.docx

But the email attachments have the directory prepended with underscores:

C__directory_files_PPR.docx

I can't figure out what is causing it, so any input would be welcome. Thanks again, 10 years later!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.