如何通过phpmailer集成附加文件与以下代码

时间:2016-03-17 16:43:45

标签: php file phpmailer attachment

我正在尝试通过phpmailer发送名为proposal的文件。但它似乎不起作用。我无法让$_FILES工作或将isset$_POST一起应用于<?php session_start(); //to access errors from form_process.php require_once '../helper/security.php'; //include security php require_once("../includes/config.php"); $errors = isset($_SESSION['errors']) ? $_SESSION['errors'] : []; $fields = isset($_SESSION['fields']) ? $_SESSION['fields'] : []; $success = isset($_SESSION['success']); $pageTitle = "valhalla | Franchise"; $pageId = "franchise"; include(ROOT_PATH . "includes/header.php"); ?> 。请帮忙。提前谢谢。

这是HTML文件:

PHP包含在html

之上
<!-- BANNER
================================================== -->
<div id="banner" class="break">
    <div class="container">
        <div class="row">
            <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">                   
                <div class="tabs_wrapper">

                    <?php if($success == 'true'): ?>
                        <div class="success_notice">
                            <ul>
                                <li><?php echo '&#8226;Email sent!' ?></li> <!--Lists down errors-->
                            </ul>
                        </div>
                    <?php endif; ?> 

                    <?php if(!empty($errors)): ?>
                        <div class="error_notice">
                            <ul>
                                <li>&#8226;<?php echo implode('</li><li>&#8226;', $errors); ?></li> <!--Lists down errors-->
                            </ul>
                        </div>
                    <?php endif; ?>

                </div><!--/.tabs_wrapper-->
            </div><!--/.col-->                          
        </div><!--/.row-->                      
        <div class="row">
            <div class="col-md-12">
                <div class="image_div_setting">
                    <img src="<?php echo BASE_URL; ?>img/banner.png" alt="Banner for franchise">
                </div>                          
            </div>
        </div>
    </div>          
</div>

<!-- FRANCHISE DETAILS
================================================== -->
<div id="franchise_details">
    <div class="container">
        <div class="row">
            <div class="col-md-3">
                <div class="page_header">
                    <h1>text</h1>                           
                </div>                          
            </div>
            <div class="col-md-6">
                <div class="description">
                    <p>
                        TEXT
                    </p>
                    <p>

                    </p>                                
                </div>                              
            </div>
            <div class="col-md-3">
                <div class="contact_form">                      
                    <button type="button" class="btn btn-default center-block" data-toggle="modal" data-target="#franchiseModal">INQUIRE NOW</button>
                </div>                          
            </div>                      
        </div>
    </div>          
</div>

