1

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="m.khaleghi@mitoconnect.com"; 
$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 ?

Martin
  • 22,212
  • 11
  • 70
  • 132
Rayan
  • 23
  • 4
  • The pending edit http://stackoverflow.com/review/suggested-edits/15121601 is wanting to add `
    ` tags; rejected.
    – Funk Forty Niner Feb 06 '17 at 18:00
  • Use PHPMailer, it makes this pile of scruffy mess look somewhat decent and much more readable, as well as much easier to deliver to intended recipients. – Martin Feb 06 '17 at 18:15

3 Answers3

1

"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:

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
1

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.

Synchro
  • 35,538
  • 15
  • 81
  • 104
  • thanks everyone , I've changed the type to text/plain , and it worked . I appreciate , Good Luck – Rayan Feb 06 '17 at 18:22
  • @M.Bourne check one of these answers and tick the tick next to it to set it as the answer. `:-)` – Martin Feb 06 '17 at 18:23
0

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

Martin
  • 22,212
  • 11
  • 70
  • 132
affaz
  • 1,191
  • 9
  • 23