Advertisement
Guest User

mail script

a guest
Apr 19th, 2017
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.48 KB | None | 0 0
  1. <?php
  2.     error_reporting(-1);
  3.     ini_set('display_errors', 'On');
  4.  
  5.     function check_input($data) : string {
  6.         $data = trim($data);
  7.         $data = stripslashes($data);
  8.         $data = htmlspecialchars($data);
  9.         return $data;
  10.     }
  11.  
  12.     function createHTMLMessage($naam, $email, $phone, $msg, $chkArr) : string {
  13.         $message = '<table>';
  14.         $message .=     '<tr>';
  15.         $message .=         '<td><b>Naam:</b></td>';
  16.         $message .=         '<td>' . $naam . '</td>';
  17.         $message .=     '</tr>';
  18.         $message .=     '<tr>';
  19.         $message .=         '<td><b>Email:</b></td>';
  20.         $message .=         '<td>' . $email . '</td>';
  21.         $message .=     '</tr>';
  22.         if ($phone) {
  23.             $message .=     '<tr>';
  24.             $message .=         '<td><b>Telefoon:</b></td>';
  25.             $message .=         '<td>' . $phone . '</td>';
  26.             $message .=     '</tr>';
  27.         }
  28.         if ($chkArr) {
  29.             $via = '';
  30.             foreach ($chkArr as $item) {
  31.                 $via .= $item . '\n';
  32.             }
  33.             $message .=  '<tr>';
  34.             $message .=         '<td><b>Animagie leren kennen via:</b></td>';
  35.             $message .=         '<td>' . $via . '</td>';
  36.             $message .=  '</tr>';
  37.         }
  38.         $message .= '</table>';
  39.         $message .= '<hr />';
  40.         $message .= $msg;
  41.  
  42.         return $message;
  43.     }
  44.  
  45.     function createNonHTMLMessage($naam, $email, $phone, $msg, $chkArr) : string {
  46.         $message = "Naam:\t\t\t" . $naam . "\n";
  47.         $message .= "Email:\t\t\t" . $email . "\n";
  48.         if ($phone) {
  49.             $message .= "Telefoon:\t\t\t" . $phone . "\n";
  50.         }
  51.         if ($chkArr) {
  52.             $via = '';
  53.             foreach ($chkArr as $item) {
  54.                 $via .= $item . '\n';
  55.             }
  56.         }
  57.         $message .= "Bericht:\n\n" . $msg . "\n";
  58.  
  59.         return $message;
  60.     }
  61.  
  62.     function sendMail($naam, $email, $phone, $subj, $msg, $chkArr) {
  63.         require 'PHPMailer/PHPMailerAutoload.php';
  64.         $TO = ' ... '; //TODO
  65.  
  66.         $mail = new PHPMailer(true);
  67.  
  68.         try {
  69.             $mail->setLanguage('nl');
  70.             $mail->isSMTP();
  71.             $mail->Host = 'smtp.gmail.com'; //TODO
  72.             $mail->SMTPAuth = true;
  73.             $mail->Username = ' ... '; //TODO
  74.             $mail->Password = ' ... '; //TODO
  75.             $mail->SMTPSecure = 'tls';
  76.             $mail->Port = 587;
  77.  
  78.             $mail->setFrom($email, $naam);
  79.             $mail->addAddress($TO, 'Animagie');
  80.             $mail->isHTML(true);
  81.  
  82.             $mail->Subject = 'Animagie: ' . $subj;
  83.             $mail->Body = createHTMLMessage($naam, $email, $phone, $msg, $chkArr);
  84.             $mail->AltBody = createNonHTMLMessage($naam, $email, $phone, $msg, $chkArr);
  85.  
  86.             if (!$mail->send()) {
  87.                 $error = "Bericht kon niet worden verstuurd: " . $mail->ErrorInfo;
  88.                 echo $error;
  89.                 exit;
  90.             } else {
  91.                 //TODO: return to success page
  92.                 echo 'Bericht succesvol verstuurd!';
  93.                 exit;
  94.             }
  95.         } catch (phpmailerException $ex) {
  96.             echo $ex->getMessage();
  97.             exit;
  98.         } catch (Exception $ex) {
  99.             echo $ex->getMessage();
  100.             exit;
  101.         }
  102.     }
  103.  
  104.     if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  105.  
  106.             /*some input validation */
  107.             sendMail($naam, $email, $phone, $subj, $msg, $chkArr);
  108.     }
  109. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement