confirmation of registration via e-mail

Asked

Viewed 1,391 times

3

I’m setting up a registration system and only need the confirmation part in the email to finish and I’m not able to do this part. I’ve researched tutorials on youtube and tried to apply them to my code, only it doesn’t work, someone could 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 = "[email protected]";
                $email = $_POST['email'];
                mail($email, $assunto, $mensagem, $headers);

            }
        }
    }


?>
  • What is the problem you are facing? Your application displays some error when code runs?

  • In case of the problem, I followed this simple tutorial: https://www.youtube.com/watch?v=CqddjTQjJXA&t=906s .

  • If you have a video tutorial or better instructions, please let me know, I’m in great need and I want to learn how to do this.

  • If the problem is no in the email sending code, try Phpmailer http://answall.com/questions/177812/envio-de-e-mail-com-fun%C3%A7%C3%A3o-mail/177832#177832

  • Check this http://answall.com/questions/40858/como-mail-do-localhost-usando-a-fun%C3%A7%C3%A3o-mail-do-php/40861#40861

  • First you need the following, is your SMTP server active and authenticated with your pop server? example, you can try the Mailgun before sending it to the online server, in which case if it works with Mailgun it might be your server.

  • Think about using some framework that can help you a lot at this point, one that makes it very easy to send emails is Laravel

Show 2 more comments

2 answers

1

I took this good example from the official PHP site:

<?php
$to      = '[email protected]';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: [email protected]' . "\r\n" .
    'Reply-To: [email protected]' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

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

This second example is more elegant:

<?php
// Multiple recipients
$to = '[email protected], [email protected]'; // 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 <[email protected]>, Kelly <[email protected]>';
$headers[] = 'From: Birthday Reminder <[email protected]>';
$headers[] = 'Cc: [email protected]';
$headers[] = 'Bcc: [email protected]';

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

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

  • This answer is correct on the PHP side, just don’t forget that there should be an email server running on the same server to work. PHP does not send e-mail directly to the destination, there needs to be an email server that effectively delivered. In addition, it is problematic to deliver emails directly to destination today due to anti-spam controls. The ideal is to hire an account in an email service to use as a smarthost, which implies to set up the e-mail server right.

-1

Forget the mail function, use Phpmailer Phpmailer

following example

require 'PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer();
$mail->isSMTP();
$mail->SMTPDebug =2;// 0,1 ou 2
$mail->Debugoutput = 'html';
$mail->Host = "smtp.gmail.com";
$mail->Port = 587;
$mail->SMTPAuth = true;
$mail->SMTPSecure = "tls";
$mail->Username = "[email protected]";
$mail->Password = "senha";
$mail->CharSet = 'UTF-8';
$mail->setFrom('[email protected]', 'Informativo');
$mail->addAddress('[email protected]', 'algum texto');
$mail->addAddress('[email protected]', 'algum texto');
$mail->addAddress('[email protected]', 'algum texto');
$mail->Subject = 'assunto do email';

$mensagem = 'Teste teste teste ';
$mensagem .= 'outro teste';

$mail->msgHTML($mensagem);

$mail->AltBody = 'Caso você esteja lendo essa mensagem é provável que seu cliente de email não tenha suporte a HTML';
if (!$mail->send()){
    echo "email NÃO enviado";
}else {
    echo "email enviado com sucesso";
}
  • 2

    For what reason, if the Phpmailer library itself uses the function mail?

  • in my opinion: more elaborate error handling, simple document attachment, inclusion of several emails in an easier way

  • 1

    So put that in your answer to improve it and actually create an answer.

  • edited my answer, blz?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.