Scusate, riposto il codice non commentato:
Codice PHP:
<?phpif(isset($_POST['invia'])){ $error = FALSE; $nome = isset($_POST['nome']) ? trim( (string) $_POST['nome']) : ''; $cognome = isset($_POST['cognome']) ? trim( (string) $_POST['cognome']) : ''; $oggetto = isset($_POST['oggetto']) ? trim( (string) $_POST['oggetto']) : ''; $email = isset($_POST['email']) ? trim( (string) $_POST['email']) : ''; $messaggio = isset($_POST['messaggio']) ? trim( (string) $_POST['messaggio']) : '';
if(empty($nome)) { $error = TRUE; $msg = 'Il campo nome è obbligatorio!'; } else if(empty($cognome)){ $error = TRUE; $msg = 'Il campo cognome è obbligatorio'; } else if(empty($oggetto)){ $error = TRUE; $msg = 'Il campo oggetto è obbligatorio'; } else if(!preg_match('/^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)+$/', $email)){ $error = TRUE; $msg = 'Il campo email è obbligatorio. Verifica di averlo digitato correttamente.'; } else if(empty($messaggio)){ $error = TRUE; $msg = 'Il campo messaggio è obbligatorio'; } else{ require_once('lib/swift_required.php'); require_once('config.php'); if( (PORT_SMTP != FALSE) AND ( SECUTITY_SMTP != FALSE ) ){ $transport = Swift_SmtpTransport::newInstance(HOST_SMTP, PORT_SMTP, SECUTITY_SMTP); } else if( PORT_SMTP != FALSE ){ $transport = Swift_SmtpTransport::newInstance(HOST_SMTP, PORT_SMTP); } else{ $transport = Swift_SmtpTransport::newInstance(HOST_SMTP); } $transport->setUsername(EMAIL_SMTP); $transport->setPassword(PASSWORD_SMTP); $mailer = Swift_Mailer::newInstance($transport); $message = Swift_Message::newInstance($oggetto); $message->setFrom(array($email => $nome)); $message->setTo(array(EMAIL_DESTINATARIO)); $message->setBody( '<html>' . '<head></head>' . '<body>' . 'Nome:' . $nome . '<br />'. 'Cognome:' . $cognome . '<br />'. 'Messaggio:' . $messaggio . '<br />'. '</body>' . '</html>', 'text/html' ); $message->setReturnPath(EMAIL_SMTP);
if( (isset($_FILES["allegato"])) AND ($_FILES["allegato"]['name'] != '') ){ $tipologie_consentite = array('jpg', 'jpeg', 'png', 'gif', 'doc', 'docx', 'pdf', 'xls', 'zip', 'rar'); $path_info = pathinfo($_FILES["allegato"]['name']);
if(!is_uploaded_file($_FILES["allegato"]['tmp_name'])){ $error = TRUE; $msg = 'Si è verificato un errore durante il caricamento del file!'; } else if(!in_array(strtolower($path_info['extension']), $tipologie_consentite)){ $error = TRUE; $msg = 'Il file non è fra i tipi consentiti!'; }
else if($_FILES["allegato"]['size'] > MAX_DIM_FILE){
$error = TRUE; $msg = 'Il file allegato eccede la dimensione massima consentita!'; }
else{ $attachment = Swift_Attachment::fromPath($_FILES["allegato"]['tmp_name']); $attachment->setFilename($_FILES["allegato"]['name']); $message->attach($attachment); } } }
if($error === FALSE){ $result = $mailer->send($message); if($result>0){ $msg = "Email inviata. Presto sarai ricontattato."; } else{ $msg = "Problemi nell'invio della email!"; } }
$get_var = urlencode($msg); header('location: offerta-lavoro.php?msg=' . $get_var); exit; }?>
Grazie per qualsiasi aiuto, Alx