How can I improve my php form to receive the entire message from my html form? (php form to email)

How can I improve my php form to receive the entire message from my html form? (php form to email)

Problem Description:

I tried to solved by myself reading how php form works and I haven’t arrived to an optimal solution. I still do not recive the mail with the user information.

// My php file named: sendbymail.php

//Importar variables

@$name = addslashes($_POST['name']);
@$ci = addslashes($_POST['ci']);
@$phone = addslashes($_POST['phone']);
@$Atype = addslashes($_POST['Atype']);
@$dept = addslashes($_POST['dept']);
@$municipality = addslashes($_POST['municipality']);
@$zone = addslashes($_POST['zone']);
@$uv = addslashes($_POST['uv']);
@$mza = addslashes($_POST['mza']);
@$lote = addslashes($_POST['lote']);
@$urb = addslashes($_POST['urb']);
@$street = addslashes($_POST['street']);
@$bank = addslashes($_POST['bank']);
@$branch = addslashes($_POST['branch']);
@$officer = addslashes($_POST['officer']);
@$oemail = addslashes($_POST['oemail']);

$to ="[email protected]";
$subject="Contacto desde el Sitio Web";
$message = "$name ha enviado un mensaje desde la web www.mywebsite.comn"
. "n"
. "Nombre: $namen"
. "Apellido: $cin"
. "Empresa: $phonen"
. "Email: $Atypen"
. "Departamento: $deptn"
. "Mensaje: $municipalityn"
. "Mensaje: $zonen"
. "Mensaje: $uvn"
. "Mensaje: $mzan"
. "Mensaje: $loten"
. "Mensaje: $urbn"
. "Mensaje: $streetn"
. "Mensaje: $bankn"
. "Mensaje: $branchn"
. "Mensaje: $officern"
. "Mensaje: $oemailn"
. "n";
$headers = "From $oemailn"
. "Reply-To: $oemailn";
$headers = "Content-type:text/html;charset=UTF-8" . "rn";
$headers = "MIME-Version: 1.0" . "rn";

mail($to, $subject, $message, $headers);

