17 March, 2018 steades

Create a visit form and send it through a form by email from your website

PHP mail function

If you need to send the collected data through an HTML form via email, you can find the php mail function useful.

We create a php document (email.php) which will be responsible for sending. We define the different variables responsible for collecting the form data, for example $ variable = $ _POST [field-name].

Suppose we have created a contact form, consisting of the following fields: name, surnames, telephone, email, subject and message; we create the variables:

  • $nombre = $_POST[nombre];
  • $apellidos = $_POST[apellidos];
  • $telefono = $_POST[telefono];
  • $email = $_POST[email];
  • $asunto = $_POST[asunto];
  • $mensaje = nl2br($_POST[mensaje]); Con esta variable mantenemos los párrafos tal y como los escribió el usuario, respetando los saltos de línea.
We build the body of the message in plain text or HTML:

  • $message

And finally we define the recipient, subject and sender.

$ email
$ subject
$ headers

Next, you can see an example of how our mail function would look like:

<? php

$ name = $ _POST [name];
$ last name = $ _POST [last name];
$ telephone = $ _POST [telephone];
$ email = $ _POST [email];
$ subject = $ _POST [subject];
$ message = nl2br ($ _ POST [message]);
$ message =

“<font face = Arial, Helvetica, sans-serif size = 2 em>

<p> Hello Administrator, </ p>

<p> A user has sent a new message, with the following details: </ p>

<p> <b> Full name: </ b> $ name $ surnames <br />
<b> Contact information: </ b> $ phone / <a href=mailto:$email> $ email </a> </ p>

<p> $ message </ p>

</ font> “;

$ email = ‘recipient@email.es’;
$ subject = $ _POST [subject];
$ headers = “From: sender@email.es \ r \ nContent-type: text / html \ r \ n”;

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

?>

Do not forget to define “method” and “action” in your HTML form.

<form id = “contact-form” name = “contact-form” method = “post” action = “email.php”>

 

Get Connected.

We are always looking for new exciting projects and collaborations. Feel free to contact us.