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

PHP - how to send e-mail after successful 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);
    }
  }

?>

我的代码有错误吗?

"Gallery" 后少了一个点:

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

更完整一点:

您在两个字符串之间缺少所谓的 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';