【问题标题】:php mail sending attachment the can't be openedphp邮件发送附件无法打开
【发布时间】:2012-12-24 18:05:44
【问题描述】:

我正在尝试编写一个页面,用户可以在其中发送电子邮件和附件。我想我快到了。现在我收到了电子邮件,但附件的文件大小为空,或者当我尝试打开它时,我收到一条无法打开的消息。任何帮助表示赞赏。

    function valid_email($Email)
{
    //new regex, didn't give me any errors...might be a bit more exact
  if (ereg('^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.(([0-9]{1,3})|([a-zA-Z]{2,3})|(aero|coop|info|museum|name))$', $Email) != false)
    return true;
  else 
    return false;
}

function sendEmail($email){
    return  mail ($email['to'], $email['subject'], $email['message'], $email['headers']);
}


if ( strlen($_FILES['Resume_File']['name']) > 0 )
{   // If a file was uploaded, do some processing
    $filename = preg_replace('/[^a-zA-Z0-9._-]/', '', $_FILES['Resume_File']['name']);
    $filetype = $_FILES["Resume_File"]["type"];
    $filesize = $_FILES["Resume_File"]["size"];
    $filetemp = $_FILES["Resume_File"]["tmp_name"]; 
    $ext = substr($filename, strpos($filename,'.'), strlen($filename)-1);

    if ( !preg_match('/\.(doc|docx|odt|pdf|rtf|txt)/i', $ext) )
    {   $errors++;
        $errorLog .= "Upload filetype not supported.";
    }
    else if ( $filesize > 2000000 )
    {   $errors++;
        $errorLog .= "File size too high, up to 2MB is allowed.";
    }
    else
    {   // Looks like the file is good, send it with the email
        //$fp = fopen($filetemp, "rb");
        //$file = fread($fp, $filesize);
        //$file = chunk_split(base64_encode($file));

        //$email['headers'] .= "\n--{$num}\n";
        //$email['headers'] .= "Content-Type:{$filetype}";
        //$email['headers'] .= "name={$filename}r\n";
        //$email['headers'] .= "Content-Disposition: attachment; ";
        //$email['headers'] .= "filename={$filename}\n";
        //$email['headers'] .= "{$file}";

    }
}

// get posted data into local variables
$fname = trim(stripslashes($_POST['fname']));
$lname = trim(stripslashes($_POST['lname']));
$emailAddress = trim(stripslashes($_POST['email']));
$company = trim(stripslashes($_POST['company'])); 
$information = trim(stripslashes($_POST['information']));
$subject = trim(stripslashes($_POST['subject']));
$title = trim(stripslashes($_POST['title']));

//setup email
$to = 'me@me.com';
$subject = "Resume Submission";

$headers = "From: {$fname} {$lname} <{$emailAddress}>\r\n";

// prepare email body text
$message = "First Name: {$fname} <br>";
$message .= "Last Name: {$lname} <br>";
$message .= "Email: {$emailAddress} <br>";
$message .= "Title: {$title} <br><br>";
$message .= "Comments: {$information}";

if ( $errors == 0 ) {
    // Attachment headers

    //$to = "myemail@mydomain.com";
    //$from = "Website <website@mydomain.com>";
    //$subject = "Test Attachment Email";

    $separator = md5(time());

    // carriage return type (we use a PHP end of line constant)
    $eol = PHP_EOL;

    // attachment name
    //$filename = "document.pdf";

    //$pdfdoc is PDF generated by FPDF
    $attachment = chunk_split(base64_encode($pdfdoc));

    // main header
    $headers .= "From: ".$from.$eol;
    $headers .= "MIME-Version: 1.0".$eol; 
    $headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"";

    // no more headers after this, we start the body! //

    $body = "--".$separator.$eol;
    $body .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
    $body .= "This is a MIME encoded message.".$eol;

    // message
    $body .= "--".$separator.$eol;
    $body .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
    $body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
    $body .= $message.$eol;

    // attachment
    $body .= "--".$separator.$eol;
    $body .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol; 
    $body .= "Content-Transfer-Encoding: base64".$eol;
    $body .= "Content-Disposition: attachment".$eol.$eol;
    $body .= $attachment.$eol;
    $body .= "--".$separator."--";

}



//sendEmail($email);

// validation

if ( $errors == 0 ) {

    if (valid_email($emailAddress) && $fname != "" && $lname != "") { //if return is true...
        mail($to, $subject, $body, $headers);
        echo 0; //Success
    }else { //otherwise
        echo 1; //Error
    }

} else {
    echo 1; //Error
}

我正在使用我在这里找到的部分代码

PHP mail() attachment problems

谢谢!

【问题讨论】:

  • 离题,但我注意到您正在使用ereg() 函数。请注意,此功能已被弃用一段时间,并且已从最新版本的 PHP 中完全删除。你应该切换到preg_match()(我看到你在其他地方使用)
  • 再次离题,但如果您使用stripslashes() 获取$_POST 数据,则意味着您正在使用PHP 的Magic Quotes 功能。请注意,此功能也已被弃用,并已从最近的 PHP 版本中删除。你也应该尝试解决这个问题,否则当你升级 PHP 版本时,它会让你的生活变得非常困难。

标签: forms email-attachments php


【解决方案1】:

PHP 的mail() 功能确实很差,尤其是在尝试执行诸如添加附件之类的高级操作时。它真的只值得用于最基本的“向站点管理员发送通知电子邮件”类型的电子邮件,即使这样它也并不总是最好的解决方案。

我建议放弃使用 mail() 函数本身的任何尝试,转而使用像适当命名的 phpMailer 这样的体面的 PHP 邮件程序类。

phpMailer 使从 PHP 创建电子邮件变得非常容易。我拥有您可能想要的所有功能,包括让添加附件、编写 HTML 电子邮件等变得非常容易。

但是 phpMailer 最好的地方在于它消除了浪费数十行代码格式化电子邮件标题的所有需要​​。所有带有分隔符和 mime 类型的东西都变成了几行简单的代码。更易于阅读,更易于维护,并且不太可能出现错误。你赢了。

【讨论】:

    【解决方案2】:
    //$pdfdoc is PDF generated by FPDF
        $attachment = chunk_split(base64_encode($pdfdoc));
    

    $pdfdoc 在页面的任何地方都没有提到。我想这就是问题所在:|

    【讨论】:

    • 谢谢 Prasanth,它帮助我找到了答案,我得到了这个工作
    猜你喜欢
    • 2016-01-15
    • 2011-03-24
    • 2019-11-29
    • 1970-01-01
    • 2011-07-26
    • 1970-01-01
    • 1970-01-01
    • 2014-10-01
    • 2023-03-21
    相关资源
    最近更新 更多