Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

How to send files via php mailer with the following code:

Hi there, 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;
?>