PHP - 成功上传文件后如何发送电子邮件?

时间:2015-06-23 09:41:32

标签: php email file-upload

如果文件上传成功,我需要PHP发送电子邮件。我的代码工作得很好:

<?php

  $target_dir = "gallery-sys/";
  $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
  $uploadOk = 1;
  $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

  if (file_exists($target_file)) {
    echo "File already exists.";
    $uploadOk = 0;
  }

  if ($uploadOk == 0) {
    echo "Error - file was not uploaded.";
  } else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
       echo "File". basename( $_FILES["fileToUpload"]["name"]). " was uploaded successfully.";
    }
  }

?>

但我需要电子邮件功能。如果我添加这6行,它甚至不能工作了:

<?php

  $target_dir = "gallery-sys/";
  $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
  $uploadOk = 1;
  $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

  if (file_exists($target_file)) {
    echo "File already exists.";
    $uploadOk = 0;
  }

  if ($uploadOk == 0) {
    echo "Error - file was not uploaded.";
  } else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
      echo "File". basename( $_FILES["fileToUpload"]["name"]). " was uploaded successfully.";

      $to = "szabo@atria.sk";
      $url = $_POST['current_url'];
      $subject = "New image was uploaded";
      $message = "URL:" . $url ;
      $headers = "Gallery" "\r\n" . 'Content-Type: text/plain; charset=UTF-8';
      mail($to,$subject,$message,$headers);
    }
  }

?>

我的代码中是否有错误?

2 个答案:

答案 0 :(得分:1)

“画廊”之后缺少一个点:

$headers = "Gallery" . "\r\n" . 'Content-Type: text/plain; charset=UTF-8';

答案 1 :(得分:1)

为了更加完整:

您在两个字符串之间缺少所谓的String Operator('.'):

$headers = "Gallery" "\r\n" . 'Content-Type: text/plain; charset=UTF-8';

应该是

$headers = "Gallery" . "\r\n" . 'Content-Type: text/plain; charset=UTF-8';

或者

$headers = "Gallery\r\n" . 'Content-Type: text/plain; charset=UTF-8';