-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsendCreatedSurvey.php
177 lines (150 loc) · 7.25 KB
/
sendCreatedSurvey.php
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<!DOCTYPE HTML PUBLIC>
<?php
//Database connection
//if (! include ('MyersDBConnection.php')){
//die('error finding connect file');
//}
//$dbh=ConnectDB();
?>
<html xmlns="http://www.w3.org/1999/html">
<head>
<title>Survey Creation</title>
<link rel="stylesheet" type="text/css" href="index.css">
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
</head>
<body>
<div class="SurveyOptions">
<div id="Selection" float="center" align="center">
<br>
<br>
<br>
<h2 align="center">Invite others to take your Survey</h2>
<form action="" method="get">
<?php
/*
* mail_form.php
* NOT A STANDALONE PAGE - MUST BE INCLUDED WITHIN HTML BODY
* process to send email through external smtp server
*/
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
include('vendor/autoload.php');
include_once('./dbConnection.php');
define(SMTP_FILE, './configs/smtp.ini');
var $webmaster = "[email protected]";
//var $cc_address = <send a copy to email address>;
var $error_mailer = '';
/* survey_ID
* GENERATED @TIME OF SURVEY CREATION:
* for access when adding users to the distribution_list
*/
$survey_ID = uniqid();
if (isset($_POST['send_email'])) {
$eol = "<br>\r\n";
$tab = " ";
$email_address = $_POST['email_address'];
$user_name = $_POST['recipient'];
$user_ID = uniqid();
$completed = 0; // FALSE: survey has not been completed
// Check if user already exists in user_list
$pdo = new dbConnection();
$sql = "SELECT * FROM user_list WHERE user_email= ? LIMIT 1";
$query = $pdo->prepare($sql);
$query->bindParam(1, $email_address); // user_email
$query->execute();
$user_exists = $query->fetch(PDO::FETCH_ASSOC);
if ($user_exists) {
$user_ID = $user_exists['user_ID'];
}
// Update user_list with data
$sql = "REPLACE INTO user_list VALUES ( ?, ?, ? )";
$query = $pdo->prepare($sql);
$query->bindParam(1, $user_ID); // user_ID
$query->bindParam(2, $email_address); // user_email
$query->bindParam(3, $user_name); // user_name
$results = $query->execute();
$query->closeCursor();
if (!$results) {
$error_mailer = "Unable to add user to database.";
// No reason to continue to send email if user_ID cannot be linked to user
header("Location: ./profile.php");
exit;
}
// add user_ID & survey_ID to distribution_list
$distributionKEY = uniqid(); // CREATE distributionKEY
$sql = "INSERT INTO distribution_list VALUES( ?, ?, ?, ? )";
$query = $pdo->prepare($sql);
$query->bindParam(1, $distributionKEY);
$query->bindParam(2, $survey_ID);
$query->bindParam(3, $user_ID);
$query->bindParam(4, $completed); // Value changed to '1' once the user takes the survey
$results = $query->execute();
$query->closeCursor();
if (!$results) {
$error_mailer = "Unable to add user to distribution list.";
exit;
}
// Get form data:
$email_subject = $_POST['email_subject'];
$email_body = $_POST['email_body'];
$survey_access_url = "http://elvis.rowan.edu/~shawt6/Survey.php?"
. "distributionKEY=" . $distributionKEY;
$mail = new PHPMailer(true); // TRUE enables exceptions
try {
$smtp_config = parse_ini_file(SMTP_FILE);
//Server settings
//$mail->SMTPDebug = 4; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = $smtp_config['smtp_host']; // Specify SMTP servers
$mail->SMTPAuth = true; // Enable authentication
$mail->Username = $smtp_config['smtp_user']; // SMTP username
$mail->Password = $smtp_config['smtp_pass']; // SMTP password
$mail->SMTPSecure = 'tls'; // Encryption type
$mail->Port = 587; // TCP port
//Recipients
$mail->setFrom($webmaster, 'Surveys');
$mail->addAddress($email_address); // Add recipient
if (isset($cc_address)) { $mail->addCC($cc_address); }
//Attachments, name is optional
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg');
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $email_subject;
$mail->Body = $_POST['recipient'] . "," . $eol
. $email_body . $eol
. "Please take our survey." . $eol
. "Your feedback is very important to us." . $eol
. $survey_access_url . $eol;
$mail->AltBody = "To view this message, please use an HTML compatible email viewer!";
$mail->send();
// SUCCESS
echo "<script>alert('Message has been sent');</script>";
} catch (Exception $e) {
$error_mailer = "Message could not be sent."
. "<br>"
. "Mailer Error: " . $mail->ErrorInfo;
}
}
?>
<div id="mail">
<div id="form">
<h2>Mail Form</h2>
<form name="form_send_email" action="" method="post" onSubmit="return form_validation(this)">
<input name="recipient" id="recipient" placeholder="Contact Name..." type="text" autofocus />
<input name="email_address" id="email_address" placeholder="Email Address..." type="text" />
<input name="email_subject" id="email_subject" placeholder="Subject..." cols="45" type="text" />
<textarea name="email_body" id="email_body" placeholder="Text Body..." type="text"></textarea>
<input name="send_email" type="submit" value=" Send Email " />
<div id="error_message"><?php echo $error_mailer; ?></div>
</form>
</div>
</div>
<button type="submit" ; id ="next">Next</button>
</div>
<br>
</div>
</form>
</div>
</div>
</body>
</html>