// The html form. I used the post method, from index.html

 <form action="sendbyemail.php" method="post" id="reused_form">

                <!-- Soliciante -->
                    <legend>Datos del solicitante</legend>
                    <!-- Persona Natural -->
                    <div id="natural1">
                        <label for="name">Nombre(s) y Apellido(s) / Razon Social:</label>
                        <input type="text" id="name" name="name" required>
                        <label for="ci">C.I. / N.I.T.:</label>
                        <input type="text" id="ci" inputmode="numeric" name="ci" required>
                        <label for="phone">Teléfono de contacto</label>
                        <input type="text" id="phone" inputmode="numeric" name="phone" required>
                <!-- T. Valuacion -->
                    <legend>Valuacion requerida</legend>
                    <label for="Atype">Tipo de Avaluo:</label>
                    <select id="Atype" name="Atype">
                        <option value="empty"></option>
                        <option value="Departamento">P. Horizontal</option>
                        <option value="Inmueble">Planta/Galpon Industrial</option>
                        <option value="Inmueble">Maquinaria industrial</option>
                        <option value="Inmueble">Vehiculos - Maq. Pesada</option>
                        <option value="Inmueble">P. rural</option>
                        <option value="Inmueble">Terreno</option>
                        <option value="Inmueble">Vivienda</option>
                        <option value="Inmueble">Obra civil</option>
                        <option value="Inmueble">Otros</option>

                <!-- Datos de Ubicacion -->
                    <legend>Ubicación del bien</legend>
                    <!-- Departamento -->
                    <label for="dept">Departamento:</label>
                    <select id="dept" name="dept" required>
                        <option value="empty"></option>
                        <option value="beni">Beni</option>
                        <option value="chuquisaca">Chuquisaca</option>
                        <option value="cochabamba">Cochabamba</option>
                        <option value="laPaz">La Paz</option>
                        <option value="oruro">Oruro</option>
                        <option value="pando">Pando</option>
                        <option value="potosi">Potosí</option>
                        <option value="santaCruz">Santa Cruz</option>
                        <option value="tarija">Tarija</option>
                    <!-- Municipio -->
                    <!-- This must be a select option and the options should only appear to the corresponding departament -->
                    <label for="municipality">Municipio:</label>
                    <input type="text" id="municipality" name="municipality" required>
                    <!-- Zona -->
                    <label for="zone">Zona:</label>
                    <input type="text" id="zone" name="zone" required>
                    <!-- Unidad Vecinal -->
                    <label for="uv">U.V.:</label>
                    <input type="text" id="uv" name="uv" required>
                    <!-- Manzano -->
                    <label for="mza">Manazana:</label>
                    <input type="text" id="mza" name="mza" required>
                    <!-- Lote -->
                    <label for="lote">Lote:</label>
                    <input type="text" id="lote" name="lote" required>
                    <!-- Urbanizacion -->
                    <label for="urb">Urbanización:</label>
                    <input type="text" id="urb" name="urb" required>
                    <!-- Avenida o Calle -->
                    <label for="street">Avenida o Calle, Nro:</label>
                    <input type="text" id="street" name="street" required>
                <!-- Entidad Financiera -->
                    <legend>Entidad Financiera</legend>
                    <!-- Entidad -->
                    <label for="bank">Entidad:</label>
                    <select id="bank"name="bank">
                        <option value="empty"></option>
                        <option value="bdr">Banco de Desarrollo Productivo S.A.M</option>
                        <option value="bcr">Banco de Crédito de Bolivia S.A.</option>
                        <option value="bec">Banco Económico S.A.</option>
                        <option value="bfs">Banco Fassil S.A</option>
                        <option value="bfo">Banco Fortaleza S.A.</option>
                        <option value="bpr">Banco Prodem S.A.</option>
                        <option value="bun">Banco Union S.A.</option>
                        <option value="pco">Banco Pyme de la Comunidad S.A.</option>
                        <option value="ici">CIDRE IFD</option>
                        <option value="icr">CRECER IFD</option>
                    <!-- Agencia -->
                    <label for="branch">Agencia o Sucursal:</label>
                    <input type="text" id="branch" name="branch">
                    <!-- Oficial de Credito -->
                    <label for="officer">Oficial de Crédito:</label>
                    <input type="text" id="officer" name="officer">
                    <!-- Correo Electrónico (del oficial de credito) -->
                    <label for="oemail">Correo Electrónico:</label>
                    <input type="email" id="oemail" name="oemail">

                    <!-- Copia de correo al usario -->
                    <input type="checkbox" name="emailcopy" id="emailcopy" class="sr-only">
                    <label for="emailcopy" class="sr-only">Deseo recibir una copia de la solicitud</label>      

                <div id="tocs">
                    <input type="checkbox" name-="tocs" id="tocs" required>
                    <label for="tocs">He leido y acepto la <a href="tocs.html" class="lnk">Política de Privacidad</a> y los <a href="tocs.html" class="lnk"> <i>Términos y Condiciones</i></a> de uso</label>

                <!-- Captcha / enviar / reset -->
                <div class="flex-layout01">
                    <div class="sr-only">Captcha</div>     
                    <input type="submit" value="Enviar" class="btn btn-1 buzz-out-on-hover">
                    <button type="reset" class="btn btn-2 buzz-out-on-hover">Reset</button>

I tried to solved by myself reading how php form works and I haven’t arrived to an optimal solution. I still do not recive the mail with the user information.

Solution – 1

  1. Make sure that the form is properly configured to send data to the PHP script.

  2. Validate the data sent by the form and make sure all the required fields are present.

  3. Use the $_POST array to access the data sent by the form.

  4. Use the filter_var() function to validate the data.

  5. Use the mail() function to send the message.

  6. Use error handling to check for any errors in the form submission.

  7. Use the stripslashes() function to remove any unwanted characters from the message.

  8. Use the htmlspecialchars() function to prevent any malicious code from being sent with the message.

  9. Use the wordwrap() function to format the message for better readability.

  10. Set the correct content type for the email using the header() function.

Rate this post
We use cookies in order to give you the best possible experience on our website. By continuing to use this site, you agree to our use of cookies.