lots of work around newsletter (un)subscription (via double opt-in)

This commit is contained in:
eclipse 2025-02-27 23:09:20 +01:00
parent 5bdd09aac9
commit af2be617cd
10 changed files with 167 additions and 6 deletions

25
content/php/confirm.php Normal file
View File

@ -0,0 +1,25 @@
<?php
// inspired by https://www.mailgun.com/blog/email/double-opt-in-with-php-mailgun/
function AddMemberToDB($recipientAddress) {
/* global $mailingList, $mgClient;
$result = $mgClient->post("lists/$mailingList/members", array(
'address' => $recipientAddress,
'name' => $recipientName,
'description' => 'Form Opt In',
'subscribed' => true
)); */
}
require("settings.php");
$c = isset($_GET['c']) ? SanitizeInputs($_GET['c']) : NULL;
$e = isset($_GET['e']) ? SanitizeInputs($_GET['e']) : NULL;
if (isset($c) && isset($e) && CheckConfirmationHash($e, $c) && AddMemberToDB($e)) {
header('Location: /newsletter/confirmed.html');
} else {
header('Location: /newsletter/confirm-error.html')
}
?>

18
content/php/contact.php Normal file
View File

@ -0,0 +1,18 @@
<?php
//source: https://www.unixdude.net/posts/2017/Nov/29/pelican-contact-form/
if(isset($_POST['address']) && $_POST['address'] == ''){
//The form was submitted
$ouremail = 'kontakt@tobias-radloff.de';
// Important: if we add any form fields to the HTML,
// and want them included in the email, we will need to add them here also
$body = "Diese Nachricht wurde soeben durch das Kontaktformular auf t-r.de übermittelt:
Name: $_POST[name]
Emailadresse: $_POST[email]
Nachricht: $_POST[nachricht]";
// From:
$headers = "From: $_POST[email]";
// send the message
mail($ouremail, 'Nachricht ueber das Kontaktformular von t-r.de!', $body, $headers );
header('Location: /kontakt/danke/');
}
?>

24
content/php/settings.php Normal file
View File

@ -0,0 +1,24 @@
<?php
// inspired by https://www.mailgun.com/blog/email/double-opt-in-with-php-mailgun/
$domain = 'tobias-radloff.de';
$fromAddress = 'newsletter@tobias-radloff.de';
$siteURL = 'https://tobias-radloff.de';
$uniqueKey = '***REMOVED***'; // works like password salt
function SanitizeInputs($var) {
return = htmlspecialchars($var, ENT_QUOTES);
}
function SanitizeEmail ($var) {
$sane = htmlspecialchars($var, ENT_QUOTES);
$pattern = "/^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+$/";
preg_match($pattern, $sane, $res);
$r = $res[0] ? $res[0] : false;
return $r;
}
function CheckConfirmationHash($confEmail, $confCode) {
return (md5($confEmail . $uniqueKey) === $confCode);
}
?>

37
content/php/subscribe.php Normal file
View File

@ -0,0 +1,37 @@
<?php
// inspired by https://www.mailgun.com/blog/email/double-opt-in-with-php-mailgun/
function MakeConfirmationHash($confEmail, $confCode) {
return md5($confEmail . $confCode);
}
function SendConfirmationEmail($recipientAddress) {
global $domain, $fromAddress, $siteURL, $uniqueKey;
$hashedUnique = MakeConfirmationHash($recipientAddress, $uniqueKey);
$confirmURL = $siteURL . '/confirm.php?c=' . $hashedUnique . '&e=' . $recipientAddress
// FIXME
// use PHPMailer?
$to = $recipientAddress;
$subject = 'Newsletter-Anmeldung bestaetigen';
$body = '<p>Hallo! Bitte bestätige die Anmeldung für meinen Newsletter, indem du auf <a href="' . $confirmURL . '">diesen Link</a> klickst. Viel Spaß mit dem Gedicht!</p><p>Bis bald und viele Grüße<br>Tobias';
$headers = 'From: newsletter@' . $domain;
$result = mail($to, $subject, $body, $headers);
return $result;
}
require("settings.php");
if (isset($_POST['email'])) {
$email = SanitizeEmail(trim($_POST['email']));
echo $email; //DEBUG
$result = SendConfirmationEmail($email);
if $result == TRUE ) {
header('Location: /newsletter/subscribed.html');
} else {
header('Location: /newsletter/subscribe-error.html')
}
}
?>

View File

@ -0,0 +1,18 @@
<?php
function RemoveMemberfromDB($subscriberAddress) {
//FIXME
}
require("settings.php");
$email = isset($_GET['email']) ? SanitizeInputs($_GET['email']) : NULL;
if (isset($email)) {
$r = RemoveMemberfromDB($email);
if ($r) {
header('Location: /newsletter/unsubscribed.html');
} else {
header('Location: /newsletter/unsubscribe-error.html')
}
}
?>

View File