<!-- MODAL -->
<div class="generalModal">
    <!--TEXT 1-->           
    <div class="modal fade" id="franchiseModal" tabindex="-1" role="dialog" aria-labelledby="franchiseModal">
      <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">X</span></button>
            </div>


            <div class="modal-body">
                <div class="modalMainHeader">
                    <h4 class="modal-title" id="franchiseModalLabel">FRANCHISE</h4>                     
                </div>
                <div class="modalDescription">
                    <p>
                        This is text.   
                    </p>                            
                </div>
                <div class="modalSubHeader">
                    <h4 class="modal-title" id="franchiseModalLabel">TEXT</h4>                          
                </div>
                <div class="contact_form">            
                    <form name="franchise" method="post" action="<?php echo BASE_URL; ?>mail/form_process_franchise.php">
                        <div class="row">
                            <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">                   
                                <div class="tabs_wrapper">

                                    <?php if($success == 'true'): ?>
                                        <div class="success_notice">
                                            <ul>
                                                <li><?php echo '&#8226;Email sent!' ?></li> <!--Lists down errors-->
                                            </ul>
                                        </div>
                                    <?php endif; ?> 

                                    <?php if(!empty($errors)): ?>
                                        <div class="error_notice">
                                            <ul>
                                                <li>&#8226;<?php echo implode('</li><li>&#8226;', $errors); ?></li> <!--Lists down errors-->
                                            </ul>
                                        </div>
                                    <?php endif; ?>

                                </div><!--/.tabs_wrapper-->
                            </div><!--/.col-->                          
                        </div><!--/.row-->                                  
                        <div class="row">
                            <div class="col-md-12">
                              <div class="form-group">
                                <label for="name">NAME</label>
                                <input type="text" name="name" class="form-control" id="" placeholder="" <?php echo isset($fields['name']) ? ' value="' . e($fields['name']) . '"' : '' ?>>
                              </div>                                    
                            </div>                                                                  
                            <div class="col-md-12">
                                <div class="form-group">
                                    <label for="email" class="control-label">EMAIL</label>
                                    <input type="text" name="email" class="form-control" id="" placeholder="" <?php echo isset($fields['email']) ? ' value="' . e($fields['email']) . '"' : '' ?>>
                                </div>                  
                            </div>
                            <div class="col-md-12">
                                <div class="form-group">
                                    <label for="phone" class="control-label">PHONE:</label>
                                    <input type="text" name="phone" class="form-control" id="" placeholder="" <?php echo isset($fields['phone']) ? ' value="' . e($fields['phone']) . '"' : '' ?>>
                                </div>                              
                            </div>
                            <div class="col-md-12">
                                <div class="form-group">
                                    <label for="location" class="control-label">LOCATION:</label>
                                    <input type="text" name="location" class="form-control" id="" placeholder="" <?php echo isset($fields['location']) ? ' value="' . e($fields['location']) . '"' : '' ?>>
                                </div>                              
                            </div>
                            <div class="col-xs-12 col-sm-6 col-md-6">
                                <div class="form-group">
                                    <label for="proposal">PROPOSAL</label>
                                    <div class="input-group">
                                        <div class="row">
                                            <div class="col-xs-8 col-sm-7 col-md-7">
                                                <input type="text" name="proposal" class="form-control" readonly="">                                                
                                            </div>
                                            <div class="col-xs-4 col-sm-5 col-md-5">
                                                <div class="input-group-btn">
                                                    <div class="btn btn-primary btn-file">
                                                        UPLOAD <input type="file" name="proposal">
                                                    </div>
                                                </div>                                              
                                            </div>                                          
                                        </div>
                                    </div>
                                    <span class="help-block">
                                        *PROPOSAL MUST BE IN PDF FORMAT
                                    </span>
                                </div>                                  
                            </div>
                            <div class="col-xs-12 col-sm-6 col-md-6">
                                <div class="form-group">
                                    <div class="captcha_security_section">
                                        <div class="row">
                                            <div class="col-xs-8 col-sm-6 col-md-8 col-lg-8">
                                                <label class="" for="captcha">Captcha</label>
                                                <input type="text" name="captcha" class="form-control" <?php echo isset($fields['captcha']) ? ' value="' . e($fields['captcha']) . '"' : '' ?>>                     
                                            </div>
                                            <div class="col-xs-4 col-sm-6 col-md-4 col-lg-4">
                                                <label class="" for="captcha">Code</label>                                      
                                                <img src="<?php echo BASE_URL; ?>mail/captcha.php"/><br>                                        
                                            </div>                                                  
                                        </div>
                                    </div>
                                </div>
                            </div>                                          
                        </div>
                        <div class="row">
                            <div class="col-md-12">
                                <div class="modal-footer">
                                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                                    <button type="submit" name="submit" id="submit" value="Submit" class="btn btn-default">Send</button>
                                </div>                                          
                            </div>
                        </div>
                    </form>
                </div>  
            </div><!--/modal-body-->
        </div>
      </div>
    </div>
</div><!-- /generalModal-->


<!-- PRESS KIT
================================================== -->
<div class="pdf_download">
    <div class="container">
        <div class="row">
            <div class="col-md-12">
                <div class="pdf_header_container">
                    <div class="header text-center">
                        <h3>OFFICIAL PRESS KIT</h3>
                    </div>
                    <div class="description text-center">
                        <p>*You can download the official valhalla PRESS KIT if you click on the boxes below:</p>
                        <p>*You need to have adobe reader installed:</p>                                    
                    </div>                          
                </div>
                <div class="row">
                    <div class="col-md-1">

                    </div>
                    <div class="col-md-10">
                        <div class="row">
                            <div class="col-md-4">

                            </div>
                            <div class="col-md-4">
                                <div class="pdf_container text-center">
                                    <a href="<?php echo BASE_URL; ?>img/menu/pdf/valhalla_press_kit_updated.pdf" target="_blank">                                           
                                        <div class ="pdf_pics">
                                            <img src="<?php echo BASE_URL; ?>img/menu/pdf/presskit_thumbnail.png" alt="Official Press Kit" class="img-circle">
                                        </div>                                          
                                        <div class="header">
                                            <h4>valhalla MENU PRESS KIT</h4>
                                        </div>
                                        <div class="description">
                                            <p>You can download the official valhalla Press Kit!</p>
                                        </div>                                                  
                                        <div class="note">
                                            <p>CLICK ON THE BOX</p>
                                        </div>
                                    </a>                                                
                                </div>                                  
                            </div>
                            <div class="col-md-4">

                            </div>                                          
                        </div>
                    </div>                              
                    <div class="col-md-1">

                    </div>
                </div>

            </div>
        </div>
    </div>
</div>

PHP代码结束

<?php 

include(ROOT_PATH . "includes/footer.php"); 

unset($_SESSION['errors']);
unset($_SESSION['fields']);
unset($_SESSION['success']);
?>

HTML文件的结束

<?php

session_start(); //for transporting information

require_once '../phpmailer/PHPMailerAutoload.php'; //included php mailer

header('Location:../franchise/'); //redirect back to franchise.php after submission

$errors = []; //array for errors

//print_r($_POST);


