Blame view

PHPMailer/examples/test/submit.php 2.32 KB
42868d70   andryeyev   Создал GIT
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
  <?php
  
  /* config start */
  
  $emailAddress = 'webplus@ukr.net';
  
  /* config end */
  
  
  require "phpmailer/class.phpmailer.php";
  
  session_name("fancyform");
  session_start();
  
  
  foreach($_POST as $k=>$v)
  {
          if(ini_get('magic_quotes_gpc'))
          $_POST[$k]=stripslashes($_POST[$k]);
          
          $_POST[$k]=htmlspecialchars(strip_tags($_POST[$k]));
  }
  
  
  $err = array();
  
  if(!checkLen('name'))
          $err[]='The name field is too short or empty!';
  
  if(!checkLen('email'))
          $err[]='The email field is too short or empty!';
  else if(!checkEmail($_POST['email']))
          $err[]='Your email is not valid!';
  
  if(!checkLen('subject'))
          $err[]='You have not selected a subject!';
  
  if(!checkLen('message'))
          $err[]='The message field is too short or empty!';
  
  if((int)$_POST['captcha'] != $_SESSION['expect'])
          $err[]='The captcha code is wrong!';
  
  
  if(count($err))
  {
          if($_POST['ajax'])
          {
                  echo '-1';
          }
  
          else if($_SERVER['HTTP_REFERER'])
          {
                  $_SESSION['errStr'] = implode('<br />',$err);
                  $_SESSION['post']=$_POST;
                  
                  header('Location: '.$_SERVER['HTTP_REFERER']);
          }
  
          exit;
  }
  
  
  $msg=
  'Name:        '.$_POST['name'].'<br />
  Email:        '.$_POST['email'].'<br />
  IP:        '.$_SERVER['REMOTE_ADDR'].'<br /><br />
  
  Message:<br /><br />
  
  '.nl2br($_POST['message']).'
  
  ';
  
  
  $mail = new PHPMailer();
  $mail->IsMail();
  
  $mail->AddReplyTo($_POST['email'], $_POST['name']);
  $mail->AddAddress($emailAddress);
  $mail->SetFrom($_POST['email'], $_POST['name']);
  $mail->Subject = "A new ".mb_strtolower($_POST['subject'])." from ".$_POST['name']." | contact form feedback";
  
  $body       = $mail->getFile('../contents.html');
  
  $mail->MsgHTML(str_replace("\r\n"," ",$body));
  $mail->AddAttachment("../images/phpmailer.gif");
  
  $mail->Send();
  
  
  unset($_SESSION['post']);
  
  if($_POST['ajax'])
  {
          echo '1';
  }
  else
  {
          $_SESSION['sent']=1;
          
          if($_SERVER['HTTP_REFERER'])
                  header('Location: '.$_SERVER['HTTP_REFERER']);
          
          exit;
  }
  
  function checkLen($str,$len=2)
  {
          return isset($_POST[$str]) && mb_strlen(strip_tags($_POST[$str]),"utf-8") > $len;
  }
  
  function checkEmail($str)
  {
          return preg_match("/^[\.A-z0-9_\-\+]+[@][A-z0-9_\-]+([.][A-z0-9_\-]+)+[A-z]{1,4}$/", $str);
  }
  
  ?>