在电子邮件中发送附件的表单

时间:2016-02-08 16:16:50

标签: 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上的输出)

但是,如果未使用附件,电子邮件仍会发送给我。 非常感谢任何帮助。

<?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;
  }
}

    }
?>

我也尝试过以下链接的答案,但也没有运气。

PHP - sending email with attachment does not show message content

1 个答案:

答案 0 :(得分:0)

我认为您应该将第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--";
    }

我建议您使用库来发送PHPMailer这样的电子邮件,它有很多可以使用的东西,并且易于使用。