Send email with PHP [closed]

-4

I'm not sure what I'm doing here, but I'm not sure what I'm going to do.

Here's the code I'm using:

if (isset($_POST["recovery_pass"]))
{
  if (filter_var($_POST["email"], FILTER_VALIDATE_EMAIL))
  {
    $email = $_POST["email"];
  }
  else
  {
    echo "email is not valid";
        exit;
    }

  // Check to see if a user exists with this e-mail
    $query = $conexao->prepare('SELECT email FROM users WHERE email = :email');
    $query->bindParam(':email', $email);
    $query->execute();
    $userExists = $query->fetch(PDO::FETCH_ASSOC);
    $conexao = null;

    if ($userExists["email"])
    {
        // Create a unique salt. This will never leave PHP unencrypted.
        $salt = "498#2D83B631%3800EBD!801600D*7E3CC13";

        // Create the unique user password reset key
        $password = hash('sha512', $salt.$userExists["email"]);

        // Create a url which we will direct them to reset their password
        $pwrurl = "$linkSite/recovery_pass/$password";

        require ("phpmailer/PHPMailerAutoload.php");
        // Inicia a classe PHPMailer
        $mail = new PHPMailer;

        $mail->isSMTP();                                      // Set mailer to use SMTP
        $mail->Host = 'smtp.gmail.com';  // Specify main and backup SMTP servers
        $mail->SMTPAuth = true;                               // Enable SMTP authentication
        $mail->Username = '[email protected]';                 // SMTP username
        $mail->Password = '';                           // SMTP password
        $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, 'ssl' also accepted
        $mail->Port = 587;

        // Define o remetente
        // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
        $mail->setFrom = ('[email protected]'); // Seu e-mail

        // Define os destinatário(s)
        // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
        $mail->AddAddress($email);

        $mail->IsHTML(true); // Define que o e-mail será enviado como HTML
        //$mail->CharSet = 'iso-8859-1'; // Charset da mensagem (opcional)
        $mail->Subject = "Recuperação de senha"; // Assunto da mensagem
        $mail->Body = "Este é o corpo da mensagem de teste, em ".$pwrurl." <b>HTML</b>!  :)";
        $mail->AltBody = "Este é o corpo da mensagem de teste, em Texto Plano! \r\n :)";

        // Envia o e-mail
        $enviado = $mail->Send();

        // Limpa os destinatários e os anexos
        $mail->ClearAllRecipients();

        // Exibe uma mensagem de resultado
        if ($enviado) {
            echo "E-mail enviado com sucesso!";
        } else {
            echo "Não foi possível enviar o e-mail.";
            echo "<b>Informações do erro:</b> " . $mail->ErrorInfo;
        }
        }
        else
        echo "No user with that e-mail address exists.";
        }
?>

<form action="#" method="post" enctype="multipart/form-data" class="form-horizontal">

<fieldset class="form-group">
    <label for="email">Email</label>
    <input type="email" class="form-control" name="email" placeholder="Email" maxlength="70" size="70" required>
</fieldset>

<input type="submit" class="btn btn-primary" name="recovery_pass" value="Recovery">
    
asked by anonymous 22.01.2016 / 19:26

2 answers

1

I got it, the code below is working!

require 'phpmailer/PHPMailerAutoload.php';

        $mail = new PHPMailer;

        //$mail->SMTPDebug = 3;

        $mail->isSMTP();
        $mail->Host = 'smtp.live.com';
        $mail->SMTPAuth = true;
        $mail->Username = 'meuemailoutlook';
        $mail->Password = 'secret';
        $mail->SMTPSecure = 'tls';
        $mail->Port = 587;

        $mail->setFrom('meuemailoutlook', '5tars');
        $mail->addAddress($email);

        $mail->isHTML(true);
        $mail->CharSet = 'iso-8859-1';

        $mail->Subject = 'Recuperação de senha';
        $mail->Body    = 'Este é o corpo da mensagem de teste, em '.$pwrurl.' <b>HTML</b>!  :)';
        $mail->AltBody = 'Este é o corpo da mensagem de teste, em Texto Plano! \r\n :)';

        if(!$mail->send()) {
            echo 'Message could not be sent.';
            echo 'Mailer Error: ' . $mail->ErrorInfo;
        } else {
            echo 'Message has been sent';
        }
    
23.01.2016 / 19:16
1

Your problem seems to be because you have not set up php.ini. search on file by [mail function] ; For Win32 only. SMTP = localhost smtp_port = 25

In SMTP = you indicate the SMTP server that you will use to send the message.

This link can help: link

    
22.01.2016 / 21:13