简体   繁体   中英

PHPMailer multiple attachments with one input button

I'm trying to make a contact form that is capable of multiple image attachments.

I got the contact form from here and everything's working fine. I really want to add the ability to attach more than one image within a single input element.

At the top of the contact form is this php:

<?PHP

require_once("./include/fgcontactform.php");

$formproc = new FGContactForm();    
$formproc->AddRecipient('****@****.com');
$formproc->SetFormRandomKey('************');
$formproc->AddFileUploadField('photo','jpg,jpeg,gif,png,bmp',4000);

if(isset($_POST['submitted']))
{
   if($formproc->ProcessForm())
   {
        $formproc->RedirectToURL("thank-you.php");
   }
}
?>

This is the html for the form (I've cut it down so its only the upload portion of the form):

<html>
<head>
    <script type='text/javascript' src='scripts/gen_validatorv31.js'></script>
    <script type='text/javascript' src='scripts/fg_captcha_validator.js'></script>
</head>
<body>

<form id='contactus' action='<?php echo $formproc->GetSelfScript(); ?>' method='post' enctype="multipart/form-data" accept-charset='UTF-8'>

   <h4><label for='photo' >Please upload your images:</label><br/>
    <input type="file" name='photo' id='photo' multiple="multiple"/><br/>

<span style="color:#999999;font-size:12px;">(To select more than 1 image hold the "CTRL" key as you click. If you're on Mac hold the "cmd" key.)</span><br/></h4>
    <span id='contactus_photo_errorloc' class='error'></span>

</form>

<script type='text/javascript'>
// <![CDATA[

    var frmvalidator  = new Validator("contactus");
    frmvalidator.EnableOnPageErrorDisplay();
    frmvalidator.EnableMsgsTogether();
    frmvalidator.addValidation("name","req","Please provide your name");

    frmvalidator.addValidation("email","req","Please provide your email address");

    frmvalidator.addValidation("email","email","Please provide a valid email address");

    frmvalidator.addValidation("message","maxlen=2048","The message is too long!(more than 2KB!)");

    frmvalidator.addValidation("photo","file_extn=jpg;jpeg;gif;png;bmp","Upload images only. Supported file types are: jpg,gif,png,bmp");
// ]]>
</script>
</body>
</html>

I have managed to get multiple image uploads working by adding the following to the top of the initial PHP and adding new input elements within the html with the relevant names:

$formproc->AddFileUploadField('photo2','jpg,jpeg,gif,png,bmp',4000);
$formproc->AddFileUploadField('photo3','jpg,jpeg,gif,png,bmp',4000);
$formproc->AddFileUploadField('photo4','jpg,jpeg,gif,png,bmp',4000);
$formproc->AddFileUploadField('photo5','jpg,jpeg,gif,png,bmp',4000);

Although this works I'd much prefer to have only one input button and allow people to attach multiple images with it (with a max of around 10, but that's an issue for another time).

I believe this is the php that composes the email if that helps:

<?PHP

require_once("class.phpmailer.php");

class FG_CaptchaHandler
{
    function Validate() { return false;}
    function GetError(){ return '';}
}

class FGContactForm
{
    var $receipients;
    var $errors;
    var $error_message;
    var $name;
    var $email;
    var $message;
    var $from_address;
    var $form_random_key;
    var $conditional_field;
    var $arr_conditional_receipients;
    var $fileupload_fields;
    var $captcha_handler;

    var $mailer;

function FGContactForm()
{
    $this->receipients = array();
    $this->errors = array();
    $this->form_random_key = 'HTgsjhartag';
    $this->conditional_field='';
    $this->arr_conditional_receipients=array();
    $this->fileupload_fields=array();

    $this->mailer = new PHPMailer();
    $this->mailer->CharSet = 'utf-8';
}

function EnableCaptcha($captcha_handler)
{
    $this->captcha_handler = $captcha_handler;
    session_start();
}

function AddRecipient($email,$name="")
{
    $this->mailer->AddAddress($email,$name);
}

function SetFromAddress($from)
{
    $this->from_address = $from;
}
function SetFormRandomKey($key)
{
    $this->form_random_key = $key;
}
function GetSpamTrapInputName()
{
    return 'sp'.md5('KHGdnbvsgst'.$this->GetKey());
}
function SafeDisplay($value_name)
{
    if(empty($_POST[$value_name]))
    {
        return'';
    }
    return htmlentities($_POST[$value_name]);
}
function GetFormIDInputName()
{
    $rand = md5('TygshRt'.$this->GetKey());

    $rand = substr($rand,0,20);
    return 'id'.$rand;
}


function GetFormIDInputValue()
{
    return md5('jhgahTsajhg'.$this->GetKey());
}

function SetConditionalField($field)
{
    $this->conditional_field = $field;
}
function AddConditionalReceipent($value,$email)
{
    $this->arr_conditional_receipients[$value] =  $email;
}

function AddFileUploadField($file_field_name,$accepted_types,$max_size)
{

    $this->fileupload_fields[] =
        array("name"=>$file_field_name,
        "file_types"=>$accepted_types,
        "maxsize"=>$max_size);
}

function ProcessForm()
{
    if(!isset($_POST['submitted']))
    {
       return false;
    }
    if(!$this->Validate())
    {
        $this->error_message = implode('<br/>',$this->errors);
        return false;
    }
    $this->CollectData();

    $ret = $this->SendFormSubmission();

    return $ret;
}

function RedirectToURL($url)
{
    header("Location: $url");
    exit;
}

function GetErrorMessage()
{
    return $this->error_message;
}
function GetSelfScript()
{
    return htmlentities($_SERVER['PHP_SELF']);
}

function GetName()
{
    return $this->name;
}
function GetEmail()
{
    return $this->email;
}
function GetMessage()
{
    return htmlentities($this->message,ENT_QUOTES,"UTF-8");
}


function SendFormSubmission()
{
    $this->CollectConditionalReceipients();

    $this->mailer->CharSet = 'utf-8';

    $this->mailer->Subject = "Customer installation competition submition from $this->name";

    $this->mailer->From = $this->GetFromAddress();

    $this->mailer->FromName = $this->name;

    $this->mailer->AddReplyTo($this->email);

    $message = $this->ComposeFormtoEmail();

    $textMsg = trim(strip_tags(preg_replace('/<(head|title|style|script)[^>]*>.*?<\/\\1>/s','',$message)));
    $this->mailer->AltBody = @html_entity_decode($textMsg,ENT_QUOTES,"UTF-8");
    $this->mailer->MsgHTML($message);

    $this->AttachFiles();

    if(!$this->mailer->Send())
    {
        $this->add_error("Failed sending email!");
        return false;
    }

    return true;
}

function CollectConditionalReceipients()
{
    if(count($this->arr_conditional_receipients)>0 &&
      !empty($this->conditional_field) &&
      !empty($_POST[$this->conditional_field]))
    {
        foreach($this->arr_conditional_receipients as $condn => $rec)
        {
            if(strcasecmp($condn,$_POST[$this->conditional_field])==0 &&
            !empty($rec))
            {
                $this->AddRecipient($rec);
            }
        }
    }
}


function IsInternalVariable($varname)
{
    $arr_interanl_vars = array('scaptcha',
                        'submitted',
                        $this->GetSpamTrapInputName(),
                        $this->GetFormIDInputName()
                        );
    if(in_array($varname,$arr_interanl_vars))
    {
        return true;
    }
    return false;
}

function FormSubmissionToMail()
{
    $ret_str='';
    foreach($_POST as $key=>$value)
    {
        if(!$this->IsInternalVariable($key))
        {
            $value = htmlentities($value,ENT_QUOTES,"UTF-8");
            $value = nl2br($value);
            $key = ucfirst($key);
            $ret_str .= "<div class='label'>$key :</div><div class='value'>$value </div>\n";
        }
    }
    foreach($this->fileupload_fields as $upload_field)
    {
        $field_name = $upload_field["name"];
        if(!$this->IsFileUploaded($field_name))
        {
            continue;
        }        

        $filename = basename($_FILES[$field_name]['name']);

        $ret_str .= "<div class='label'>File upload '$field_name' :</div><div class='value'>$filename </div>\n";
    }
    return $ret_str;
}

function ExtraInfoToMail()
{
    $ret_str='';

    $ip = $_SERVER['REMOTE_ADDR'];
    $ret_str = "<div class='label'>IP address of the submitter:</div><div class='value'>$ip</div>\n";

    return $ret_str;
}

function GetMailStyle()
{
    $retstr = "\n<style>".
    "body,.label,.value { font-family:Arial,Verdana; } ".
    ".label {font-weight:bold; margin-top:5px; font-size:1em; color:#333;} ".
    ".value {margin-bottom:15px;font-size:0.8em;padding-left:5px;} ".
    "</style>\n";

    return $retstr;
}
function GetHTMLHeaderPart()
{
     $retstr = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">'."\n".
               '<html><head><title></title>'.
               '<meta http-equiv=Content-Type content="text/html; charset=utf-8">';
     $retstr .= $this->GetMailStyle();
     $retstr .= '</head><body>';
     return $retstr;
}
function GetHTMLFooterPart()
{
    $retstr ='</body></html>';
    return $retstr ;
}
function ComposeFormtoEmail()
{
    $header = $this->GetHTMLHeaderPart();
    $formsubmission = $this->FormSubmissionToMail();
    $extra_info = $this->ExtraInfoToMail();
    $footer = $this->GetHTMLFooterPart();

    $message = $header."Submission details:<p>$formsubmission</p><hr/>$extra_info".$footer;

    return $message;
}

function AttachFiles()
{
    foreach($this->fileupload_fields as $upld_field)
    {
        $field_name = $upld_field["name"];
        if(!$this->IsFileUploaded($field_name))
        {
            continue;
        }

        $filename =basename($_FILES[$field_name]['name']);

        $this->mailer->AddAttachment($_FILES[$field_name]["tmp_name"],$filename);
    }
}

function GetFromAddress()
{
    if(!empty($this->from_address))
    {
        return $this->from_address;
    }

    $host = $_SERVER['SERVER_NAME'];

    $from ="nobody@$host";
    return $from;
}

function Validate()
{
    $ret = true;

    if(empty($_POST[$this->GetFormIDInputName()]) ||
      $_POST[$this->GetFormIDInputName()] != $this->GetFormIDInputValue() )
    {

        $this->add_error("Automated submission prevention: case 1 failed");
        $ret = false;
    }


    if(!empty($_POST[$this->GetSpamTrapInputName()]) )
    {

        $this->add_error("Automated submission prevention: case 2 failed");
        $ret = false;
    }


    if(empty($_POST['name']))
    {
        $this->add_error("Please provide your name");
        $ret = false;
    }
    else
    if(strlen($_POST['name'])>50)
    {
        $this->add_error("Name is too big!");
        $ret = false;
    }


    if(empty($_POST['email']))
    {
        $this->add_error("Please provide your email address");
        $ret = false;
    }
    else
    if(strlen($_POST['email'])>50)
    {
        $this->add_error("Email address is too big!");
        $ret = false;
    }
    else
    if(!$this->validate_email($_POST['email']))
    {
        $this->add_error("Please provide a valid email address");
        $ret = false;
    }


    if(strlen($_POST['message'])>2048)
    {
        $this->add_error("Message is too big!");
        $ret = false;
    }


    if(isset($this->captcha_handler))
    {
        if(!$this->captcha_handler->Validate())
        {
            $this->add_error($this->captcha_handler->GetError());
            $ret = false;
        }
    }

    if(!empty($this->fileupload_fields))
    {
     if(!$this->ValidateFileUploads())
     {
        $ret = false;
     }
    }
    return $ret;
}

function ValidateFileType($field_name,$valid_filetypes)
{
    $ret=true;
    $info = pathinfo($_FILES[$field_name]['name']);
    $extn = $info['extension'];
    $extn = strtolower($extn);

    $arr_valid_filetypes= explode(',',$valid_filetypes);
    if(!in_array($extn,$arr_valid_filetypes))
    {
        $this->add_error("Valid file types are: $valid_filetypes");
        $ret=false;
    }
    return $ret;
}

function ValidateFileSize($field_name,$max_size)
{
    $size_of_uploaded_file =
            $_FILES[$field_name]["size"]/2048;//size in KBs
    if($size_of_uploaded_file > $max_size)
    {
        $this->add_error("The file is too big. File size should be less than $max_size KB");
        return false;
    }
    return true;
}

function IsFileUploaded($field_name)
{
    if(empty($_FILES[$field_name]['name']))
    {
        return false;
    }
    if(!is_uploaded_file($_FILES[$field_name]['tmp_name']))
    {
        return false;
    }
    return true;
}
function ValidateFileUploads()
{
    $ret=true;
    foreach($this->fileupload_fields as $upld_field)
    {
        $field_name = $upld_field["name"];

        $valid_filetypes = $upld_field["file_types"];

        if(!$this->IsFileUploaded($field_name))
        {
            continue;
        }

        if($_FILES[$field_name]["error"] != 0)
        {
            $this->add_error("Error in file upload; Error code:".$_FILES[$field_name]["error"]);
            $ret=false;
        }

        if(!empty($valid_filetypes) &&
         !$this->ValidateFileType($field_name,$valid_filetypes))
        {
            $ret=false;
        }

        if(!empty($upld_field["maxsize"]) &&
        $upld_field["maxsize"]>0)
        {
            if(!$this->ValidateFileSize($field_name,$upld_field["maxsize"]))
            {
                $ret=false;
            }
        }

    }
    return $ret;
}

function StripSlashes($str)
{
    if(get_magic_quotes_gpc())
    {
        $str = stripslashes($str);
    }
    return $str;
}

function Sanitize($str,$remove_nl=true)
{
    $str = $this->StripSlashes($str);

    if($remove_nl)
    {
        $injections = array('/(\n+)/i',
            '/(\r+)/i',
            '/(\t+)/i',
            '/(%0A+)/i',
            '/(%0D+)/i',
            '/(%08+)/i',
            '/(%09+)/i'
            );
        $str = preg_replace($injections,'',$str);
    }

    return $str;
}

function CollectData()
{
    $this->name = $this->Sanitize($_POST['name']);
    $this->email = $this->Sanitize($_POST['email']);
    $this->message = $this->StripSlashes($_POST['message']);
}

function add_error($error)
{
    array_push($this->errors,$error);
}
function validate_email($email)
{
    return eregi("^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$", $email);
}

function GetKey()
{
    return $this->form_random_key.$_SERVER['SERVER_NAME'].$_SERVER['REMOTE_ADDR'];
}

}

?>

What I do for multiple file/pictures upload is to use an IFRAME for a hidden form, with a file input that would get triggered once you press a button that you can include in your original form. The form is submitted in the iframe (therefore doesn't reload your page) and uploads the files, you can get files' details and add them with javascript to your form (ex. pic1.jpg uploaded, pic2.png uploaded...) I have a code sample, but I'll have to look for it a bit, so try and come up with the code yourself following this strategy.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM