· 10 years ago · Jul 26, 2016, 08:21 PM
1<?php
2 require_once('incl/PHP_Mailer/class.phpmailer.php');
3
4 function check_if_field_isset($field) {
5 if (isset($_POST[$field])) {
6 return $_POST[$field];
7 }
8 else {
9 return "";
10 }
11 }
12
13 function check_if_file_is_uploaded() {
14 if(empty($_FILES)) {
15 return false;
16 }
17 if(!file_exists($_FILES['myfile']['tmp_name']) || !is_uploaded_file($_FILES['myfile']['tmp_name'])) {
18 return false;
19 }
20 return true;
21 }
22
23 function send_mail($mail_data, $user_data) {
24 $mail = new PHPMailer();
25
26 try {
27 $mail->IsSMTP();
28 $mail->SMTPAuth = true;
29 $mail->SMTPSecure = "ssl";
30 $mail->Host = "smtp.gmail.com";
31 $mail->Port = 465;
32 $mail->Username = "************@gmail.com";
33 $mail->Password = "******";
34
35 $mail->IsHTML(true);
36 $mail->From = $mail_data["send_from"];
37 $mail->FromName = $mail_data["send_from_name"];
38 $mail->Subject = $mail_data["subject"];
39 $mail->SMTPDebug = 0;
40 $mail->MailerDebug = false;
41 $mail->Body = $mail_data["content"];
42 $mail->AddAddress($mail_data["recipient"]);
43 $mail->CharSet = 'UTF-8';
44
45 //attachment
46 if (check_if_file_is_uploaded() == TRUE) {
47 $mail->AddAttachment($user_data["attachment"][tmp_name], $user_data["attachment"][name]);
48 }
49
50 $mail->Send();
51
52 echo $mail_data["message_success"];
53 }
54 catch (phpmailerException $e) {
55 //echo $e->errorMessage(); //error messages from PHPMailer
56 echo $mail_data["message_fail"];
57 }
58 catch (Exception $e) {
59 //echo $e->getMessage(); //error messages from anything else!
60 echo $mail_data["message_fail"];
61 }
62 }
63
64 $user_data["packet"] = check_if_field_isset("packet");
65 $user_data["name"] = check_if_field_isset("name");
66 $user_data["surname"] = check_if_field_isset("surname");
67 $user_data["email"] = check_if_field_isset("email");
68 $user_data["website"] = check_if_field_isset("website");
69 $user_data["note"] = check_if_field_isset("note");
70 // New line in textarea is displayed as \r\n, so this string should be replaced with <br>.
71 $user_data["note"] = str_replace("\r\n", "<br>", $user_data["note"]);
72 $user_data["attachment"] = "";
73
74 // if file is uploaded then read some file data
75 if (check_if_file_is_uploaded() == TRUE) {
76 $user_data["attachment"] = $_FILES["myfile"];
77
78 $file_size = $_FILES['myfile']['size'];
79 $mimetype = $_FILES['myfile']['type'];
80 }
81
82 $errors = FALSE;
83 //check for errors
84 if ($user_data["name"] == "") {
85 $errors = TRUE;
86 echo "<div class='alert alert-danger'>First name is required field!</div>";
87 }
88 elseif ($user_data["surname"] == "") {
89 $errors = TRUE;
90 echo "<div class='alert alert-danger'>Last name is required field!</div>";
91 }
92 elseif ($user_data["email"] == "") {
93 $errors = TRUE;
94 echo "<div class='alert alert-danger'>E-mail address is required field!</div>";
95 }
96 elseif (! filter_var($user_data["email"], FILTER_VALIDATE_EMAIL)) {
97 $errors = TRUE;
98 echo "<div class='alert alert-danger'>E-mail address is invalid!</div>";
99 }
100
101 if (! $errors && check_if_file_is_uploaded() == TRUE) {
102 // convert max_file_size to bytes
103 if ($file_size > $max_file_size * 1024 * 1024) {
104 $errors = TRUE;
105 echo "<div class='alert alert-danger top40'>Attachment file is too big. Maximum file size can be " . $max_file_size . "MB.</div>";
106 }
107
108 if (! in_array($mimetype, $file_types, true)) {
109 if ($file_types_text == "") {
110 if (empty($file_types) == TRUE) {
111 $file_types_text_tmp = "All";
112 }
113 else {
114 $file_types_text_tmp = "";
115 foreach ($file_types as $key => $value) {
116 if ($file_types_text_tmp == "") {
117 $file_types_text_tmp = $key;
118 }
119 else {
120 $file_types_text_tmp = $file_types_text_tmp . ", " . $key;
121 }
122 }
123 }
124 }
125 else {
126 $file_types_text_tmp = $file_types_text;
127 };
128
129 // check, if file has correct type
130 $errors = TRUE;
131 echo "<div class='alert alert-danger top40'>Wrong file type! You can use this file types: " . $file_types_text_tmp . ".</div>";
132 }
133 }
134
135 if ( $errors == FALSE ) {
136 $content = '<html><body><table>';
137 $content .= '<tr style="background-color: #00b9e1; color: #FFFFFF;"><th style="border: 1px solid gray; padding: 5px">Field</th>';
138 $content .= '<th style="border: 1px solid gray; padding: 5px">Data</th></tr>';
139
140 foreach($user_data as $key => $field) {
141 $content .= '<tr><td style="border: 1px solid gray; padding: 5px">' . $key . '</td>';
142 if (($key == "attachment") && ($field != "")) {
143 $content .= '<td style="border: 1px solid gray; padding: 5px">' . $field["name"] . '</td></tr>';
144 }
145 else {
146 $content .= '<td style="border: 1px solid gray; padding: 5px">' . $field . '</td></tr>';
147 }
148 }
149
150 $content .= "</table></body></html>";
151
152 $mail_data["recipient"] = $recipient;
153
154 // if string "contact_form" is chosen in configuration, then data from contact form is inserted. On the other way data is static.
155 if ($send_from == "contact_form") {
156 $mail_data["send_from"] = $user_data["email"];
157 }
158 else {
159 $mail_data["send_from"] = $send_from;
160 }
161
162 // if string "contact_form" is chosen in configuration, then data from contact form is inserted. On the other way data is static.
163 if ($send_from_name == "contact_form") {
164 $mail_data["send_from_name"] = $user_data["name"] . " " . $user_data["surname"];
165 }
166 else {
167 $mail_data["send_from_name"] = $send_from_name;
168 }
169
170 $mail_data["subject"] = $subject;
171 $mail_data["content"] = $content;
172 $mail_data["message_success"] = $message_success;
173 $mail_data["message_fail"] = $message_fail;
174
175 send_mail($mail_data, $user_data);
176 }
177
178 echo '<br><div class="text-center"><a href="index.php">Send again!</a></div>';
179?>