使用邮件php函数发送附件

时间:2014-01-27 05:25:39

标签: php

你好这是发送文本和附件的代码它只发送文本而不是附件我尽我所能,但我没有找到它。任何人都可以访问我的代码并对其进行编辑,然后将其发布给我,这将是非常好的。

帮帮我吧。

<?php

if(isset($_POST['propertystatus'])) 

{

    $email_to = "contact@scale-property.com";
    $email_subject = "New Property From Customer";
    $email_subject_to_user = "Copy of your email";
    $propery_Information = 'PROPERTY INFORMATION';
    $propery_Location = 'PROPERTY LOCATION';
    $contact_Information = 'CONTACT INFORMATION';


        // Allowed file types. add file extensions WITHOUT the dot.
    $allowtypes=array("zip", "rar", "doc", "pdf");

        // Require a file to be attached: false = Do not allow attachments true = allow only 1 file to be attached
    $requirefile="true";

    // Maximum file size for attachments in KB NOT Bytes for simplicity. MAKE SURE your php.ini can handel it,
    // post_max_size, upload_max_filesize, file_uploads, max_execution_time!
    // 2048kb = 2MB,       1024kb = 1MB,     512kb = 1/2MB etc..
    $max_file_size="1024";

    // Thank you message
    $thanksmessage="Your attachments has also been sent successfully.";

    $errors = array(); //Initialize error array

    $error_message = "";



    function died($error) 
    {

        // your error code can go here

        echo "We are very sorry, but there were errors found with the form you submitted.<br>";

        echo $error."<br />";

        die();

    }  


    // checks for required file
    // http://amiworks.co.in/talk/handling-file-uploads-in-php/
    if($requirefile=="true") {
        if($_FILES['attachment']['error']==4) {
            $errors[]='You forgot to attach a file';
        }
    }


        //checks attachment file
    // checks that we have a file
    if((!empty($_FILES["attachment"])) && ($_FILES['attachment']['error'] == 0)) {
            // basename -- Returns filename component of path
            $filename = basename($_FILES['attachment']['name']);
            $ext = substr($filename, strrpos($filename, '.') + 1);
            $filesize=$_FILES['attachment']['size'];
            $max_bytes=$max_file_size*1024;

            //Check if the file type uploaded is a valid file type. 
            if (!in_array($ext, $allowtypes)) {
                $errors[]="Invalid extension for your file: <strong>".$filename."</strong>";

        // check the size of each file
        } elseif($filesize > $max_bytes) {
                $errors[]= "Your file: <strong>".$filename."</strong> is to big. Max file size is ".$max_file_size."kb.";
            }

    }


        // if !empty FILES

    if (empty($errors))
     { //If everything is OK

        // send an email
        // Obtain file upload vars
        $fileatt      = $_FILES['attachment']['tmp_name'];
        $fileatt_type = $_FILES['attachment']['type'];
        $fileatt_name = $_FILES['attachment']['name'];


        // create a boundary string. It must be unique
          $semi_rand = md5(time());
          $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";


       // Add the headers for a file attachment
          $headers .= "\nMIME-Version: 1.0\n" .
                      "Content-Type: multipart/mixed;\n".
                      " boundary=\"{$mime_boundary}\"";

          // Add a multipart boundary above the plain message
          $email_message .="This is a multi-part message in MIME format.\n\n";
          $email_message .="--{$mime_boundary}\n";
          $email_message .="Content-Type: text/plain; charset=\"iso-8859-1\"\n";
          $email_message .="Content-Transfer-Encoding: 7bit\n\n";
          $email_message .="From: ".$namefrom."\n\n";

        if (is_uploaded_file($fileatt)) {
          // Read the file to be attached ('rb' = read binary)
          $file = fopen($fileatt,'rb');
          $data = fread($file,filesize($fileatt));
          fclose($file);

          // Base64 encode the file data
          $data = chunk_split(base64_encode($data));

          // Add file attachment to the message
          $email_message .= "--{$mime_boundary}\n" .
                      "Content-Type: {$fileatt_type};\n" .
                      " name=\"{$fileatt_name}\"\n" .
                      //"Content-Disposition: attachment;\n" .
                      //" filename=\"{$fileatt_name}\"\n" .
                      "Content-Transfer-Encoding: base64\n\n" .
                      $data . "\n\n" .
                      "--{$mime_boundary}--\n";
        }

        $envs = array("HTTP_USER_AGENT", "REMOTE_ADDR", "REMOTE_HOST");
        foreach ($envs as $env)
        $email_message .= "$env: $_SERVER[$env]\n";

    } //end of if(empty($errors))


    // validation expected data exists

    if(!isset($_POST['propertystatus']) ||

        !isset($_POST['currencytype']) ||

        !isset($_POST['currency']) ||

        !isset($_POST['rooms']) ||

        !isset($_POST['bathroom']) ||

        !isset($_POST['kitchen']) ||

        !isset($_POST['yard']) ||

        !isset($_POST['housedesproperty']) ||

        !isset($_POST['drop_1']) ||

        !isset($_POST['drop_2']) ||

        !isset($_POST['drop_3']) ||

        !isset($_POST['region']) ||

        !isset($_POST['street']) ||

        !isset($_POST['name']) ||

        !isset($_POST['emailadd']) ||

        !isset($_POST['conemailadd']) ||

        !isset($_POST['phone1']) ||

        !isset($_POST['phone2']))

    {

        died('You have not properly selected the fields.');      

    }   

    $property_status = $_POST['propertystatus']; // required
    $currency_type = $_POST['currencytype']; // required
    $currency = $_POST['currency']; // required
    $rooms = $_POST['rooms']; // required
    $bathroom = $_POST['bathroom']; // required
    $kitchen = $_POST['kitchen']; // required
    $yard = $_POST['yard']; // required
    $housedesproperty = $_POST['housedesproperty']; // required
    $drop_1 = $_POST['drop_1']; // required
    $drop_2 = $_POST['drop_2']; // required
    $drop_3 = $_POST['drop_3']; // required
    $region = $_POST['region']; // required
    $street = $_POST['street']; // required
    $name = $_POST['name']; // required
    $emailadd = $_POST['emailadd']; // required
    $conemailadd = $_POST['conemailadd']; // required
    $phone1 = $_POST['phone1']; // required
    $phone2 = $_POST['phone2']; // required

    $email_message = "Property details below.\n\n";

    function clean_string($string) 
    {

      $bad = array("content-type","bcc:","to:","cc:","href");

      return str_replace($bad,"",$string);

    }

    $email_message .= "".clean_string($propery_Information)."\n";
    $email_message .= "--------------------------------------------"."\n";
    $email_message .= "Property Status: ".clean_string($property_status)."\n";
    $email_message .= "Total Price: ".clean_string($currency_type)." ".(number_format($currency, 2, '.', ','))."\n";
    $email_message .= "Rooms: ".clean_string($rooms)."\n";
    $email_message .= "Bathrooms: ".clean_string($bathroom)."\n";
    $email_message .= "Bathrooms: ".clean_string($kitchen)."\n";
    $email_message .= "Bathrooms: ".clean_string($yard)."\n";
    $email_message .= "Description: ".clean_string($housedesproperty)."\n\n";
    $email_message .= "".clean_string($propery_Location)."\n";
    $email_message .= "--------------------------------------------"."\n";
    $email_message .= "Province: ".clean_string($drop_1)."\n";
    $email_message .= "District: ".clean_string($drop_2)."\n";
    $email_message .= "PD(Nahya): ".clean_string($drop_3)."\n";
    $email_message .= "Bathrooms: ".clean_string($region)."\n";
    $email_message .= "Street: ".clean_string($street)."\n\n";
    $email_message .= "".clean_string($contact_Information)."\n";
    $email_message .= "--------------------------------------------"."\n";
    $email_message .= "Name: ".clean_string($name)."\n";
    $email_message .= "Email ID: ".clean_string($emailadd)."\n";
    $email_message .= "Con-Email ID: ".clean_string($conemailadd)."\n";
    $email_message .= "Phone(1): ".clean_string($phone1)."\n";
    $email_message .= "Phone(2): ".clean_string($phone2)."\n";

// create email headers

$headers = 'From: '.$emailadd."\r\n".


'Reply-To: '.$emailadd."\r\n" .
'X-Mailer: PHP/' . phpversion();

@mail($email_to, $email_subject, $email_message, $headers); 
@mail($emailadd, $email_subject_to_user, $email_message, "From: $email_to");

?> 
Your Property has been Posted please check your email address.
<?php

}

?>

1 个答案:

答案 0 :(得分:0)

我建议使用PHPMailer。它是一个PHP类,可以轻松处理高级邮件功能,包括发送附件。