//to check if fields are set
if(isset($_POST['name'], $_POST['email'], $_POST['phone'], $_POST['location'], $_POST['proposal'] )) { //fields are empty

    $fields = [                                 //store empty fields
        'name' => $_POST['name'],
        'email' => $_POST['email'],     
        'phone' => $_POST['phone'],
        'location' => $_POST['location'],
        'proposal' => $_POST['proposal']
    ];

    foreach($fields as $field => $data) {
        //file extension
        $file_ext = explode('.', $fields['proposal']);
        $file_ext = strtolower(end($file_ext));
        $allowed = array('pdf', 'pdf');


        if(empty($data)) {
            $errors[] = 'Enter ' . $field . ' field';
        } else {
            if ((!preg_match("/^[a-zA-Z ]*$/",$fields['name'])) && (!empty($fields['name'])) ) {
                $errors['name'] = 'Name: Only letters and white space allowed.';            
            }

            if ((!preg_match("/^[0-9]{0,12}$/",$fields['phone'])) && (!empty($fields['phone'])) ) {
                $errors['phone'] = 'Phone: Numbers only';           
            }

            if((!filter_var($fields['email'], FILTER_VALIDATE_EMAIL)) && (!empty($fields['email'])) ){
                $errors['email'] = 'Please input proper email.';
            }

            if((!in_array($file_ext, $allowed)) && (!empty($fields['proposal'])) ) {
                    $errors['proposal'] = 'Only pdf allowed.';                          
            }

        }
    } 

    if(empty($errors) && ($_POST["captcha"]!=""&&$_SESSION["code"]==$_POST["captcha"]) ) { //this is for sending the mail

        $m = new PHPMailer;

        $m->isSMTP();
        $m->SMTPAuth = true;

        $m->SMTPDebug = 2; // for debugging

        //These 5 elements are used to connect to server
        $m->Host = 'hosting';
        $m->Username = 'randomusername';
        $m->Password = 'randompassword';
        $m->SMTPSecure = 'ssl';
        $m->Port = 465;

        $m->isHTML(); // To send message as html

        $m->Subject = 'Contact form submitted by ' . $fields['name'];
        $m->Body = 'You have received a new message from your website contact form.' .
        '<p>' . 'The reservation details are the following: ' . '</p>' .        
        '<p>' . '<b>' . 'From: '      . '</b>' . $fields['name'] . ' (' . $fields['email'] . ')' . '</p>' .
        '<p>' . '<b>' . 'Phone: '    . '</b>' . $fields['phone'] . '</p>' .
        '<p>' . '<b>' . 'Location: '      . '</b>' . $fields['location'] . '</p>';

        $m->FromName = 'valhalla.com.ph'; //if not then it will be replaced with user
        $m->SetFrom('valhalla@valhalla.com.ph', 'valhalla.com.ph'); //Important for gmail.

        $m->AddReplyTo($fields['email'], $fields['name']); //doesn't work with google

        //Target email. Message to be sent to
        $m->AddAddress('formsubmission@valhalla.com.ph', 'valhalla' );

        //$mail->AddAttachment($file);
        $mail->AddAttachment($_FILES['proposal']['tmp_name'], $_FILES['proposal']['name']);     

        // delete the cookie so it cannot sent again by refreshing this page
        setcookie('tntcon','');

        if($m->send()){
                $success='true';                
        } else {
            $errors[] = 'Sorry, could not send email. Try again later.';
        }

    } else {
        $errors[] = 'Invalid Security Code.';
    }


} else {                                                          //fields are not empty
    $errors[] = 'Something wrong';
}

$_SESSION['errors'] = $errors;  //stores errors from array and loop
$_SESSION['fields'] = $fields;
$_SESSION['success'] = $success;
?>

HTML文件的结尾

这是一个不同的PHP文件:

@Test
public void registerUserTest(){
    // code
}

@Test
public void welcomeNewUserTest(){
    // code
}

@Test
public void questionaireNewUserTest(){
    // code
}

1 个答案:

答案 0 :(得分:0)

你为SO问题发布的内容过多,但我认为无论如何我都发现了你的问题。您的表单标记缺少允许文件上载的MIME类型。而不是:

<form name="franchise" method="post" action="<?php echo BASE_URL; ?>mail/form_process_franchise.php">

你需要:

<form name="franchise" method="post" enctype="multipart/form-data" action="<?php echo BASE_URL; ?>mail/form_process_franchise.php">

您还需要MAX_FILE_SIZE元素:

<input type="hidden" name="MAX_FILE_SIZE" value="300000">

将值更改为适合您预期上传的内容。

之后您需要阅读PHP {38}提供的PHP docs on handling uploadssend_file_upload示例,但简短版本是您需要使用move_uploaded_file来确保上传的是在尝试使用它们之前是安全的。

所有这些都可以在PHP中处理文件上传的任何初学者级教程中找到,所以请在将来发布之前进行搜索。