带有图像上传器的PHP电子邮件不发送

时间:2016-02-02 04:04:42

标签: php email

我正在构建一个发送电子邮件php文件,其中还包含一个文件上传器。代码整体工作,因为我没有收到任何错误,以及我在localhost中测试时没有遇到任何错误。包含文件上传功能后,电子邮件发送功能停止工作。

<?php

if ($_POST) {
    $name = $_POST['firstlastname'];
    // more items
    $accessories = $_POST['accessories'];

    // file upload //
    $name_of_uploaded_file = basename($_FILES['uploaded_file']['name']);
    $type_of_uploaded_file = substr($name_of_uploaded_file, strrpos($name_of_uploaded_file, '.') + 1);
    $size_of_uploaded_file = $_FILES["uploaded_file"]["size"]/1024;

    // settings
    $max_allowed_file_size = 3000;
    $allowed_extensions = array("jpg", "jpeg", "png", "pdf");

    if($size_of_uploaded_file > $max_allowed_file_size )
{
  $errors .= "\n Size of file should be less than $max_allowed_file_size";
}

//------ Validate the file extension -----
$allowed_ext = false;
for($i=0; $i<sizeof($allowed_extensions); $i++)
{
  if(strcasecmp($allowed_extensions[$i],$type_of_uploaded_file) == 0)
  {
    $allowed_ext = true;
  }
}

if(!$allowed_ext)
{
  $errors .= "\n The uploaded file is not supported file type. ".
  " Only the following file types are supported: ".implode(',',$allowed_extensions);
}

$upload_folder = "media/uploaded/";
$path_of_uploaded_file = $upload_folder . $name_of_uploaded_file;
$tmp_path = $_FILES["uploaded_file"]["tmp_name"];

if(is_uploaded_file($tmp_path))
{
  if(!copy($tmp_path,$path_of_uploaded_file))
  {
    $errors .= '\n error while copying the uploaded file';
  }
}
if (!empty($_FILES["uploaded_file"]["name"])) {
        if (move_uploaded_file($_FILES["uploaded_file"]["tmp_name"], $upload_folder.$_FILES["uploaded_file"]["name"]))
        $uploaded = $upload_folder.$_FILES["uploaded_file"]["name"];
}
}


    $empty = false;

            function IsInjected($str) {
            $injections = array('(\n+)',
                '(\r+)',
                '(\t+)',
                '(%0A+)',
                '(%0D+)',
                '(%08+)',
                '(%09+)'
            );
            $inject = join('|', $injections);
            $inject = "/$inject/i";
            if (preg_match($inject, $str)) {
                return true;
            } else {
                return false;
            }
        }

        if (IsInjected($guest_email)) {
          echo "";

          } else {
            $email_from = $name; //<== update the email address
            $email_subject = "You have a new message from $name";


            // message 
            $message = "<html><body>".
            <h3>Attachment</h3>".
            "<img src='".$uploaded."' alt='".$name_of_uploaded_file."'/>".
            "</body></html>";

            $to = "my email"; //<== enter personal email here

                    // headers
            $headers = "From: $email_from \r\n";
            $headers .= "Reply-To: $guest_email \r\n";
            $headers .= "BCC: $guest_email\r\n";
            $headers .= "MIME-Version: 1.0\r\n";
            $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
            //Send the email!
            mail($to, $email_subject, $message, $headers);

            // echo $message;

                        //done. redirect to thank-you page.
     header('Location: http://markjborg.com/TestArea/Bathrooms/quote.php?pageID=7'); 

        }

?>

0 个答案:

没有答案