sending mail with attach, bcc and cc

static function multi_attach_mail($to, $subject, $message_body, $files, $sendermail,$cc="",$bcc=""){
		$message_body = '<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>Edify Email</title><body>'.$message_body.'</body></head></html>'; 

		// email fields: to, from, subject, and so on
		//$sendermail = "heenabhalla007@gmail.com";
		$from = "<".$sendermail.">";		
		//$subject = date("d.M H:i")." F=".count($files); 
		$message = date("Y.m.d H:i:s")."\n".count($files)." attachments";
		$headers = "From: $from \r\n";
		if($cc !=""){
			//$cc = "<".$cc.">";		
			$headers .= "Cc: $cc \r\n";
		}
		if($bcc !=""){
			//$bcc = "<".$bcc.">";
			$headers .= "Bcc: $bcc \r\n";
		}
		$headers .= "Reply-To: $sendermail \r\n";
		$headers .= "X-Mailer: PHP/".phpversion();                                 

		// 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 = "--{$mime_boundary}\n" . "Content-Type: text/html; charset=\"iso-8859-1\"\n" .
		"Content-Transfer-Encoding: 7bit\n\n" . $message_body . "\n\n"; 
	 
		// preparing attachments
		for($i=0;$i<count($files);$i++){
			if(is_file($files[$i])){
				$message .= "--{$mime_boundary}\n";
				$fp =    @fopen($files[$i],"rb");
			$data =    @fread($fp,filesize($files[$i]));
						@fclose($fp);
				$data = chunk_split(base64_encode($data));
				$message .= "Content-Type: application/octet-stream; name=\"".basename($files[$i])."\"\n" . 
				"Content-Description: ".basename($files[$i])."\n" .
				"Content-Disposition: attachment;\n" . " filename=\"".basename($files[$i])."\"; size=".filesize($files[$i]).";\n" . 
				"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
				}
			}
		$message .= "--{$mime_boundary}--";
		$returnpath = "-f" . $sendermail;
		
		@mail($to, $subject, $message, $headers, $returnpath);	    
	}

Leave a Reply

Your email address will not be published. Required fields are marked *