使用PHP发送带附件的邮件

时间:2017-02-06 17:55:03

标签: php html email email-attachments

以下是我发送带附件的电子邮件的代码:

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'); 
}  


}

所有设置和工作都很好,但我在另一边有问题!当我收到电子邮件时,所有信息都显示在第1行:

name last_name phone address email age education position start_date message

但我希望他们是这样的:

姓名
last_name
电话
地址
电子邮件
年龄
教育 位置
start_date
留言

我尝试了不同的使用方式&#34; \ n&#34;和&#34; \ r \ n&#34;但它还没有奏效。所以任何人都有解决方案吗?

3 个答案:

答案 0 :(得分:2)

&#34;我尝试了不同的使用方式&#34; \ n&#34;和&#34; \ r \ n&#34;但它还没有奏效。所以任何人都有解决方案?&#34;

那是因为您希望以HTML格式发送电子邮件。

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

您需要将<br>用于新行。

旁注:

我注意到您使用了$_SERVER['HTTP_REFERER']。这并不总是可靠的。

阅读以下主题:

答案 1 :(得分:1)

HTML折叠空格(即换行符),但您只需更改为纯文本消息(保留空格),如下所示:

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

显然,您以该格式放置的任何HTML标签都不会这样呈现。

顺便提一下,您的代码可能容易受到标头注入攻击,并且您没有安全地处理上传。请参阅有关安全处理文件上传的PHP文档,或使用一个包含所有这些内容的库,例如您用此标记此问题的PHPMailer

答案 2 :(得分:0)

使用<br>代替\n,因为您的内容类型为text / html