Php 在电子邮件中发送附件的表格

Php 在电子邮件中发送附件的表格,php,email,email-attachments,Php,Email,Email Attachments,我是新来的,但在浏览了很多页面后,我想出了一个实际有效的表单,并将附件发送到我的电子邮件中。然而,如果在填写表格时没有附上附件,我会得到这个而不是感谢页面 警告:file_get_contents():第21行/home/advem/public_html/careers.php中的文件名不能为空 警告:无法修改标题信息-标题已由第61行/home/advem/public_html/careers.php中的/home/advem/public_html/careers.php:21开始输出)

我是新来的,但在浏览了很多页面后,我想出了一个实际有效的表单,并将附件发送到我的电子邮件中。然而,如果在填写表格时没有附上附件,我会得到这个而不是感谢页面

警告:file_get_contents():第21行/home/advem/public_html/careers.php中的文件名不能为空

警告:无法修改标题信息-标题已由第61行/home/advem/public_html/careers.php中的/home/advem/public_html/careers.php:21开始输出)发送

但是,如果没有使用附件,电子邮件仍然会发送给我。 非常感谢您的帮助

<?php

if(!isset($_POST['submit']))
{
    //This page should not be accessed directly. Need to submit the form.
    echo "error; you need to submit the form!";
}

    if(isset($_POST['submit']))
    {

        //Deal with the email
        $to = 'email@testdesign.net';
        $subject = 'New Proof Request';
        $visitor_email = $_POST['email'];

        $message = strip_tags($_POST['message']);
        $sku = $_POST['sku'];
        $phone = $_POST['phone'];
        $name = $_POST['name'];
        $attachment = chunk_split(base64_encode(file_get_contents($_FILES['file']['tmp_name'])));
        $filename = $_FILES['file']['name'];

        $boundary =md5(date('r', time())); 

        //Validate first
if(empty($name)||empty($visitor_email)) 
{
    echo "Name and email are mandatory!";
    exit;
}

        $headers = "From: email@testdesign.net\r\nReply-To: $visitor_email";
        $headers .= "\r\nMIME-Version: 1.0\r\nContent-Type: multipart/mixed; boundary=\"_1_$boundary\"";

        $message="This is a multi-part message in MIME format.

--_1_$boundary
Content-Type: multipart/alternative; boundary=\"_2_$boundary\"

--_2_$boundary
Content-Type: text/plain; charset=\"iso-8859-1\"
Content-Transfer-Encoding: 7bit

$name
$sku
$message
$phone

--_2_$boundary--
--_1_$boundary
Content-Type: application/octet-stream; name=\"$filename\" 
Content-Transfer-Encoding: base64 
Content-Disposition: attachment 

$attachment
--_1_$boundary--";

        mail($to, $subject, $message, $headers);
        //done. redirect to thank-you page.
        header('Location: thank-you-proof-request');
        
        // Function to validate against any email injection attempts
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;
  }
}

    }
?>

我也在下面的链接中尝试了这个答案,但是在那里我也没有运气


我认为您应该将第21行的代码更改为:

    $attachment = '';
    $filename = '';
    if($_FILES["file"]["error"] != 4 && file_exists($_FILES['file']['tmp_name'])){ 
        $attachment = chunk_split(base64_encode(file_get_contents($_FILES['file']['tmp_name'])));
        $filename = $_FILES['file']['name'];
    }
第51行:

    if(!empty($filename)){
        --_1_$boundary
        Content-Type: application/octet-stream; name=\"$filename\" 
        Content-Transfer-Encoding: base64 
        Content-Disposition: attachment 

        $attachment
        --_1_$boundary--";
    }

我建议你使用一个库来发送电子邮件,比如,它有很多可以使用的东西,而且很容易使用。

在使用
文件获取内容
功能之前,只需添加一个检查,看看
$\u文件
是否包含任何内容。至于已经发送的标题,请参见Qirel…..您能告诉我如何编写该标题以及将其放置在何处吗?到目前为止,我所掌握的只是从Stackoverflow的不同问题中复制和粘贴。恐怕我会把事情搞砸的。