Salve a tutti
Ho messo online la nuova versione di un nostro sito e quando provo ad inviare una email tramite il modulo nella pagina dei contatti, questa email arriva nella casella di posta indesiderata.
Utilizzo phpmailer e questo e' il codice che utilizzo per inviare l'email, magari sbaglio qualcosa?
Codice PHP:
// Clean up the input values
foreach($_POST as $key => $value) {
if(ini_get('magic_quotes_gpc'))
$_POST[$key] = stripslashes($_POST[$key]);
$_POST[$key] = htmlspecialchars(strip_tags($_POST[$key]));
}
// Assign the input values to variables for easy reference
$Name = $_POST["name"];
$Emailaddress = $_POST["email"];
$telefono = $_POST["telefono"];
$BodyMessage = $_POST["message"];
/*
$cmsg = "
Subject: \"Richiesta dal sito\"
Message: ".$BodyMessage;
*/
// Test input values for errors
$errors = array();
if(strlen($Name) < 2) {
if(!$Name) {
$errors[] = "Ti Preghiamo di inserire nome e cognome.";
} else {
$errors[] = "Il nome deve contenere almeno 2 caratteri";
}
}
if(!$Emailaddress) {
$errors[] = "Ti Preghiamo di inserire un indirizzo email";
} else if(!validEmail($Emailaddress)) {
$errors[] = "Ti Preghiamo di inserire un indirizzo email valido";
}
if(strlen($BodyMessage) < 10) {
if(!$BodyMessage) {
$errors[] = "Ti Preghiamo di inserire il testo del messaggio.";
} else {
$errors[] = "Il testo del messaggio deve essere di almeno 10 caratteri";
}
}
if($errors) {
// Output errors and die with a failure message
$errortext = "";
foreach($errors as $error) {
$errortext .= "<li>".$error."</li>";
}
die("<span class='failure'>Errore rilevato:<ul>". $errortext ."</ul></span>");
}
require_once('../phpmailer/class.phpmailer.php');
$mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch
$mail->IsSMTP(); // telling the class to use SMTP
try {
$x_mailer = 'X-Mailer: PHP/' . phpversion();
$mail->Host = "smtp.mailgun.org"; // SMTP server
$mail->SMTPDebug = 1; // enables SMTP debug information (for testing)
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Host = "smtp.mailgun.org"; // sets the SMTP server
$mail->Port = 25; // set the SMTP port for the GMAIL server
$mail->Username = "miousernamedimailgun"; // SMTP account username
$mail->Password = "miapassworddimailgun"; // SMTP account password
$mail->addCustomHeader($x_mailer);
$mail->AddAddress("info@miosito.it", "Mio Nome");
$mail->SetFrom("$Emailaddress", "$Name");
$mail->Sender = "$Emailaddress";
$mail->Subject = "Richiesta dal sito";
$mail->AltBody = 'Per vedere questa email devi utilizzare un client abilitato ad aprire le email in html'; // optional - MsgHTML will create an alternate automatically
$mail->Body = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
<html><body>
<font face=Verdana size=2><b>Richiesta Informazioni dal sito</b></font><br><br>
<font face=Verdana size=2>
Nome e Cognome: $Name<br>
Email: $Emailaddress<br>
Telefono: $telefono<br><br>
Messaggio<br>
$BodyMessage<br>
</font></body></html>";
$mail->Send();
echo "";
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
// Die with a success message
die("<span style='color:#FF9900;' class='success'><strong>Messaggio Inviato con Successo</strong></span>");
// A function that checks to see if
// an email is valid
function validEmail($Emailaddress)
{
$isValid = true;
$atIndex = strrpos($Emailaddress, "@");
if (is_bool($atIndex) && !$atIndex)
{
$isValid = false;
}
else
{
$domain = substr($Emailaddress, $atIndex+1);
$local = substr($Emailaddress, 0, $atIndex);
$localLen = strlen($local);
$domainLen = strlen($domain);
if ($localLen < 1 || $localLen > 64)
{
// local part length exceeded
$isValid = false;
}
else if ($domainLen < 1 || $domainLen > 255)
{
// domain part length exceeded
$isValid = false;
}
else if ($local[0] == '.' || $local[$localLen-1] == '.')
{
// local part starts or ends with '.'
$isValid = false;
}
else if (preg_match('/\\.\\./', $local))
{
// local part has two consecutive dots
$isValid = false;
}
else if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain))
{
// character not valid in domain part
$isValid = false;
}
else if (preg_match('/\\.\\./', $domain))
{
// domain part has two consecutive dots
$isValid = false;
}
else if(!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/',
str_replace("\\\\","",$local)))
{
// character not valid in local part unless
// local part is quoted
if (!preg_match('/^"(\\\\"|[^"])+"$/',
str_replace("\\\\","",$local)))
{
$isValid = false;
}
}
if ($isValid && !(checkdnsrr($domain,"MX") || checkdnsrr($domain,"A")))
{
// domain not found in DNS
$isValid = false;
}
}
return $isValid;
}