Ir para conteúdo
Fórum Script Brasil
  • 0

Validação de e-mails para de cadastro de newsletters


arnaldoydas

Pergunta

bom dia Amigos!

Tenho um sistema em php que cadastra e-mails num arquivo .txt. Até aí tudo Ok!

Funciona bem, mas não consigo validar o e-mail em hipótese alguma, tentei de tudo, mas não manjo nada de php, ainda sou novato.

Já tentei

$email = 'teste@meusite.com';

if(filter_var($email, FILTER_VALIDATE_EMAIL)){

echo 'E-mail válido';

}else{

echo 'E-mail invalido';

}

E também

function validaEmail($mail){

if(preg_match("/^([[:alnum:]_.-]){3,}@([[:lower:][:digit:]_.-]{3,})(\.[[:lower:]]{2,3})(\.[[:lower:]]{2})?$/", $mail)) {

return true;

}else{

return false;

}

}

Além disso, não consigo fazer o e-mail de confirmação funcionar quando digito o e-mail no input. Alguém poderia me dar uma luz?

Valeu!

Postei o código abaixo:

<?php

/* Verifica qual é o sistema operacional do servidor para ajustar o cabeçalho de forma correta. */

if(PATH_SEPARATOR == ";") $quebra_linha = "\r\n"; //Se for Windows

else $quebra_linha = "\n"; //Se "não for Windows"

########################################################################

# Global Settings...

########################################################################

# Set the password for the eMail List editor!

$pass="secret";

# Name of the datafile

$filelocation="subscribers.txt";

# Title of the newsletter, will be displayed in the FROM field of the mailclient

$lettername="eu@meudominio.com.br";

# Your email, will be the reply-to mail

$youremail="eu@meudominio.com.br";

########################################################################

# Displayed Messages

########################################################################

# Welcome message displayed above the form for subscribing/unsubscribing

$welcomemessage = "Gostaria de receber nosso Informativo?";

# Sorrymessage for failed subscription, will be followed by the email!

$sorrysignmessage = "Desculpe, já existe em nosso cadastro o e-mail ";

# Subscribe message, will be displayed when subscribing

$subscribemessage = "Obrigado por se cadastrar, um e-mail de confirmação foi enviado para ";

# Subscribemail, will be sent when someone subscribes.

$subscribemail = "Obrigado por se cadastrar em nosso Informativo";

# Unsubscribemessage for deletion, will be followed by the email!

$unsubscribemessage = "Foi apagado de nosso sistema o e-mail ";

# Unsubscribemessage for failed deletion, will be followed by the email!

$failedunsubscriptionmessage = "Desculpe, não consta em nosso banco de dados o e-mail ";

########################################################################

# Let the code begin...

########################################################################

# Checks if the file exists, if not creates a new one

if (!file_exists($filelocation)) {

$newfile = fopen($filelocation,"w+");

fclose($newfile);

}

# Open the datafile and read the content

$newfile = fopen($filelocation,"r");

$content = fread($newfile, filesize($filelocation));

fclose($newfile);

# Remove the slashes PHP automatically puts before special characters

$content=stripslashes($content);

# Reset the output of the "search result"

$out="";

# Put the entries into the array lines

$lines = explode("%",$content);

for ($key=1;$key<sizeof($lines);$key++){

# when the email is not in the list, add the old entries

if ($lines[$key] != $email){

$out .= "%".$lines[$key];

}

# when it's already in the list, set found

else {

$found=1;

}

}

########################################################################

# Signing in

########################################################################

if ($action=="sign"){

# When there is already a subscription for this email *duh*

if ($found==1){

# Display Sorrymessage

echo "<div align=\"left\">".$sorrysignmessage.$email."</div><br/>";

$disp="yes";

}

# otherwise, add the email to the list

else {

$disp="no";

$newfile = fopen($filelocation,"a+");

$add = "%".$email;

fwrite($newfile, $add);

fclose($newfile);

# display the message of subscription

echo "<div align=\"left\">".$subscribemessage.$email."</div><br/>";

# send confirmation Mail

$submailheaders = "From: $lettername subscription form";

$submailheaders .= "Reply-To: $youremail";

mail ($email,$lettername." subscription",$subscribemail,$submailheaders);

}

}

########################################################################

# Signing out

########################################################################

if ($action=="delete"){

$disp="no";

# If the email is in the list...

if ($found == 1){

$newfile = fopen($filelocation,"w+");

fwrite($newfile, $out);

fclose($newfile);

# display the message for deleted items...

echo "<div align=\"left\">".$unsubscribemessage.$email."</div><br/>";

$disp="no";

}

# if the email is not in the list

if ($found != 1){

# display the message that tells that...

echo "<div align=\"left\">".$failedunsubscriptionmessage.$email."</div><br/>";

$disp="yes";

}

}

########################################################################

# The core for the owner of the letter

########################################################################

if ($pw == $pass){

# When nothing was entered so far, display the form

if ($send != "yes" && $send != "test"){

print'<form method="post"><input type="hidden" name=pw value='.$pass.'><input type="hidden" name=send value=yes>

Cadastro de Informativo:</b><br/>

Assunto:<br/>

<input type="text" name="subject" size=20><br/>

Mensagem:<br/>

<textarea cols=50 rows=10 wrap="virtual" name="message"></textarea><br/>

<input type="submit" value="send">

</form>';

}

# Predefine the Mail Settings for sending...

$mailheaders = "From: $lettername";

$mailheaders .= "Reply-To:$youremail";

# add data in bcc fields

# Data was ok, send button is pressed

if ($send == "yes"){

$message=stripslashes($message);

$subject=stripslashes($subject);

$lines = explode("%",$content);

for ($key=1;$key<sizeof($lines);$key++){

mail ($lines[$key],$subject,$message,$mailheaders);

}

print "O seguinte e-mail foi enviado!";

print "<pre>$mailheaders\n$subject\n$message</pre>";

}

}

########################################################################

# The Form for the users...

########################################################################

if ($pw != $pass && $disp != "no"){

print $welcomemessage;

print'

<div align="left"><form method="post">

<input type="text" name="email" size=30>

<input type="radio" name="action" value="sign" checked="checked">cadastrar

<input type="radio" name="action" value="delete">cancelar<br>

<input type="submit" value="send">

</form></div>

';

}

?>

Link para o comentário
Compartilhar em outros sites

1 resposta a esta questão

Posts Recomendados

Participe da discussão

Você pode postar agora e se registrar depois. Se você já tem uma conta, acesse agora para postar com sua conta.

Visitante
Responder esta pergunta...

×   Você colou conteúdo com formatação.   Remover formatação

  Apenas 75 emoticons são permitidos.

×   Seu link foi incorporado automaticamente.   Exibir como um link em vez disso

×   Seu conteúdo anterior foi restaurado.   Limpar Editor

×   Você não pode colar imagens diretamente. Carregar ou inserir imagens do URL.



  • Estatísticas dos Fóruns

    • Tópicos
      152,3k
    • Posts
      652,2k
×
×
  • Criar Novo...