【问题标题】:Mail() with attachment file doesn't work (PHP + HTML)带有附件文件的邮件()不起作用(PHP + HTML)
【发布时间】:2020-08-03 06:10:51
【问题描述】:

我正在尝试发送带有附件文件的电子邮件,但没有成功,我收不到任何文件。

语法上似乎是正确的,因为我在任何地方都没有任何邮件,问题是我没有收到任何邮件。

这是我的 HTML 代码:


 html>
  <head>
    
  </head>
  <body>
    <form action="hola.php" method="post">
      <input type="file" name="csv" id="csv" value="RUN"/><br/>
      <input type="submit" value="enviar">
    </form>
  <body>
</html>


这是我的 PHP 代码:


  $recipient_email = 'prueba@prueba.com';
  $subject = "Mensaje de prueba";
  $message = "Hola, esto es una prueba";

  if (isset($_FILES['csv'])) {
    $tmp_name = $_FILES['my_file']['tmp_name'];
    $name = $_FILES['my_file']['name'];
    $size = $_FILES['my_file']['size'];
    $type = $_FILES['my_file']['type'];
    $error = $_FILES['my_file']['error'];

    $total = count($_FILES['csv']['name']);

    for( $i=0 ; $i < $total ; $i++ ) {
      $tmpFilePath = $_FILES['csv']['tmp_name'][$i];

      $headers = "MIME-Version: 1.0\r\n";
      $headers .= "From:".$from_email."\r\n";
      $headers .= "Reply-To: ".$reply_to_email."\r\n";
      $headers .= "Content-Type: multipart/mixed;\r\n";
      $headers .= "From:" . "doc@gestio.aliance-brother.com";

      $mensaje .="Content-Type: $file_type; name=".$file_name."\r\n"; 
      $mensaje .="Content-Disposition: attachment; filename=".$file_name."\r\n"; 
      $mensaje .="Content-Transfer-Encoding: base64\r\n"; 
      $mensaje .="X-Attachment-Id: ".rand(1000, 99999)."\r\n\r\n";  
      $mensaje .= $encoded_content;

      mail($recipient_email, $subject, $mensaje, $headers);
      
    }

    header('Location: https://' . $servidor . '/home.php');

}

【问题讨论】:

  • 您是否尝试打印文件变量?回声“
    ”; print_r($_FILES);回声“
    ”;出口;你得到什么了吗???

标签: php html email


【解决方案1】:

为表单添加 enctype="multipart/form-data" 属性。

https://www.w3schools.com/tags/att_form_enctype.asp

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-06
    • 2013-09-15
    • 2017-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-20
    • 2015-12-29
    相关资源
    最近更新 更多