@ -20,15 +20,21 @@ THEME = "theme/"
PATH = "content" PATH = "content"
ARTICLE_PATHS = ["posts"] ARTICLE_PATHS = ["posts"]
PAGE_PATHS = ["pages"] PAGE_PATHS = ["pages"]
STATIC_PATHS = ["images", "favicon"] STATIC_PATHS = ["images", "favicon", "php"]
DIRECT_TEMPLATES = ['index', 'tags'] DIRECT_TEMPLATES = ['index', 'tags']
IGNORE_FILES = ['**/.*', '__pycache__', 'favicon-from-svg.sh', '*.metadata'] IGNORE_FILES = ['**/.*', '__pycache__', 'favicon-from-svg.sh', '*.metadata']
EXTRA_PATH_METADATA = { EXTRA_PATH_METADATA = {
'favicon/favicon.ico': {'path': 'favicon.ico'}, 'favicon/favicon.ico': {'path': 'favicon.ico'},
'php/settings.php': {'path': 'settings.php'},
'php/subscribe.php': {'path': 'subscribe.php'},
'php/confirm.php': {'path': 'confirm.php'},
'php/unsubscribes.php': {'path': 'unsubscribe.php'},
'php/contact.php': {'path': 'contact.php'}
} }
PAGE_URL = '{slug}/' PAGE_URL = '{slug}/'
PAGE_SAVE_AS = '{slug}/index.html' PAGE_SAVE_AS = '{slug}/index.html'
ARTICLE_SAVE_AS = '{category}/{slug}.html' ARTICLE_SAVE_AS = '{category}/{slug}.html'

View File

@ -27,6 +27,7 @@
Oxygen, Ubuntu, Cantarell, Helvetica, Arial, "Helvetica Neue", sans-serif, Oxygen, Ubuntu, Cantarell, Helvetica, Arial, "Helvetica Neue", sans-serif,
var(--pico-font-family-emoji); var(--pico-font-family-emoji);
--card-height: 300px; --card-height: 300px;
--smallest-width: 350px;
} }
/* don't underline links by default */ /* don't underline links by default */
@ -238,6 +239,22 @@ a {
margin-right: 0; margin-right: 0;
} }
/* style newsletter subscription form */
.newsletter-form {
display: flex;
flex-flow: row wrap;
}
.newsletter-email {
flex: 1 0 var(--smallest-width);
min-width: var(--smallest-width);
}
.newsletter-submit {
flex: 0 1 content;
}
/* Main content */ /* Main content */
#main-header h1, #main-header h1,
#site-footer p { #site-footer p {
@ -265,8 +282,8 @@ a {
} }
p { p {
flex: 0 1 350px; flex: 0 1 var(--smallest-width);
font-size: 2em; font-size: 1.5em;
} }
} }
@ -348,7 +365,7 @@ a {
.klappentext { .klappentext {
flex: 0 0 calc(50% - var(--pico-block-spacing-horizontal)); flex: 0 0 calc(50% - var(--pico-block-spacing-horizontal));
min-width: 350px; min-width: var(--smallest-width);
} }
.featured-image { .featured-image {
@ -358,7 +375,7 @@ a {
img { img {
max-height: calc(var(--card-height) * 2); max-height: calc(var(--card-height) * 2);
min-width: 350px; min-width: var(--smallest-width);
max-width: 100%; max-width: 100%;
aspect-ratio: auto; aspect-ratio: auto;
padding: var(--pico-spacing); padding: var(--pico-spacing);

View File

@ -1,6 +1,6 @@
{% extends "page.html" %} {% extends "page.html" %}
{% block content_body %} {% block content_body %}
<form id="contact-form" method="post" action="/theme/static/contact.php"> <form id="contact-form" method="post" action="{{ SITEURL }}/contact.php">
<fieldset> <fieldset>
<label> <label>
Name Name

View File

@ -0,0 +1,8 @@
<hgroup>
<h2>Abonniere meinen Newsletter!</h2>
<p>Erfahre zuerst von Neuerscheinungen, Lesungen und allem anderen. Mein Dankeschön: ein unveröffentlichtes Gedicht.</p>
</hgroup>
<form class="newsletter-form" method="post" action="{{ SITEURL }}/subscribe.php">
<input class="newsletter-email" type="email" name="email" placeholder="Emailadresse" autocomplete="email" aria-label="Emailadresse" required/>
<input class="newsletter-submit" type="submit" value="Abonnieren" />
</form>

View File

@ -0,0 +1,8 @@
<hgroup>
<h2>Newsletter-Abmeldung</h2>
<p>Komm bald wiederm okay?</p>
</hgroup>
<form class="newsletter-form" method="get" action="{{ SITEURL }}/unsubscribe.php">
<input class="newsletter-email" type="email" name="email" placeholder="Emailadresse" autocomplete="email" aria-label="Emailadresse" required/>
<input class="newsletter-submit" type="submit" value="Abonnieren" />
</form>