How do I make the HTML form send email from PHP or something similar


It turns out that I'm making a website and I do not know how to send a php from the html form to send it to my email because it does not let me (or if there is an easier way, without php or something) I do not give any error but I do not send the email directly, I would appreciate it if you helped me: D I use infinityfree to host the website. Here I leave the codes:

HTML code of the Form

<!doctype html>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>INICIO - DARKTEAM</title>
        body {
            background-color: #f2f2f2;
            padding: 80px;
            text-align: center;
            background: #1abc9c;
            color: white;   
        .header h1{
            font-size: 40px;
            overflow: hidden;
            background-color: #333;
        .navbar a{
            float: left;
            display: block;
            color: white;
            text-align: center;
            padding: 14px 20px;
            text-decoration: none;
        .navbar a.right {
            float: right; 
        .navbar a:hover {
            background-color: #ddd; 
            color: black; 
        .row {
            display: flex;
            flex-wrap: wrap;
        .side {
            flex: 30%; 
            background-color: #f1f1f1; 
            padding: 20px; 
        .main {
            flex: 70%;
            background-color: white; 
            padding: 20px; 
        @media screen and (max-width: 700px) {
            .row {
                flex-direction: column;
        @media screen and (max-width: 400px) {
            .navbar a {
                float: none;
                width: 100%;
        .footer {
            padding: 20px;
            text-align: center; 
            background: #ddd; 
        input[type=text], select, textarea{
            width: 100%;
            padding: 12px;
            border: 1px solid #ccc;
            margin-top: 6px;
            margin-bottom: 16px;
            resize: vertical;
            background-color: #4caf50;
            color: white;
            padding: 12px 20px;
            border: none;
            cursor: pointer;
            background-color: #45a049;
            border-radius: 5px;
            padding: 10px;
            float: left;
            width: 60%;
            margin-top: 6px;
            padding: 20px 60px 20px 15px;
            background-color: #f2f2f2;
            border-radius: 5px;
        .row:after {
            content: "";
            display: table;
            clear: both;
        @media screen and (max-width: 600px) {
            .column, input[type=submit] {
                width: 100%;
                margin-top: 0;

    <div class="header">
    <div class="navbar">
        <a href="/">LINK</a>
        <a href="/info.html">LINK</a>
        <a href="/contact.html">LINK</a>
        <a href="/" class="right">LINK</a>
    <div class="container">
                <div style="text-align=center">
    <div class="row">
        <div class="column">
            <form action="/contactform.php">
                <label for="name">Nombre</label>
                <input type="text" id="name" name="name" placeholder="Tu nombre..." required>
                <label for="email">Email</label>
                <input type="text" id="email" name="email" placeholder="[email protected]" required>
                <label for="comment">Comentario</label>
                <textarea id="comment" name="comment" placeholder="Tu comentario..." style="height:170px" required></textarea>
                <input type="submit" value="Enviar">
        <div class="side">


     <div class="footer">

PHP code to which redirects the HTML when giving it to send



// Edit this path if PHPMailer is in a different location.
require './PHPMailer/PHPMailerAutoload.php';

$mail = new PHPMailer;

 * Server Configuration

$mail->Host = ''; // Which SMTP server to use.
$mail->Port = 587; // Which port to use, 587 is the default port for TLS security.
$mail->SMTPSecure = 'tls'; // Which security method to use. TLS is most secure.
$mail->SMTPAuth = true; // Whether you need to login. This is almost always required.
$mail->Username = "[email protected]"; // Your Gmail address.
$mail->Password = ""; // Your Gmail login password or App Specific Password.
$mail->SMTPDebug = 2;
 * Message Configuration

$mail->setFrom($email, $name); // Set the sender of the message.
$mail->addAddress('[email protected]', 'TheDarkDragon117'); // Set the recipient of the message.
$mail->Subject = 'FORMULARIO DE CONTACTO'; // The subject of the message.

 * Message Content - Choose simple text or HTML email

// Choose to send either a simple text email...
$name = $_POST['name'];
$email = $_POST['email'];
$comment = $_POST['comment'];
$bodyresponse = "Has recibido un Formulario de Contacto de $name - $email \n Aqui esta el comentario: \n $comment";

// ... or send an email with HTML.
// Optional when using HTML: Set an alternative plain text message for email clients who prefer that.
//$mail->AltBody = 'This is a plain-text message body'; 

// Optional: attach a file

if ($mail->send()) {
} else {
    echo "Mailer Error: " . $mail->ErrorInfo;

EDIT: Now I have another problem, I do not know how to make the tickets work (the $ name $ email $ comment) now I have managed to get it but in the parts where these tickets have to go it appears empty EDIT2: ALL ERRORS CORRECTED, NOW EVERYTHING WORKS WELL

asked by user9451723 12.03.2018 в 23:10

2 answers


You can use the phpmailer library, you only need to import the class.phpmailer.php and class.smtp.php files. Below I share an example configured with a gmail mail


	$mail = new PHPMailer();
	$mail->CharSet = 'UTF-8';
	$mail->SMTPDebug  = 0;
	$mail->SMTPAuth   = true;

	$mail->SMTPSecure = 'tls';
	$mail->Host       = '';
	$mail->Port       = '587';
	$mail->Username   = '[email protected]';
	$mail->Password   = '***';
	$mail->SetFrom($mail->Username, 'stackoverflow');
	$mail -> Subject = 'Información recibida de formulario';
	$bodyresponse = "Has recibido un formulario de $name Aqui esta el mensaje comment";
	$mail -> Subject = '¡Gracias por registrarte!';
	$mail->AddAddress('[email protected]');	

		echo "Mailer Error: ".  $mail->errorInfo;
		echo "Mail Enviado";
answered by 14.03.2018 / 07:57

Hello, I'll tell you in two parts:

1.- In your Html code, when you open your form, you need to specify the method to send the data, it will look something like this:

2.- In your php file, for the mail () method to work, you must have the sendmail library installed.   
-if you use ubuntu you can install sendmail with:
sudo apt-get install sendmail

answered by 14.03.2018 в 18:27