· 10 years ago · Jan 13, 2016, 10:18 AM
1<?php
2
3/**
4 * Subscription Form Configuration for PHP5
5 *
6 * You have three options:
7 * 1) Save subscriber email to a txt file
8 * 2) Send subscriber email to your email account
9 * 3) Save subscriber email to database (MySQL)
10 *
11 * Usage: Set "TRUE" or "FALSE" to option you like to enable or disable
12 * Note: You can choose more than one option if you like!
13 *
14 */
15
16// Save subscriber email to a txt file, set TRUE or FALSE
17$saveTxt = FALSE;
18
19// Send subscriber email to your email account, set TRUE or FALSE
20$sendEmail = TRUE;
21
22// Save subscriber email to database (MySQL), set TRUE or FALSE
23$saveMySQL = FALSE;
24
25
26/********************* Note: DON'T forget to CONFIG your options bellow *********************/
27
28if(!empty($_POST)){
29
30 // Input data from subscription form
31 $email = trim($_POST['email']);
32 if (get_magic_quotes_gpc()){
33 $email = stripslashes($email);
34 }
35
36 // Email Validation
37 $regexp = "/^[_a-zA-Z0-9-]+([\.+][_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*(\.[a-zA-Z]{2,6})$/";
38 if (preg_match($regexp, $email)){
39
40 // It's a valid email address
41 echo json_encode(array('error' => false));
42
43 // Continue with options:
44
45 /** SaveTxt starts **/
46 if($saveTxt){
47
48 $file = "myFile.txt"; // CONFIG: Enter a filename for your txt file HERE
49 $delimiter = "\r\n"; // CONFIG: New line between emails OR use anything you like, as example ", "
50
51 if(file_exists($file)){
52 // Append if the file already exists
53 file_put_contents($file,$email.$delimiter,FILE_APPEND);
54 }else{
55 // Otherwise write a new file
56 file_put_contents($file,$email.$delimiter);
57 }
58 }
59 /** SaveTxt stops **/
60
61
62
63 /** SendEmail starts **/
64 if($sendEmail){
65
66 $to = "aqib.rashid@apexure.com"; // CONFIG: Enter your email address HERE
67 $subject = "From Subscription Form"; // CONFIG: Enter your Subject
68
69 $body = "From Subscription Form: ".$email."\r\n";
70 $body .= "\r\n";
71 $body .= "-----------------------------------\r\n";
72 $body .= "IP Address: $_SERVER[REMOTE_ADDR]\r\n";
73 $body .= "Web Browser: $_SERVER[HTTP_USER_AGENT]\r\n";
74 $body .= "Date: ".date('Y-m-d H:i:s')."\r\n";
75
76 $headers = 'MIME-Version: 1.0'."\r\n";
77 $headers .= 'Content-type: text/plain; charset=utf-8'."\r\n";
78 $headers .= "From:".$email."\r\n";
79
80 mail($to,$subject,$body,$headers);
81 }
82 /** SendEmail stops **/
83
84
85
86 /** SaveMySQL starts **/
87 if($saveMySQL){
88
89 $server = 'localhost'; // CONFIG: Usually it's by default "localhost", don't change it unless you know it
90 $username = 'root'; // CONFIG: Enter your MySQL username
91 $password = ''; // CONFIG: Enter your MySQL password
92 $database = 'myDatabase'; // CONFIG: Enter your database name *(you have to create it with phpMyAdmin)
93 $table = 'myTable'; // CONFIG: Enter your table name *(your table will create for you if not exist)
94
95 // Connect to database
96 $db = new mysqli($server,$username,$password,$database);
97 if($db->connect_errno > 0){
98 die('Unable to connect to database');
99 }
100
101 // Create the table if not exist
102 $query =
103 "CREATE TABLE IF NOT EXISTS ".$table." (
104 id INT NOT NULL AUTO_INCREMENT,
105 email VARCHAR(200) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
106 PRIMARY KEY(id)
107 )";
108
109 $result = $db->query($query) or die ('Unable to create table to database');
110
111 // Save the email in database
112 $email = $db->real_escape_string($email);
113 $query = "INSERT into ".$table." (id, email) VALUES ('', '".$email."')";
114 $result = $db->query($query) or die ('Unable to insert to database');
115
116 // Close the database
117 mysqli_close($db);
118 }
119 /** SaveMySQL stops **/
120
121 // Finish
122 exit;
123
124 }else{
125
126 // It's nοt a valid email address
127 echo json_encode(array('error' => true));
128
129 // Finish
130 exit;
131 }
132}
133?>