Advertisement
jfs

phpmailer

jfs
Oct 20th, 2018
400
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.29 KB | None | 0 0
  1. <?php
  2. $formid = 'fomid-'. $module->autoid() . '_';
  3.  
  4.  
  5. //PHPmailer
  6. use PHPMailer\PHPMailer\PHPMailer;
  7. use PHPMailer\PHPMailer\Exception;
  8.  
  9. require_once __DIR__ . '/phpmailer/vendor/phpmailer/phpmailer/src/Exception.php';
  10. require_once __DIR__ . '/phpmailer/vendor/phpmailer/phpmailer/src/PHPMailer.php';
  11. require_once __DIR__ . '/phpmailer/vendor/phpmailer/phpmailer/src/SMTP.php';
  12. ?>
  13.  
  14.  
  15. <form action="<?php $module->action(); ?>" method="<?php echo $module->method(); ?>" enctype="<?php e($module->enctype() == 'multipart', 'multipart/form-data'); ?>" class="<?php echo $module->modulclass(); ?>" style="<?php echo $module->fieldstyle(); ?>">
  16.     <?php echo $module->modules(['formid' => $formid]); ?>
  17.     <input type="hidden" name="csrf" value="<?php echo csrf(); ?>">
  18. </form>
  19.  
  20.  
  21. <?php
  22. if(r::is('post') && get($formid . 'submit')) {
  23.     $token = $_POST['csrf'];
  24.     if(csrf($token) === true) {
  25.  
  26.         $formItems = $_POST; //Fill var with the $_POST
  27.         array_splice($formItems, -2); //delete CSFR and submit
  28.  
  29.  
  30.         //get Data for Mailtext
  31.         $formItemsHTML = '';
  32.         $formItemsNoHTML = '';
  33.  
  34.         if(isset($formItems['Sonstiges-20'])){
  35.             $sonstiges = $formItems['Sonstiges-20']; //save sonstiges form in var
  36.             unset($formItems['Sonstiges-20']); // delete sonstiges from array
  37.         }
  38.  
  39.         foreach($formItems as $key => $value){
  40.             $origkey = $key;
  41.  
  42.             if($key == 'Sonstiges-20') {
  43.                 unset($key['Sonstiges-20']);
  44.             }
  45.  
  46.             if($key !== 'Nutzung_(aktuell)' AND $key !== 'Bebauung_(aktuell)'){
  47.                 $key = strstr($key, '-', true);
  48.                 $key = ucwords($key);
  49.             }
  50.  
  51.             $key = str_replace('_', ' ', $key);
  52.  
  53.             // $searchWord = ['as', 'or', 'of', 'by'];
  54.             // $key = str_ireplace($searchWord, $searchWord, $key);
  55.             // $searchUCWordOne = ['ssp', 'ape', 'vat', 'po '];
  56.             // $searchUCWordTwo = ['SSP', 'APE', 'VAT', 'PO '];
  57.             // $key = str_ireplace($searchUCWordOne, $searchUCWordTwo, $key);
  58.  
  59.             // $searchWord = ['conference', 'organisation', 'adress'];
  60.             // $searchUCWord = ['Conference', 'Organisation', 'Adress'];
  61.             // $key = str_ireplace($searchWord, $searchUCWord, $key);
  62.  
  63.             if($value == 'Sonstiges' AND isset($sonstiges)){
  64.                 $value = $sonstiges;
  65.             }
  66.  
  67.             if ($value === 'on'):
  68.                 $value = '&#x2713;';
  69.                 // $searchWord = ['on'];
  70.                 // $replace = ['✓'];
  71.                 // $value = str_ireplace($searchWord, $replace, $value);
  72.             elseif(empty($value)):
  73.                 $value = '-';
  74.             endif;
  75.  
  76.  
  77.             $plainValue = $value;
  78.  
  79.             $formItemsHTML .= '
  80.                    <tr>
  81.                        <td width="230" style="padding-left: 15px; padding-top:5px; padding-bottom:5px;"><b>' . $key . '</b></td>
  82.                        <td width="270" style="padding-right: 10px; padding-top:5px; padding-bottom:5px;">' . $plainValue .'</td>
  83.                    </tr>';
  84.  
  85.             $formItemsNoHTML .= $key . $plainValue;
  86.         }
  87.  
  88.         $headerHTMLOne = file_get_contents(__DIR__ . '/htmlmail-header-one.html');
  89.         $headerHTMLTwo = file_get_contents(__DIR__ . '/htmlmail-header-two.html');
  90.         $footerHTML    = file_get_contents(__DIR__ . '/htmlmail-footer.html');
  91.  
  92.         $bodyHTMLOne     = $headerHTMLOne . $formItemsHTML . $footerHTML;
  93.         $bodyHTMLTwo     = $headerHTMLTwo . $formItemsHTML . $footerHTML;
  94.  
  95.         $bodyNoHTML   = $formItemsNoHTML;
  96.  
  97.  
  98.         $mail = new PHPMailer(true);
  99.  
  100.         $maximumfiles = 10;
  101.         $finfo = new finfo;
  102.         $acceptedMime = ['jpg' => 'image/jpeg','png' => 'image/png','gif' => 'image/gif','pdf' => 'application/pdf'];
  103.  
  104.  
  105.         // $mail->isSMTP();
  106.         $mail->IsSendmail();
  107.         $mail->SMTPAuth = true;
  108.         $mail->Host = $site->smtphost();
  109.         $mail->Username = $site->smtpusername();
  110.         $mail->Password = $site->smtppassword();
  111.         $mail->Port = $site->smtpport();
  112.         $mail->SMTPSecure = 'tls';
  113.         $mail->SMTPDebug  = 1;
  114.  
  115.         $from_name     = '';
  116.         $from_nameTwo  = $site->mainmailname();
  117.         $from_mail     = '';
  118.         $from_mailTwo  = $site->mainmail();
  119.         $replyto       = 'XYZ@gmail.com';
  120.         $replytoTwo    = $site->mainmail();
  121.         $mailto        = $site->mainmail();
  122.         $mailtoTwo     = 'XYZ@gmail.com';
  123.  
  124.         $subject       = $page->title();
  125.         $messageHTML   = $bodyHTML;
  126.         $messageNoHTML = $bodyNoHTML;
  127.  
  128.  
  129.         // 1.mail – Mail to the websiteoperator
  130.         $mail->CharSet = "UTF-8";
  131.         $mail->From = $from_mail;
  132.         $mail->FromName = $from_name;
  133.         $mail->addAddress($mailto); // Add a "To" address
  134.         $mail->addReplyTo($replyto); // Antwortadresse festlegen
  135.         $mail->isHTML(true); // Mail als HTML versenden
  136.         try {
  137.             if (array_key_exists('uploaded_files', $_FILES)) {
  138.                 //Attach multiple files one by one
  139.                 for ($i = 0; $i < count($_FILES['uploaded_files']['tmp_name']); $i++) {
  140.                     if ($i == $maximumfiles) {
  141.                         throw new RuntimeException('<div class="alert">Sie haben zuviele Dateien hochgeladen.</div>');
  142.                     }
  143.                     if (!isset($_FILES['uploaded_files']['error'][$i])) {
  144.                         throw new RuntimeException('<div class="alert">Etwas mit Ihren Dateien stimmt nicht.</div>');
  145.                     }
  146.                     switch ($_FILES['uploaded_files']['error'][$i]) {
  147.                         case UPLOAD_ERR_OK:
  148.                             break;
  149.                         // case UPLOAD_ERR_NO_FILE:
  150.                         //     throw new RuntimeException('<div class="alert">Eine Ihrer Dateien scheint nicht verschickt worden zu sein.</div>');
  151.                         case UPLOAD_ERR_INI_SIZE:
  152.                         case UPLOAD_ERR_FORM_SIZE:
  153.                             throw new RuntimeException('<div class="alert">Eine Ihrer Dateien scheint von der Datenmenge groß zu sein.</div>');
  154.                         // default:
  155.                         //     throw new RuntimeException('<div class="alert">Ein unbekannter Fehler ist aufgetreten.</div>');
  156.                     }
  157.                     if ($_FILES['uploaded_files']['size'][$i] > 5000000) {
  158.                         throw new RuntimeException('<div class="alert">Die maximale Dateigröße ist überschritten.</div>');
  159.                     }
  160.                     // if(in_array($finfo->file($_FILES['uploaded_files']['tmp_name'][$i]), $acceptedMime, true) === false){
  161.                     //   throw new RuntimeException('<div class="alert">Invalid file format.</div>');
  162.                     // }
  163.  
  164.  
  165.                     // if (false === $ext = array_search(
  166.                     //     $finfo->file($_FILES['uploaded_files']['tmp_name'][$i]),
  167.                     //     array(
  168.                     //         'jpg' => 'image/jpeg',
  169.                     //         'png' => 'image/png',
  170.                     //         'gif' => 'image/gif',
  171.                     //         'pdf' => 'application/pdf',
  172.                     //     ),
  173.                     //     true
  174.                     // )) {
  175.                     //     throw new RuntimeException('<div class="alert">Invalid file format.</div>');
  176.                     // }
  177.  
  178.                     $uploadfile = $_FILES['uploaded_files']['tmp_name'][$i];
  179.                     $filename = $_FILES['uploaded_files']['name'][$i];
  180.  
  181.                     $files = $mail->addAttachment($uploadfile, $filename);
  182.                 }
  183.             }
  184.  
  185.             $mail->Subject = $subject;
  186.             $mail->Body = $messageHTML;
  187.             $mail->AltBody = $messageNoHTML;
  188.             if(isset($uploadfile) AND !empty($uploadfile) AND isset($filename) AND !empty($filename)){
  189.                 echo $files;
  190.             }
  191.             $mail->send();
  192.             $mail->ClearAllRecipients();
  193.             $mail->clearReplyTos();
  194.             $mail->ClearAttachments();
  195.  
  196.             // // 2.mail – Mail to the person who has written the mail.
  197.             $mail->CharSet = "UTF-8";
  198.             $mail->From = $from_mail;
  199.             $mail->FromName = $from_name;
  200.             $mail->addAddress($mailtoTwo); // Add a "To" address
  201.             $mail->addReplyTo($replytoTwo); // Antwortadresse festlegen
  202.             $mail->isHTML(true); // Mail als HTML versenden
  203.             $mail->Subject = $subject;
  204.             $mail->Body = $messageHTML;
  205.             $mail->AltBody = $messageNoHTML;
  206.             if(isset($uploadfile) AND !empty($uploadfile) AND isset($filename) AND !empty($filename)){
  207.                 echo $files;
  208.             }
  209.             $mail->send();
  210.             $mail->ClearAllRecipients();
  211.             $mail->clearReplyTos();
  212.             $mail->ClearAttachments();
  213.  
  214.             go('grundstuck-verkaufen-success');
  215.  
  216.             // if(!$mail->send()) {
  217.             //     echo ':(!';
  218.             //     echo 'oops: ' . $mail->ErrorInfo;
  219.             // } else {
  220.             //     go('grundstuck-verkaufen-success');
  221.             // }
  222.         } catch (RuntimeException $e) {
  223.             echo $e->getMessage();
  224.         }
  225.  
  226.     }
  227.     else {
  228.         echo "Token doesn't match";
  229.     }
  230. }
  231. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement