【问题标题】:Adding an attachment to an email form: Integrating PHPmailer for handling the attachment向电子邮件表单添加附件:集成 PHPmailer 以处理附件
【发布时间】:2012-07-10 02:30:10
【问题描述】:

下面是我的 PHP 脚本,它处理来自 HTML 表单的输入。该脚本运行良好,但在尝试和尝试之后,我无法获得普通的旧 PHP 代码来处理表单中的附件。

在阅读了几天并测试了不同的代码之后,每个人都建议使用预制的应用程序来简化附件。 (我选择了 PHPmailer。)

我试图让它工作,但我只能找到静态文件的示例:

PHPmailer 自述文件中的示例:

$mail->AddAttachment("c:\temp\js-bak.sql"); // 添加附件

$mail->AddAttachment("c:/temp/11-10-00.zip");

我的问题是,是否可以使用 HTML 表单中的“文件”选择器附加文件? 如果是这样,我可以将 PHPmailer 附件代码集成到我的生产脚本中而无需重新编码整个文件吗?

我需要什么代码来附加表单中的文件?

我当前的脚本:

<?php

require("phpmailer.inc.php");
$email = new phpmailer;
$uploaddir = 'uploads/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
    $email->AddAttachment($uploadfile);
} else {
    echo "File failed to attach. Maximum size of 5mb\n";    
}

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

    // EDIT THE 2 LINES BELOW AS REQUIRED
    $email_to = "example@abc.com";
    $email_subject = "IT Request (Support)";  //Email Subject


    function died($error) {
        // your error code can go here
        echo "We are very sorry, but there were error(s) found with the form you submitted. ";
        echo "These errors appear below.<br /><br />";
        echo $error."<br /><br />";
        echo "Please go back and fix these errors.<br /><br />";
        die();
    }

    // validation: expected data already exists
    if(!isset($_POST['name']) ||
    !isset($_POST['datereg']) || 
    !isset($_POST['message']) || 
    !isset($_POST['email'])) {
        died('We are sorry, but there appears to be a problem with the form you submitted.');       
    }

    $name = $_POST['name'];                 //Required
    $request_date = $_POST['datereg'];      //Required
    $email_address = $_POST['email'];       //Required
    $message = $_POST['message'];           //Not required
    $error_message = "";    
    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
    if(!preg_match($email_exp,$email_address)) {
        $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
    }
    $string_exp = "/^[\$A-Za-z0-9._%-]/";
    if(!preg_match($string_exp,$name)) {
        $error_message .= 'The Name you entered does not appear to be valid.<br />';
    }
    if(!preg_match($string_exp,$request_date)) {
        $error_message .= 'The Request Date you entered does not appear to be valid.<br />';
    } 
    if(!preg_match($string_exp,$message)) {
        $error_message .= 'You must list a description in order to submit the form.<br />';
    }     
    if(strlen($error_message) > 0) {
        died($error_message);
    }
    $email_message = "---IT Request Form Content---\n\n";

    function clean_string($string) {
        $bad = array("content-type","bcc:","to:","cc:","href");
        return str_replace($bad,"",$string);
    }

    //These fields are required per validation so they don't need to be tested.
    $email_message .= "Name: ".clean_string($name)."\n";
    $email_message .= "Email: ".clean_string($email_address)."\n";
    $email_message .= "Date of Request: ".clean_string($request_date)."\n";
    $email_message .= "Description: ".clean_string($message)."\n";  

    // create email headers
    $headers = 'From: '.$email_address."\r\n".
    'Reply-To: '.$email_address."\r\n" .
    'X-Mailer: PHP/' . phpversion();
    @mail($email_to, $email_subject, $email_message, $headers);  
?>

    <!-- include your own success html here -->

    Thank you for submitting an IT Support Request. Your information will be reviewed as soon as possible.
    <META HTTP-EQUIV="refresh" CONTENT="2;URL=http://getsupport/index.html">

<?php
}
?>

【问题讨论】:

  • 文件上传后,就是磁盘上的常规文件。所以你只需要像任何其他文件一样附加它。
  • 别忘了照常做:检查是否上传、文件类型是否正确、不要太大、存档关闭/附加后删除等。
  • $email->AddAttachment($_FILES['userfile']['tmp_name'], $_FILES['userfile']['name']);其中 $email 是 PHPmailer 类的一个实例。
  • 好的,我更新了我的原始脚本。我现在正在测试文件的大小并上传到服务器。表单正常发送,但未附加文件。我缺少什么代码来包含附件?我是否需要在底部重新编码所有电子邮件编译位以包括 $email->AddAttachment($uploadfile); ?

标签: php phpmailer


【解决方案1】:

您将在$_FILES 变量中找到临时文件的路径和文件名。看看the documentation

【讨论】:

  • 为了简单起见,我跳过了 size\type 验证。在它工作后,我会重新添加它。这是附加功能的要求吗?
猜你喜欢
  • 1970-01-01
  • 2011-02-03
  • 1970-01-01
  • 2023-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-05
相关资源
最近更新 更多