Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- error_reporting(-1);
- ini_set('display_errors', 'On');
- function check_input($data) : string {
- $data = trim($data);
- $data = stripslashes($data);
- $data = htmlspecialchars($data);
- return $data;
- }
- function createHTMLMessage($naam, $email, $phone, $msg, $chkArr) : string {
- $message = '<table>';
- $message .= '<tr>';
- $message .= '<td><b>Naam:</b></td>';
- $message .= '<td>' . $naam . '</td>';
- $message .= '</tr>';
- $message .= '<tr>';
- $message .= '<td><b>Email:</b></td>';
- $message .= '<td>' . $email . '</td>';
- $message .= '</tr>';
- if ($phone) {
- $message .= '<tr>';
- $message .= '<td><b>Telefoon:</b></td>';
- $message .= '<td>' . $phone . '</td>';
- $message .= '</tr>';
- }
- if ($chkArr) {
- $via = '';
- foreach ($chkArr as $item) {
- $via .= $item . '\n';
- }
- $message .= '<tr>';
- $message .= '<td><b>Animagie leren kennen via:</b></td>';
- $message .= '<td>' . $via . '</td>';
- $message .= '</tr>';
- }
- $message .= '</table>';
- $message .= '<hr />';
- $message .= $msg;
- return $message;
- }
- function createNonHTMLMessage($naam, $email, $phone, $msg, $chkArr) : string {
- $message = "Naam:\t\t\t" . $naam . "\n";
- $message .= "Email:\t\t\t" . $email . "\n";
- if ($phone) {
- $message .= "Telefoon:\t\t\t" . $phone . "\n";
- }
- if ($chkArr) {
- $via = '';
- foreach ($chkArr as $item) {
- $via .= $item . '\n';
- }
- }
- $message .= "Bericht:\n\n" . $msg . "\n";
- return $message;
- }
- function sendMail($naam, $email, $phone, $subj, $msg, $chkArr) {
- require 'PHPMailer/PHPMailerAutoload.php';
- $TO = ' ... '; //TODO
- $mail = new PHPMailer(true);
- try {
- $mail->setLanguage('nl');
- $mail->isSMTP();
- $mail->Host = 'smtp.gmail.com'; //TODO
- $mail->SMTPAuth = true;
- $mail->Username = ' ... '; //TODO
- $mail->Password = ' ... '; //TODO
- $mail->SMTPSecure = 'tls';
- $mail->Port = 587;
- $mail->setFrom($email, $naam);
- $mail->addAddress($TO, 'Animagie');
- $mail->isHTML(true);
- $mail->Subject = 'Animagie: ' . $subj;
- $mail->Body = createHTMLMessage($naam, $email, $phone, $msg, $chkArr);
- $mail->AltBody = createNonHTMLMessage($naam, $email, $phone, $msg, $chkArr);
- if (!$mail->send()) {
- $error = "Bericht kon niet worden verstuurd: " . $mail->ErrorInfo;
- echo $error;
- exit;
- } else {
- //TODO: return to success page
- echo 'Bericht succesvol verstuurd!';
- exit;
- }
- } catch (phpmailerException $ex) {
- echo $ex->getMessage();
- exit;
- } catch (Exception $ex) {
- echo $ex->getMessage();
- exit;
- }
- }
- if ($_SERVER['REQUEST_METHOD'] === 'POST') {
- /*some input validation */
- sendMail($naam, $email, $phone, $subj, $msg, $chkArr);
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement