Rayan
Rayan

Reputation: 23

Send mail with attachment in PHP

Here is my codes for sending an email with attachment :

if (isset($_POST['submit'])) {

@$name=stripslashes($_POST['name']);
@$last_name=stripslashes($_POST['last_name']);
@$phone=stripslashes($_POST['phone']);
@$address=stripslashes($_POST['address']);
@$email=stripslashes($_POST['email']);
@$age=stripslashes($_POST['age']);
@$education=stripslashes($_POST['education']);
@$position=stripslashes($_POST['position']);
@$s_date=stripslashes($_POST['s_date']);
@$message=stripslashes($_POST['message']);





@$attachment=$_FILES['attachment']['name'];
$to="[email protected]"; 
$subject="Job Application";
$max_file_size = 1000; 
$formats = array('pdf','docx'); 


if ( empty($name)  ){   

        sw_alert('warning','Please Fill All Fields'); 

}

$hash = md5(uniqid(time()));
$header = "";  

$header .= "MIME-Version: 1.0\n";  
$header .= "Content-Type: multipart/mixed; boundary=\"".$hash."\"\n\n";  
$header .= "This is a multi-part message in MIME format.\n";  

$header .= "--".$hash."\n";  
$header .= "Content-type: text/html; charset=utf-8\n";  
$header .= "Content-Transfer-Encoding: 7bit\n\n";  


$header .= "$name.\n"; 
$header .= "$last_name.\n";
$header .= "$phone.\n"; 
$header .= "$address.\n";
$header .= "$email.\n"; 
$header .= "$age.\n";
$header .= "$education.\n";
$header .= "$position.\n"; 
$header .= "$s_date.\n"; 
$header .= "$message.\n"; 



if (!empty ($attachment)) { 
$tmp_name = $_FILES['attachment']['tmp_name'];
$type = $_FILES['attachment']['type'];
$file_name = $_FILES['attachment']['name'];
$file_size = $_FILES['attachment']['size']/1024;

if ($file_size>$max_file_size) {


sw_alert('warning','File size is too large'); 
die("<meta http-equiv=\"content-Type\" content=\"text/html; charset=utf-8\">
<meta http-equiv='refresh' content='2;url=".$_SERVER['HTTP_REFERER']."' />
 ");

   }


@$ext = end(explode('.',$file_name));
if(!in_array($ext,$formats)){


sw_alert('warning','File type is not allowed'); 

die("<meta http-equiv=\"content-Type\" content=\"text/html; charset=utf-8\">
<meta http-equiv='refresh' content='2;url=".$_SERVER['HTTP_REFERER']."' />
 ");

}


$content = chunk_split(base64_encode(file_get_contents($tmp_name)));  
$header .= "--".$hash."\n";  
$header .= "Content-Type: application/octet-stream; 
name=\"".$file_name."\"\n";  
$header .= "Content-Transfer-Encoding: base64\n";  
$header .= "Content-Disposition: attachment; 
filename=\"".$file_name."\"\n\n";  
$header .= $content."\n\n";  
}  

$sendemail = @mail($to,$subject,null,$header); 

if($sendemail)  
{  
sw_alert('success','Your application has been sent'); 
}  
else  
{  
sw_alert('warning','Something Wrong , Please try again'); 
}  


}

all set and work good , but I have a problem on the other side ! when I receive the email , all information shown in 1 row :

name last_name phone address email age education position start_date message

but I want them to be like this :

name
last_name
phone
address
email
age
education
position
start_date
message

I have tried different ways of using "\n" and "\r\n" but it ain't worked . so anyone has a solution ?

Upvotes: 1

Views: 207

Answers (3)

affaz
affaz

Reputation: 1191

Use <br> instead of \n as your content type is text/html

Upvotes: 0

Funk Forty Niner
Funk Forty Niner

Reputation: 74216

"I have tried different ways of using "\n" and "\r\n" but it ain't worked . so anyone has a solution ?"

That's because you're wanting to send your email as HTML.

$header .= "Content-type: text/html; charset=utf-8\n";

You need to use <br>'s for new lines.

Sidenote:

I noticed your use of $_SERVER['HTTP_REFERER']. That isn't always reliable.

Read the following on the subject:

Upvotes: 1

Synchro
Synchro

Reputation: 37700

HTML collapses white space (i.e. line breaks), but you can just change to a plain-text message (which preserves whitespace), like this:

$header .= "Content-type: text/plain; charset=utf-8\n";  

Obviously any HTML tags you put in that format will not be rendered as such.

Incidentally your code may be vulnerable to header injection attacks, and you're not handling uploads safely. Refer to the PHP docs on handling file uploads safely, or use a library that takes case of all that for you, such as PHPMailer that you tagged this question with.

Upvotes: 1

Related Questions