doujiao7325 2016-03-17 16:43
浏览 54
已采纳

如何通过phpmailer集成附件文件与以下代码[关闭]

I am trying to send a file named proposal via phpmailer. But it doesn't seem to work. I can't seem to get $_FILES working or apply the isset to it together with the $_POST. Please help. Thank you in advance.

This is the HTML file:

PHP included on top of html

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

End of PHP code

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

End of PHP for HTML file

<?php 

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

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

End of HTML file

This is a different PHP file:

<?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;
?>
  • 写回答

1条回答 默认 最新

  • dongtan5558 2016-03-17 17:11
    关注

    You've posted way too much for an SO question, but I think I spotted your problem anyway. Your form tag is missing a MIME type that allows file uploads. Instead of:

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

    You need:

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

    You also need a MAX_FILE_SIZE element:

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

    Alter the value to whatever is appropriate for your expected uploads.

    After that you need to read the PHP docs on handling uploads, and the send_file_upload example provided with PHPMailer, but the short version is that you need to make use of move_uploaded_file to ensure your uploads are safe before trying to use them.

    All of this will be found in any beginner-level tutorial on handling file uploads in PHP, so please search before posting in future.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