Skip to content

Commit 839c2fa

Browse files
committed
Add newsletter lesson
1 parent f807b1d commit 839c2fa

File tree

9 files changed

+131
-0
lines changed

9 files changed

+131
-0
lines changed

PHP/newsletter/.mdb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"backend": {
3+
"platform": "php7.4"
4+
},
5+
"meta": {
6+
"type": "backend"
7+
}
8+
}

PHP/newsletter/compose.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<form action="send.php" method="POST">
2+
<label for="subject">Subject: <br><input type="text" name="subject"></label><br>
3+
<label for="body">Body of the email: <br> <textarea name="body" cols="30" rows="10"></textarea></label> <br>
4+
<input type="submit" value="Send">
5+
</form>

PHP/newsletter/connection.inc.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
$user = 'newsletteree02baad';
4+
$password = 'Secret.1';
5+
$host = '10.0.0.57';
6+
$dbase = 'newsletter25361119';
7+
$table = 'subscribers';
8+
9+
10+
// Connect to DBase
11+
$dbc = mysqli_connect($host, $user, $password, $dbase)
12+
or die("Unable to connect");
13+
14+
15+
$query = "CREATE TABLE IF NOT EXISTS $table ( `id` INT NOT NULL AUTO_INCREMENT , `first_name` TEXT NULL , `last_name` TEXT NULL , `email` VARCHAR(60) NOT NULL , `date` DATE NOT NULL , PRIMARY KEY (`id`), UNIQUE `uniqueemail` (`email`)) ENGINE = InnoDB;";
16+
17+
18+
$result = mysqli_query($dbc, $query)
19+
or die('Error creating table. ' . mysqli_error($dbc));

PHP/newsletter/index.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<h1>Sign up for a my newsletter:</h1>
2+
3+
4+
<form action="subscribe.php" method="post">
5+
<label for="firstname"> <input type="text" name="firstname"> First name</label> <br>
6+
<label for="lastname"> <input type="text" name="lastname"> Last name</label><br>
7+
<label for="email"> <input type="text" name="email"> Email: name</label><br>
8+
<input type="submit" name="submit" value="Subscribe">
9+
</form>

PHP/newsletter/out.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<h1>Please enter the email for which you want to unsubscribe:</h1>
2+
3+
<form action="unscubscribe.php" method="POST">
4+
<label for="email">Email: <input type="text" name="email"></label> <br>
5+
<input type="submit" value="Unsubscribe">
6+
</form>

PHP/newsletter/package.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "newsletter",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"author": "",
10+
"license": "ISC"
11+
}

PHP/newsletter/send.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
require_once 'connection.inc.php';
3+
4+
$subject = $_POST['subject'];
5+
$body = $_POST['body'];
6+
7+
$headers = 'From: [email protected]' . '\n';
8+
$headers .= 'Reply-To: [email protected]' . '\n';
9+
$headers .= 'Content-Type: text/plain; charset="iso-8859-1"' . "\n";
10+
$headers .= 'Content-Transfer-Encoding: 8bit';
11+
12+
$query = "SELECT * FROM $table";
13+
$result = mysqli_query($dbc, $query)
14+
or die('Error querying database' . mysqli_error($dbc));
15+
16+
while ($row = mysqli_fetch_array($result)) {
17+
$first_name = $row['first_name'];
18+
$last_name = $row['last_name'];
19+
$email = $row['email'];
20+
21+
$msg = "Dear $first_name $last_name, \n $body";
22+
23+
if (mail($email, $subject, $msg, $headers)) {
24+
echo 'Email sent to ' . $email . '<br>';
25+
} else {
26+
echo 'Error while sending an email to: ' . $email;
27+
}
28+
}
29+
30+
31+
32+
mysqli_close($dbc);

PHP/newsletter/subscribe.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
require_once 'connection.inc.php';
3+
4+
5+
$first_name = $_POST['firstname'];
6+
$last_name = $_POST['lastname'];
7+
$email = $_POST['email'];
8+
9+
10+
11+
$query = "SELECT email FROM $table where email='$email'";
12+
$result = $dbc->query($query);
13+
14+
if ($result->num_rows > 0) {
15+
echo 'This email has been already subscribed. ';
16+
} else {
17+
18+
$date = date("Y-m-d H:i:s");
19+
20+
$query = "INSERT INTO $table (first_name, last_name, email, date) VALUES ('$first_name', '$last_name', '$email', '$date')";
21+
22+
mysqli_query($dbc, $query)
23+
or die("Error inserting email" . mysqli_error($dbc));
24+
25+
echo 'You have been successfully added.';
26+
}
27+
28+
mysqli_close($dbc);

PHP/newsletter/unscubscribe.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
require_once 'connection.inc.php';
3+
4+
$email = $_POST['email'];
5+
6+
$query = " DELETE FROM $table WHERE email='$email'";
7+
8+
$result = mysqli_query($dbc, $query)
9+
or die("Error removing email from database" . mysqli_error($dbc));
10+
11+
echo "You have been successfully unsubscribed";
12+
13+
mysqli_close($dbc);

0 commit comments

Comments
 (0)