Home > Software design >  PHP contact form - attachment limit problem, additional field, reset form after sending
PHP contact form - attachment limit problem, additional field, reset form after sending

Time:11-26

I'm using "jQuery Contact Form with Attachment using PHP" from PHPPOT website but I need your help with mentioned problems I have.

First, I would like to increase attachment limit which is now 2 MB. I asked my ISP to set new parameters in php.ini but they've said that my limit is already 32 MB. Unfortunately, when I try to send the file bigger than 2 MB I've got message "Could not access file:" and receive the mail without attachment.

Second thing, I would like to add additional filed "Telephone number" to my contact form and I've done everything I have conclude logically (just to remark that I'm PHP noob :( ) but I cannot make this info to be part of my e-mail.

And the last thing...how can I force the form to be reset after successful sending?

Thanks a lot!!!

Below are the PHP files and you can se my form here: https://test.arhviz.rs/Test.html

index.php

    <style>
body{width:100%;}
#frmContact {border-top:#F0F0F0 0px solid;background:rgba(250,248,248,0.00);padding:10px;}
#frmContact div{margin-bottom: 15px}
#frmContact div label{margin-left: 5px}
.demoInputBox{padding:10px; border:#27aae1 0px solid; border-radius:4px;background-color:#C6C6C6;width:100%;}
.error{background-color: #FF5A5A;border:#AA4502 0px solid;padding: 5px 10px;color: #FFFFFF;border-radius:4px;}
.success{background-color: #27AAE1;border:#0FA015 0px solid;padding: 5px 10px;color: #FFFFFF;border-radius:4px;}
.info{font-size:.8em;color: #FF5A5A;letter-spacing:2px;padding-left:5px;}
.btnAction{background-color:#27AAE1;border:0;padding:10px 40px;color:#FFF;border:#F0F0F0 0px solid; border-radius:4px;}
</style>
<script src="https://code.jquery.com/jquery-2.1.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function (e){
$("#frmContact").on('submit',(function(e){
    e.preventDefault();
    $('#loader-icon').show();
    var valid;  
    valid = validateContact();
    if(valid) {
        $.ajax({
        url: "contact_mail.php",
        type: "POST",
        data:  new FormData(this),
        contentType: false,
        cache: false,
        processData:false,
        success: function(data){
        $("#mail-status").html(data);
        $('#loader-icon').hide();
        },
        error: function(){}             
        
        });
    }
}));

function validateContact() {
    var valid = true;   
    $(".demoInputBox").css('background-color','');
    $(".info").html('');
    
    if(!$("#userName").val()) {
        $("#userName-info").html("(required)");
        $("#userName").css('background-color','#FF5A5A');
        valid = false;
    }
    if(!$("#userEmail").val()) {
        $("#userEmail-info").html("(required)");
        $("#userEmail").css('background-color','#FF5A5A');
        valid = false;
    }
    if(!$("#userEmail").val().match(/^([\w-\.] @([\w-] \.) [\w-]{2,4})?$/)) {
        $("#userEmail-info").html("(invalid)");
        $("#userEmail").css('background-color','#FF5A5A');
        valid = false;
    }
    if(!$("#userPhone").val().match(/[0-9]/)) {
        $("#userPhone-info").html("(invalid)");
        $("#userPhone").css('background-color','#FF5A5A');
        valid = false;
    }
    if(!$("#subject").val()) {
        $("#subject-info").html("(required)");
        $("#subject").css('background-color','#FF5A5A');
        valid = false;
    }
    if(!$("#content").val()) {
        $("#content-info").html("(required)");
        $("#content").css('background-color','#FF5A5A');
        valid = false;
    }
    
    return valid;
}

});
</script>
<form id="frmContact" action="" method="post">
<div id="mail-status"></div>
<div>
<label style="padding-top:20px;">Ime</label>
<span id="userName-info" class="info"></span><br/>
<input type="text" name="userName" id="userName" class="demoInputBox">
</div>
<div>
<label>E-mail</label>
<span id="userEmail-info" class="info"></span><br/>
<input type="text" name="userEmail" id="userEmail" class="demoInputBox">
</div>
<div>
<label>Telefon</label>
<span id="userPhone-info" class="info"></span><br/>
<input type="text" name="userPhone" id="userPhone" class="demoInputBox">
</div>
<div>
<label>Pošaljite Vaš projekat (DWG, DXF ili PDF):</label><br/>
<input type="file" name="attachmentFile" id="attachmentFile" class="demoInputBox">
</div>
<div>
<label>Tema</label> 
<span id="subject-info" class="info"></span><br/>
<input type="text" name="subject" id="subject" class="demoInputBox">
</div>
<div>
<label>Poruka</label> 
<span id="content-info" class="info"></span><br/>
<textarea name="content" id="content" class="demoInputBox" cols="60" rows="6"></textarea>
</div>
<div>
<input type="submit" value="Send" class="btnAction" />
</div>
</form>
<div id="loader-icon" style="display:none;"><img src="LoaderIcon.gif" /></div>

contact_mail.php

    <?php
require('phpmailer/class.phpmailer.php');

$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 0;
$mail->SMTPAuth = TRUE;
$mail->SMTPSecure = "ssl";
$mail->Port     = 465;  
$mail->Username = "[email protected]";
$mail->Password = "*****";
$mail->Host     = "mail.arhviz.rs";
$mail->Mailer   = "smtp";
$mail->SetFrom($_POST["userEmail"], $_POST["userName"]);
$mail->AddReplyTo($_POST["userEmail"], $_POST["userName"]);
$mail->AddAddress("[email protected]");  
$mail->Subject = $_POST["subject"];
$mail->WordWrap   = 80;
$mail->MsgHTML($_POST["content"]);

if(is_array($_FILES)) {
$mail->AddAttachment($_FILES['attachmentFile']['tmp_name'],$_FILES['attachmentFile']['name']); 
}

$mail->IsHTML(true);

if(!$mail->Send()) {
    echo "<p class='error'>Došlo je do greške pri slanju poruke. Molim, pokušajte kasnije ili pošaljite mail direktno na [email protected]. Hvala!.</p>";
} else {
    echo "<p class='success'>Poruka je uspešno poslata. Potrudiću se da Vam odgovorima u najkraćem roku. Hvala.</p>";
}   
?>

There are also three files in folder phpmailer but I think they are just configuration files for connection with server.

CodePudding user response:

I am pretty sure that in these two lines...

$mail->MsgHTML($_POST["userPhone"]);
$mail->MsgHTML($_POST["content"]);

...you first set the phone number as mail content and then override it with the actual message. Try it like this:

$mail->MsgHTML( 'Phone: ' . $_POST["userPhone"] . "\nMessage: " . $_POST["content"] );

As for resetting the form: You seem to be sending the E-Mail via an AJAX request. There is already a function in your index.php code that runs after the successfull sending of the E-Mail:

success: function(data){
    $("#mail-status").html(data);
    $('#loader-icon').hide();
}

That's the point where you could reset the form, e.g. like this:

success: function(data){
    $("#mail-status").html(data);
    $('#loader-icon').hide();
    $('#frmContact').trigger("reset");
}

Update: I tried to make it clearer, where the reset call needs to go. If it's part of the success function, it should only happen, if sending the mail was successful.

Update 2: A first step should be to check that the values of the file limitations are what you think they are. You can output the values like these:

<?php
echo "<!-- upload_max_filesize=" . ini_get('upload_max_filesize') . ", post_max_size=" . ini_get('post_max_size') . "-->";

Put that at the end of the index.php file, after the loader icon. Then refresh the website and have a look at the source code of the page. Once the values are what you expect them to be, remove it again.

  • Related