doupin2013 2012-07-10 15:04
浏览 61

将附件添加到电子邮件表单:集成PHPmailer以处理附件

Below is my PHP script that handles the input from an HTML form. The script works great, but after trying and trying, I couldn't get plain old PHP code to handle an attachment from the form.

After reading for days and testing different code, everyone suggests using a pre-made application to simplify the attachment. (I chose PHPmailer.)

I've tried to get it working, but all I can find are examples of static files:

Example from the PHPmailer readme:

$mail->AddAttachment("c:\temp\js-bak.sql"); // add attachments

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

My question is, it possible to attach a file using a "file" selector from the HTML form? If so, can I integrate the PHPmailer attachment code into my production script without having to recode the entire file?

What code do I need to attach the file from my form?

My current script:

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

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---

";

    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)."
";
    $email_message .= "Email: ".clean_string($email_address)."
";
    $email_message .= "Date of Request: ".clean_string($request_date)."
";
    $email_message .= "Description: ".clean_string($message)."
";  

    // create email headers
    $headers = 'From: '.$email_address."
".
    'Reply-To: '.$email_address."
" .
    '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
}
?>
  • 写回答

1条回答 默认 最新

  • doubian19900911 2012-07-10 15:09
    关注

    You'll find the path to the temporary file and the name of the file in the $_FILES variable. Take a look at the documentation.

    评论

报告相同问题?

悬赏问题

  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口