Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
邮件附件PHP错误_Php_Email_File Upload_Smtp_Email Attachments - Fatal编程技术网

邮件附件PHP错误

邮件附件PHP错误,php,email,file-upload,smtp,email-attachments,Php,Email,File Upload,Smtp,Email Attachments,我在构建一个PHP表单时遇到了很多麻烦,该表单将发送一个包含表单其余信息的电子邮件附件。我不知道我做错了什么。每次我提交表格时,它都无法发送。请帮忙。多谢各位 <?php // hide all basic notices from PHP error_reporting(E_ALL); ini_set('display_errors', 1); if(isset($_FILES) && (bool) $_FILES) { $allowedExtensio

我在构建一个PHP表单时遇到了很多麻烦,该表单将发送一个包含表单其余信息的电子邮件附件。我不知道我做错了什么。每次我提交表格时,它都无法发送。请帮忙。多谢各位

<?php

// hide all basic notices from PHP
 error_reporting(E_ALL);
 ini_set('display_errors', 1);

if(isset($_FILES) && (bool) $_FILES) {

    $allowedExtensions = array("pdf","doc","docx","gif","jpeg","jpg","png","rtf","txt");

    $files = array();
    foreach($_FILES as $name=>$file) {
        $file_name = $file['name']; 
        $temp_name = $file['tmp_name'];
        $file_type = $file['type'];
        $path_parts = pathinfo($file_name);
        $ext = $path_parts['extension'];
        if(!in_array($ext,$allowedExtensions)) {
            die("File $file_name has the extensions $ext which is not allowed");
        }
        array_push($files,$file);
    }
    $date = $_POST['date'];
    $namefirst = $_POST['first-name'];
    $namelast = $_POST['last-name'];
    $address1 = $_POST['street-address'];
    $address2 = $_POST['street-address-line-2'];
    $city = $_POST['city'];
    $state = $_POST['state'];
    $zip = $_POST['zip'];
    $country = $_POST['country'];
    $phone = $_POST['phone'];
    $message = $_POST['message'];

        // 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 
    $message1 = "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" . $message1 . "\n\n"; 
    $message1 .= "--{$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'];
        $message1 .= "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";
        $message1 .= "--{$mime_boundary}\n";
    }

    if(!isset($hasError)) {

        $emailTo = 'XXX';
        $subject = 'New Submitted Message From: ' . $name;
        $body = "Name: $namefirst $namelast \n\nDOB: $date \n\nAddress: $address1 \n\nAddress2: $address2 \n\nCity: $city \n\nState: $state \n\nZip Code: $zip \n\nCountry: $country \n\nPhone: $phone \n\nMessage: $messsge";
        $headers = 'From: ' .' <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;

        mail($emailTo, $subject, $body, $headers);

        $message = 'Thank you ' . $name . ', your message has been submitted.';
        $result = true;

    } else {

        $arrMessage = array( $nameError, $emailError, $messageError );

        foreach ($arrMessage as $key => $value) {
            if( !isset($value) )
                unset($arrMessage[$key]);
        }

        $message = implode( '<br/>', $arrMessage );
        $result = false;
    }

    header("Content-type: application/json");
    echo json_encode( array( 'message' => $message, 'result' => $result ));
    die();
}


?>


停止滚动您自己的-开始使用phpmailer或swiftmailer由于您已打开错误报告功能,它是否会打印错误?此外,php邮件命令不是100%有效(ISP在这方面进行了大量垃圾邮件过滤)-最好使用SMTP设置。说真的,不要使用自己的设置。此脚本中存在基本错误和标头注入漏洞。我不会发布修复程序,因为这是浪费时间-使用库。