Ragazzi sto cercando di usare PHPMailer per ricevere emails tramite un contact form ma c'è qualcosa che non va nel codice perchè ho sempre errore.
Chi mi da una manina?
PHP:
<?php
require 'src/PHPMailerAutoload.php';
$fromEmail = 'demo@domain.com';
$fromName = 'Demo contact form';
$sendToEmail = 'miaemail@tiscali.it';
$sendToName = 'Demo contact form';
$subject = 'Nuovo messaggio';
$smtpHost = 'smtp.tiscali.it';
$smtpUsername = 'miaemail@tiscali.it';
$smtpPassword = '12345';
$fields = array('name' => 'Name', 'surname' => 'Surname', 'email' => 'Email', 'message' => 'Message');
$okMessage = 'Email inviata consuccesso!';
$errorMessage = 'Impossibile inviare la mail';
error_reporting(E_ALL & ~E_NOTICE);
try {
if (count($_POST) == 0) {
throw new \Exception('Campi vuoti');
}
$emailTextHtml = "<h1>Nuovo messaggio dal form</h1><hr>";
$emailTextHtml .= "<table>";
foreach ($_POST as $key => $value) {
if (isset($fields[$key])) {
$emailTextHtml .= "<tr><th>$fields[$key]</th><td>$value</td></tr>";
}
}
$emailTextHtml .= "</table><hr>";
$emailTextHtml .= "<p>Buona giornata,<br>Salutij</p>";
$mail = new PHPMailer;
$mail->setFrom($fromEmail, $fromName);
$mail->addAddress($sendToEmail, $sendToName);
$mail->addReplyTo($from);
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $emailTextHtml;
$mail->msgHTML($emailTextHtml);
$mail->isSMTP();
$mail->SMTPDebug = 0;
$mail->Debugoutput = 'html';
$mail->Host = gethostbyname($smtpHost);
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = $smtpHost;
$mail->Password = $smtpPassword;
if (!$mail->send()) {
throw new \Exception('I could not send the email.' . $mail->ErrorInfo);
}
$responseArray = array('type' => 'success', 'message' => $okMessage);
} catch (\Exception $e) {
// $responseArray = array('type' => 'danger', 'message' => $errorMessage);
$responseArray = array('type' => 'danger', 'message' => $e->getMessage());
}
Chi mi da una manina?
