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

1

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>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>INICIO - DARKTEAM</title>
    <style>
        body {
            font-family:Arial;
            background-color: #f2f2f2;
        }
        .header{
            padding: 80px;
            text-align: center;
            background: #1abc9c;
            color: white;   
        }
        .header h1{
            font-size: 40px;
        }
        .navbar{
            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;
        }
        input[type=submit]{
            background-color: #4caf50;
            color: white;
            padding: 12px 20px;
            border: none;
            cursor: pointer;
        }
        input[type=submit]:hover{
            background-color: #45a049;
        }
        .container{
            border-radius: 5px;
            padding: 10px;
        }
        .column{
            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;
            }
        }
    </style>
</head>

<body>
    <div class="header">
        <h1>DARKTEAM</h1>
    </div>
    <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>
    <div class="container">
                <div style="text-align=center">
                    <h2>CONTACTANOS</h2>
                </div>
            </div>
    <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">
            </form>
        </div>
        <div class="side">

        </div>

    </div>
     <div class="footer">
        <h2>DARKTEAM.EPIZY.COM</h2>
     </div> 
</body>
</html>

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

<?php

date_default_timezone_set('Etc/UTC');

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

$mail = new PHPMailer;
$mail->isSMTP();

/*
 * Server Configuration
 */

$mail->Host = 'smtp.gmail.com'; // 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";
$mail->MsgHTML($bodyresponse);

// ... or send an email with HTML.
//$mail->msgHTML(file_get_contents('contents.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()) {
    echo "MENSAJE ENVIADO CORRECTAMENTE :D";
} 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
source

2 answers

2

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

<?php	
	require_once('classes/class.phpmailer.php');
	require_once("classes/class.smtp.php");

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

	// AGREGAR TUS DATOS DE CORREO 
	$mail->SMTPSecure = 'tls';
	$mail->Host       = 'smtp.gmail.com';
	$mail->Port       = '587';
	$mail->Username   = '[email protected]';
	$mail->Password   = '***';
			
	// ------ CONFIGURACIÓN DEL MAIL DE AUTORESPUESTA  -----------
	$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->MsgHTML($bodyresponse);
	$mail->ClearAddresses();
	$mail->AddAddress('[email protected]');	

	if(!$mail->Send()){
		echo "Mailer Error: ".  $mail->errorInfo;
	}
	else{
		echo "Mail Enviado";
	}
?>
    
answered by 14.03.2018 / 07:57
source
0

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