· 7 years ago · Jan 13, 2019, 06:06 AM
1test PHP mail()
2$ok = @mail($to, $subject, $message, $headers, $returnpath);
3 if($ok){ header("Location: $ThanksURL");
4 exit;
5}else{ $_SESSION['error'] .= " There has been a problems submitting your details. <br />";
6 header("Location: $form");
7 exit;
8 }// end if ok
9
10<?php
11session_start();
12$form = 'index.php';
13$_SESSION['error'] = "The following errors have occured: ";
14
15if(isset($_POST['submitted'])) {
16
17 // email fields: to, from, subject, and so on
18 // Here
19$from = "Form Feedback <******@gmail.com>";
20 $to = "******@gmail.com";
21 $subject = "Competition";
22 $ThanksURL = "thankyou.html";
23 $headers = "From: $from";
24 $returnpath = "-f" . $from;
25 $attachment = 0; // is there an attachement
26 //form fields
27 $emailAddress = stripslashes($_POST['email']);
28 $phone = stripslashes($_POST['phone']);
29 $comments = stripslashes($_POST['comments']);
30 //basic message info
31 $message = "Info submitted:nnEmail address: " .$emailAddress ."nnPhone number: " .$phone."nnComments:n ".$comments. "nn";
32
33 if($_FILES['attachment']['error'] == 4) {
34 $message .="No attachement included";
35 }else{
36 // test file type and size for submission
37 $allowedExts = array("doc", "docx", "pdf", "txt");
38 $extension = end(explode(".", $_FILES['attachment']["name"]));
39
40 if ((($_FILES['attachment']["type"] == "application/msword")
41 || ($_FILES['attachment']["type"] == "application/vnd.openxmlformats-officedocument.wordprocessingml.document")//docx mime
42 || ($_FILES['attachment']["type"] == "application/pdf")
43 || ($_FILES['attachment']["type"] == "application/plain")
44 || ($_FILES['attachment']["type"] == "text/plain"))
45 && ($_FILES['attachment']["size"] < 2097152 )
46 && in_array($extension, $allowedExts)){
47 // boundary
48 $semi_rand = md5(time());
49 $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
50
51 // headers for attachment
52 $headers .= "nMIME-Version: 1.0n" . "Content-Type: multipart/mixed;n" . " boundary="{$mime_boundary}"";
53
54 // multipart boundary
55 $message = "--{$mime_boundary}n" . "Content-Type: text/plain; charset="iso-8859-1"n"."Content-Transfer-Encoding: 7bitnn" . $message . "nn";
56
57 // prepare the files uplaod
58 $message .= "--{$mime_boundary}n";
59 $fp = @fopen($_FILES['attachment']['tmp_name'],"rb");
60 $data = @fread($fp,filesize($_FILES['attachment']['tmp_name']));
61 @fclose($fp);
62 $data = chunk_split(base64_encode($data));
63
64 $message .= "Content-Type: application/octet-stream; name="".$_FILES['attachment']['name'].""n"."Content-Description: ".$_FILES['attachment']['name']."n" ."Content-Disposition: attachment;n" . " filename="".$_FILES['attachment']['name']."";size=".$_FILES['attachment']['size'].";n"."Content-Transfer-Encoding: base64nn" . $data . "nn";
65
66$message .= "--{$mime_boundary}--";
67 }else{
68 $_SESSION['error'] .= "Error: " . $_FILES["attachment"]["name"] . " is not a doc, docx, pdf or txt file or is larger than 2mb. Please resubmit <br />";
69 header("Location: $form");
70 exit;
71 }
72 }//file conditional
73 //prepare mail
74 $ok = @mail($to, $subject, $message, $headers, $returnpath);
75 if($ok){
76 header("Location: $ThanksURL");
77 exit;
78 }else{
79 $_SESSION['error'] .= " There has been a problems submitting your details. <br />";
80 header("Location: $form");
81 exit;
82 }// end if ok
83 }// end sub
84
85?>