<?php

require('class.phpmailer.php');

$mail=new PHPMailer();
$mail->isSMTP();
$mail->SMTPDebug=1;
$mail->SMTPAuth=true;
$mail->Port=465;
$mail->Host="ssl//smtp.gmail.com";
$mail->Host = "ssl://smtp.gmail.com";
$i=0;
if(isset($_POST['submit']))

{	
	$mail->Username=check($_POST['UserName'],"user id  required");
	if(filter_var($_POST['UserName'],FILTER_VALIDATE_EMAIL)==false)
	{
	show_error("Invalid email address");
	}
	$mail->Username=$_POST['UserName'];

	$mail->Password=check($_POST['pass'],"password required");
	$con=check($_POST['conpass'],"confirm password required");
	if($con!=$mail->Password)
	show_error("Passwords do not match");
	$mail->FromName=check($_POST['name']);
	$_POST['rec']=check($_POST['rec'],"recipient required");
	if(filter_var($_POST['rec'],FILTER_VALIDATE_EMAIL)==false)
	{
	show_error("Invalid email address");
	}

	$_POST['recn']=check($_POST['recn']);
	$mail->AddAddress($_POST['rec'],$_POST['recn']);

	$mail->Subject=$_POST['sub'];
	$mail->Body =$_POST['body'];$i=0;
	if ($_FILES["fname"]["error"]>0)
  {
		ECHO $_FILES["fname"]["error"];
  }
  else
  {
	echo "<BR>UPLOAD SIZE OF FILE<br>".$_FILES["fname"]["size"]."bits";
	echo "<BR>UPLOAD NAME OF FILE<br>".$_FILES["fname"]["name"];
	echo "<BR>UPLOAD TYPE OF FILE<br>".$_FILES["fname"]["type"];
	//upper case generates error
	echo "<BR>UPLOAD location OF FILE<br>".$_FILES["fname"]["tmp_name"];
	// $fname=fopen($_FILES["fname"]["name"],"r") or exit("bye");
	// while(!feof($fname))
	// {
		// echo fPOSTs($fname);
	// }
	
	$mail->IsHTML(false);
//mail->AddAttachment($_FILES['fname']['tmp_name'],$_FILES['fname']['name']);
//mail->AddAttachment($_FILES['fname']['tmp_name'],$_FILES['fname']['name']);

	$mail->AddAttachment('file:///H:/ha.txt','a.txt');
  }
  
	
	
	while($i<3)
{		if ($mail->Send() == true) {
			echo 'The message has been sent at '. time();
			}	
			else {
			echo 'The email message has NOT been sent for some reason. Please try again later.';
			echo 'Mailer error: ' . $mail->ErrorInfo;
		
				}	$i++;
}
}

else
{
echo "<html>
<body bgcolor=#ECE5B6>
<center>
<h1>GMAIL PAGE</h1> 
<form action='email4.php' method='POST' >
<table bgcolor=#ECD672>
<h2>
<tr><td style ='background-color :#ECD672'>Sender Email Address*: <td><input type='text' style ='background-color :#ECD672'name='UserName' /><br>
<tr><td style ='background-color :#ECD672'>Password*:<td> <input style ='background-color :#ECD672'type='password' name='pass' /><br>
<tr><td style ='background-color :#ECD672'>Confirm Password*: <td><input style ='background-color :#ECD672'type='password' name='conpass' /><br>
<tr><td>Name: <td><input type='text' name='name' /><br>
<tr><td style ='background-color :#ECD672'>ReceiverAddress*:<td> <input style ='background-color :#ECD672' type='text' name='rec' /><br>
<tr><td>ReceiverName:<td> <input type='text' name='recn' /><br>
<tr><td>Subject: <td><input type='text' name='sub' /><br>
<tr ><td valign='top' style='height:200 px'>Body: <td style='height:200 px'><input type='text' name='body' style='height:200 px'><br></h2>
</table>
<input type='file' name='fname' id='file' />
<input type='hidden' name='submit' value='1' /><br>
<input type='submit' value='SEND EMAIL'/><br>
</center>
</form>

</body>
</html> 
";
}




function check($d,$prob='')
{
	$d=trim($d);
	$d=stripslashes($d);
	$d=htmlspecialchars($d);
	if($prob &&strlen($d)==0)
	{
		show_error($prob);
	}
	return $d;
}
function show_error($myError)
{

   echo " <html>
    <body>

    <b><h1><center>Please correct the following error:</b><br />";
  echo $myError;

echo "</center></h1>
    </body>
    </html>";

exit();


}
// function checke($d,$prob='')
// {
	// $d=trim($d);
	// $d=stripslashes($d);
	// $d=htmlspecialchars($d);
	// if(!preg_match($d,"^(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$"))
	// {
		// show_error($prob);
	// }
	// return $d;
// }

?>

I am trying to send attachments to the receiver.
Although,it doesn't show any error ,I am not receiving the attachment.

When you upload a file you need to move it from temporary directory to a directory of the server, so you can store the file, use move_uploaded_file(): http://php.net/manual/en/function.move-uploaded-file.php
And then change this line, it seems wrong:

$mail->AddAttachment('file:///H:/ha.txt','a.txt');

with:

$mail->AddAttachment('h:/server_directory/files/ha.txt','a.txt');

When you upload a file you need to move it from temporary directory to a directory of the server, so you can store the file, use move_uploaded_file(): http://php.net/manual/en/function.move-uploaded-file.php
And then change this line, it seems wrong:

$mail->AddAttachment('file:///H:/ha.txt','a.txt');

with:

$mail->AddAttachment('h:/server_directory/files/ha.txt','a.txt');

Thanks this worked.
But what if I want to accept the file in the form itself by the user?
As in upload it

It is always the same, just make sure PHP can't be executed in destination directory and that is not directly accessible to web, so no one can upload a script and use it to hack your server. You can do that by placing a .htaccess file in the directory that will store files and writing just this:

order deny,allow
deny from all
php_flag engine off

Usually Windows doesn't allow filename starting with a dot, if you are uploading to a remote directory, just use ftp to rename the file. Bye :)

Note: you should also check the mime of the files and run an antivirus on the server to check the files submitted.

To who voted against my previous reply: explain me what I did wrong, at least I can learn ;D

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.