php – registration confirmation via email

Question:

I'm setting up a registration system and I just need the confirmation part of the email to finish and I'm not able to do this part. I've already searched for tutorials on youtube and tried to apply them to my code, but it doesn't work, could someone help me?

<?php

if(isset($_POST['cadastrar']) && $_POST ['cadastrar'] == "register")
{
    $nome = $_POST['nome'];
    $sobrenome = $_POST['sobrenome'];
    $data = $_POST['data'];
    $cpf = $_POST['cpf'];
    $email = $_POST['email'];
    $pais = $_POST['pais'];
    $estado = $_POST['estado'];
    $login = $_POST['login'];
    $senha = $_POST['senha'];
    $rsenha = $_POST['rsenha'];
    $image = $_FILES['image']['name'];

    if(empty($nome) || empty($sobrenome) || empty($data) || empty($cpf) || empty($email) ||  empty($pais) || empty($estado) || empty($login) || empty($senha) || empty($rsenha) || ($cpf_enviado == false) || (@$emailvalida == false))

        {

        }else{
            $query = "SELECT * FROM cadastro WHERE login = '$login'";
            $result = mysql_query($query);
            $conta = mysql_num_rows($result);
            $busca = mysql_fetch_assoc($result);

            if($conta > 0){
                echo '<div id="preencha" style="width:200px; position:relative; left:580px; top:-30px; color:#fff; font-size:15px; ">Usuário já cadastrado!</div>   ';
            }else{
                $cadastrar = "INSERT INTO cadastro (nome, sobrenome, data, cpf, email, pais, estado, login, senha, rsenha, image)
                             VALUES ('$nome', '$sobrenome', '$data', '$cpf', '$email', '$pais', '$estado', '$login', '$senha', '$rsenha', '$image')";
                if(mysql_query($cadastrar))
                {
                    $_SESSION['nome'] = $nome;
                    $_SESSION['sobrenome'] = $sobrenome;
                    $_SESSION['data'] = $data;
                    $_SESSION['cpf'] = $cpf;
                    $_SESSION['email'] = $email;
                    $_SESSION['pais'] = $pais;
                    $_SESSION['estado'] = $estado;
                    $_SESSION['login'] = $login;
                    $_SESSION['senha'] = $senha;    
                    $_SESSION['rsenha'] = $rsenha;
                    $_SESSION['image'] = $image;

                    echo "<script type=\"text/javascript\">window.setTimeout(\"location.href='cadastroRealizado.php';\");</script>";
                }
                else
                {
                    echo '<div id="preencha" style="width:200px; position:relative; left:580px; top:-30px; color:#fff; font-size:15px; ">Erro ao cadastrar!</div>';
                }

                $conexaoemail = mysql_query("SELECT * FROM cadastro WHERE nome = '$nome'");
                $resultado = mysql_fetch_array($conexaoemail);
                $id = $resultado['id'];

                $assunto = "Ative sua conta";
                $mensagem = "Ative sua conta clicando no link:";
                $headers = "equipe.weedinfo@gmail.com";
                $email = $_POST['email'];
                mail($email, $assunto, $mensagem, $headers);

            }
        }
    }


?>

Answer:

I took this good example from the official PHP site:

<?php
$to      = 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "\r\n" .
    'Reply-To: webmaster@example.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

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

This second example is more elegant:

<?php
// Multiple recipients
$to = 'johny@example.com, sally@example.com'; // note the comma

// Subject
$subject = 'Birthday Reminders for August';

// Message
$message = '
<html>
<head>
  <title>Birthday Reminders for August</title>
</head>
<body>
  <p>Here are the birthdays upcoming in August!</p>
  <table>
    <tr>
      <th>Person</th><th>Day</th><th>Month</th><th>Year</th>
    </tr>
    <tr>
      <td>Johny</td><td>10th</td><td>August</td><td>1970</td>
    </tr>
    <tr>
      <td>Sally</td><td>17th</td><td>August</td><td>1973</td>
    </tr>
  </table>
</body>
</html>
';

// To send HTML mail, the Content-type header must be set
$headers[] = 'MIME-Version: 1.0';
$headers[] = 'Content-type: text/html; charset=iso-8859-1';

// Additional headers
$headers[] = 'To: Mary <mary@example.com>, Kelly <kelly@example.com>';
$headers[] = 'From: Birthday Reminder <birthday@example.com>';
$headers[] = 'Cc: birthdayarchive@example.com';
$headers[] = 'Bcc: birthdaycheck@example.com';

// Mail it
mail($to, $subject, $message, implode("\r\n", $headers));
?>

Source: http://php.net/manual/en/function.mail.php

Scroll to Top