Php发送邮件错误并上传错误 [英] Php send mail errors and upload error

查看:76
本文介绍了Php发送邮件错误并上传错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个工作正常的表单,但我有两个问题:



1.我收到错误消息:

警告:mail(/var/log/phpmail.log):无法打开流:第115行/www1/xxx/Contact_Us.php中的权限被拒绝。



2.我无法上传超过20KB的文件,即使在我的代码中,我允许直到2 MB,而php.ini upload_max_filesize为8MB,post_max_size为8MB。



所以,任何人都可以帮助我解决这两个问题,因为我真的被困在那里,不知道该做什么,它是我面临的最后一个问题,我无法解决它。

提前致谢;



我的尝试:



 <?php  
// set tiem zone
date_default_timezone_set(' 亚洲/迪拜');
// 显示错误
error_reporting(E_ALL);
ini_set(' display_errors',1);

// 定义错误变量
$ fnameErr = $ lnameErr = $ emailErr = $ humanErr = $ fileErr = $ result = $ result2 = ;

// 当我们按提交时,请执行以下操作
if (isset($ _ POST [' submit']))
{
< span class =code-comment> //
定义联系表单变量
$ fname = $ _POST [' fname'];
$ lname = $ _POST [' lname ];
$ email = $ _POST [' email ];
$ tel = $ _POST [' tel ];
$ design = $ _POST [' design ];
$ country = $ _POST [' 国家/地区];
$ comment = $ _POST [' comment ];
$ human = $ _POST [' human ];

// define检查变量
$ check1 = $ check2 = $ check3 = $ check4 = ;


// 让我们做一些检查
// 检查名字
if(空($ _ POST [ fname])){
$ fnameErr = 名称必填;
} else {
$ fname = test_input($ _ POST [ FNAME]);
// 检查名称是否只包含字母和空格
if (!preg_match( / ^ [a-zA-Z] * $ /,$ fname)){
$ fnameErr = < span class =code-string>只允许字母和空格
;
} else {
$ check1 = 1 ;
}
}
// 检查姓氏
if(空($ _ POST [ lname])){
$ lnameErr = 名称是必需的;
} else {
$ lname = test_input($ _ POST [ L-NAME]);
// 检查名称是否只包含字母和空格
if (!preg_match( / ^ [a-zA-Z] * $ /,$ lname)){
$ lnameErr = < span class =code-string>只允许字母和空格
;
} else {
$ check2 = 1 ;
}
}
// 检查电子邮件地址
if(空($ _ POST [ email])){
$ emailErr = 需要电子邮件;
} else {
$ email = test_input($ _ POST [ 电子邮件]);
// 检查电子邮件地址是否格式正确
if (!filter_var($ email,FILTER_VALIDATE_EMAIL)){
$ emailErr = 无效的电子邮件格式;
} else {
$ check3 = 1 ;
}
}
// 检查反垃圾邮件问题
if(空($ _ POST [ human])){
$ humanErr = 请输入答案;
}其他{
如果($ human!= 4){
$ humanErr = 请检查您的答案;
} else {
$ check4 = 1 ;
}
}

// 检查附件
if(isset($ _ FILES)&&(bool)$ _FILES){

$ allowedExtensions = array( pdf doc docx);

$ files = array();
foreach($ _ FILES as $ name => $ file){
$ file_name = $ file [' name'];
$ temp_name = $ file [' tmp_name ];
$ file_type = $ file [' type ];
$ file_size = $ file [' size ];
$ path_parts = pathinfo($ file_name);
$ ext = $ path_parts [' 扩展名];

if($ file_size> 2000000){
$ fileErr = 允许的最大大小为2 MB;
} else {
if(!in_array($ ext,$ allowedExtensions)){
$ fileErr = 文件$ file_name的扩展名为$ ext,不允许;
} else {
array_push($ files,$ file);}

}
}


// 定义电子邮件变量
$ to = ' email@domain.com';
$ from = 工作申请< /跨度>;
$ subject = ' Job Application'< /跨度>;
$ message = ' From:'.$fname。$ lname。 \\\\ n
' 电子邮件:'。$ email。 \\\\ n
' Telephone:'。$ tel。 \\\\ n
' 名称:'。$ design。 \\\\ n
' 国家/地区Appled From:'。$ country。 \\\\ n
' 消息:'。$ comment。 \\\\ nn \r\\\
;
$ headers = From:$ from ;
// boundary
$ semi_rand = md5(time());
$ mime_boundary = == Multipart_Boundary_x { $ semi_rand} X;

// 附件标题
$ headers 。= \ nMIME-Version:1.0 \ n Content-Type:multipart / mixed; \ n boundary = \{$ mime_boundary} \;

// multipart boundary
$ message = 这是MIME格式的多部分消息。\\ n \ n - {$ mime_boundary} \ n Content-Type:text / plain; charset = \iso-8859-1 \\ n Content-Transfer-Encoding:7bit \ n \ n $ message \ n \ n;
$ message 。= - { $ mime_boundary} \\\
;

// 准备附件

for( $ x = 0; $ x< count($ files); $ x ++){
$ file = fopen($ files [$ x] [' tmp_name'], RB);
$ data = fread($ file,filesize($ files [$ x] [' tmp_name']));
fclose($ file);
$ data = chunk_split(base64_encode($ data));
$ name = $ files [$ x] [' 名称];
$ message 。= Content-Type :{\application / octet-stream \}; \ n name = \$ name \\ n
Content-Disposition:attachment; \ n filename = \$ name \\ n
Content-Transfer-Encoding:base64 \ n \ n $ data \ n \ n;
$ message 。= - { $ mime_boundary} \\\
;
}


// 如果所有支票都是,则通过电子邮件发送内容正确
if($ check1&& $ check2 && $ check3 && $ check4 == 1){
mail($ to,$ subject,$ message,$ headers);
$ result = 消息已成功发送 ;
} else {
$ result2 = 无法发送消息;
}
}}
函数test_input($ data){
$ data = trim($ data);
$ data = stripslashes($ data);
$ data = htmlspecialchars($ data);
return $ data ;
}

?>
<! - PHP代码结束 - >

解决方案

fnameErr =

lnameErr =


emailErr =

i have created a form its working fine but i have two problems:

1. i am receiving error message :

Warning: mail(/var/log/phpmail.log): failed to open stream: Permission denied in /www1/xxx/Contact_Us.php on line 115.


2. i can't upload files more than 20KB even in my code i allow till 2 MB and on the php.ini upload_max_filesize is 8MB and post_max_size is 8MB.

So please can anyone help me with this two problems because i am really stuck there and don't know what to do and its the last problem i face and i can't solve it.
Thanks in advance;

What I have tried:

<?php
//set tiem zone
date_default_timezone_set('Asia/Dubai');
// Show errors
error_reporting(E_ALL);
ini_set('display_errors', 1);
	
// define Errors variables
$fnameErr = $lnameErr = $emailErr = $humanErr = $fileErr = $result = $result2 =  "" ;
	
// when we press submit do the following
if(isset($_POST['submit']))
{
// define contact form variables
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['email'];
$tel = $_POST['tel'];
$design = $_POST['design'];
$country = $_POST['country'];
$comment = $_POST['comment'];
$human = $_POST['human'];

// define Checks variables
$check1 = $check2 = $check3  = $check4  = "";

	
// Let's do some checks	
// Checking the First Name
if(empty($_POST["fname"])){
	$fnameErr = "Name is Required";
}else{
	$fname = test_input($_POST["fname"]);
	// check if name only contains letters and whitespace
    if (!preg_match("/^[a-zA-Z ]*$/",$fname)) {
      $fnameErr = "Only letters and white space allowed"; 
    }else{
		$check1 = 1;
	}
}
// Checking the Last Name	
if(empty($_POST["lname"])){
	$lnameErr = "Name is Required";
}else{
	$lname = test_input($_POST["lname"]);
	// check if name only contains letters and whitespace
    if (!preg_match("/^[a-zA-Z ]*$/",$lname)) {
      $lnameErr = "Only letters and white space allowed"; 
    }else{
		$check2 = 1;
	}
}
//Checking the Email Adress
if(empty($_POST["email"])){
	$emailErr = "Email is Required";
}else{
	$email = test_input($_POST["email"]);
    // check if e-mail address is well-formed
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
      $emailErr = "Invalid email format"; 
    }else{
		$check3 = 1;
	}
}
//Checking the Anti-Spam Question
if(empty($_POST["human"])){
	$humanErr = "Please Enter the Answer";
}else{
	if ($human != 4){
		$humanErr = "Please check your answer";
	}else{
		$check4 = 1;
	}
}
	
// checking the attachment
if(isset($_FILES) && (bool) $_FILES) {
  
	$allowedExtensions = array("pdf","doc","docx");
	
	$files = array();
	foreach($_FILES as $name=>$file) {
		$file_name = $file['name']; 
		$temp_name = $file['tmp_name'];
		$file_type = $file['type'];
		$file_size = $file['size'];
		$path_parts = pathinfo($file_name);
		$ext = $path_parts['extension'];

		if($file_size > 2000000){
        $fileErr = "Max allowed size is 2 MB";
        } else {
				if(!in_array($ext,$allowedExtensions)) {
				$fileErr = "File $file_name has the extensions $ext which is not allowed";    
				}else{
					array_push($files,$file);}
	
			   }
	}

	
// define email variables
$to = 'email@domain.com';
$from = "Job Application"; 
$subject = 'Job Application';
$message = 'From: '.$fname .$lname."\r\n".
           'E-mail: '.$email."\r\n".
           'Telephone: '.$tel."\r\n".
           'Designation: '.$design."\r\n".
           'Country Appled From: '.$country."\r\n".
           'Message: '.$comment."\r\n"."\r\n";
$headers = "From: $from";	
// 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 = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n"; 
	$message .= "--{$mime_boundary}\n";
	 
	// preparing attachments
	
	for($x=0;$x<count($files);$x++){
		$file = fopen($files[$x]['tmp_name'],"rb");
		$data = fread($file,filesize($files[$x]['tmp_name']));
		fclose($file);
		$data = chunk_split(base64_encode($data));
		$name = $files[$x]['name'];
		$message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$name\"\n" . 
		"Content-Disposition: attachment;\n" . " filename=\"$name\"\n" . 
		"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
		$message .= "--{$mime_boundary}\n";
	}	
	 
	
// Emailing the Contents if all Checks are correct	
if($check1 && $check2 && $check3 && $check4  == 1){
	mail($to, $subject, $message, $headers);
	$result =  "Message Sent Sucessfully";
}else{
	$result2 = "Message Can't be sent";
}
} }
 function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}
	
?>
<!-- END OF PHP CODE -->

解决方案

fnameErr =


lnameErr =


emailErr =


这篇关于Php发送邮件错误并上传错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