123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- <?php
- require_once("PHPMailer/PHPMailer.php");
- require_once("PHPMailer/SMTP.php");
- require_once("PHPMailer/Exception.php");
- require_once("mailconf.php");
- use PHPMailer\PHPMailer\PHPMailer;
- use PHPMailer\PHPMailer\SMTP;
- $mail = new PHPMailer(true);
- if(!isset($GLOBALS['MAILSUB_CONFIRM'])){
- $GLOBALS['MAILSUB_CONFIRM']="<h2>Confirm Subscription</h2>
- <p>You are subscribing to my email newsletter. Click the link below to confirm:</p><br />
- <a href='{CONFIRMLINK}'>{CONFIRMLINK}</a>";
- }
- if(!isset($GLOBALS['MAILSUB_REGULAR'])){
- $GLOBALS['MAILSUB_REGULAR']="
- <p>This is my email newsletter. <a href='{EDITSUBSCRIPTION}'>Edit your subscription</a></p><br />";
- }
- if(!isset($GLOBALS['MAILSUB_WELCOME'])){
- $GLOBALS['MAILSUB_WELCOME']="<h2>Welcome</h2>
- <p>You have subscribed to my email newsletter. Edit your subscription below:</p><br />
- <a href='{EDITSUBSCRIPTION}'>{EDITSUBSCRIPTION}</a>";
- }
- if(!isset($GLOBALS['MAILSUB_EDIT'])){
- $GLOBALS['MAILSUB_EDIT']="<h2>Edit Subscription</h2>
- <p>Unsubscribe my newsletter: <a href='{UNSUBSCRIBE}'>CONFIRM</a></p><br />";
- }
- if(!isset($GLOBALS['MY_HOST'])){
- $GLOBALS['MY_HOST']="https://www.example.com";
- }
- $PENDING=[];
- $ACCEPTED=[];
- $f = fopen("pending_confirm.php",'r'); $tries=0;
- while(!$f && $tries<100){ $f = fopen("pending_confirm.php",'w'); fclose($f); $tries++;} $f=0;
- $content = file_get_contents("pending_confirm.php");
- $f = fopen("all_subscribers.php",'r'); $tries=0;
- while(!$f && $tries<100){ $f = fopen("all_subscribers.php",'w'); fclose($f); $tries++;} $f=0;
- $subscribers = file_get_contents("all_subscribers.php");
- if(preg_match_all("/(.*)\s+?\[\[\[(.*)\]\]\]\s*?(zh|en|)/",$content,$matches,PREG_SET_ORDER)){
- foreach($matches as $m){ $PENDING[]=[$m[1],$m[2],$m[3]]; }
- }
- if(preg_match_all("/(.*)\s+?\[\[\[(.*)\]\]\]\s*?(zh|en|)/",$subscribers,$matches,PREG_SET_ORDER)){
- foreach($matches as $m){ $ACCEPTED[]=[$m[1],$m[2],$m[3]]; }
- }
- if(isset($_GET['subscribe']) && isset($_POST['email_subscribe_name'])){
- if(preg_match("/^((?!\.)[\w\-_.]*[^.])(@\w+)(\.\w+(\.\w+)?[^.\W])$/",$_POST['email_subscribe_name'])){
- $item=[uniqid(),$_POST['email_subscribe_name'],'en'];
- $PENDING[]=$item;
- send_verification($item[0],$_POST['email_subscribe_name']);
- }else{
- echo "Not a valid email.";
- }
- $tries=0; while(!$f && $tries<100){ $f = fopen("pending_confirm.php",'w'); $tries++; }
- fwrite($f,"<php".PHP_EOL);
- foreach($PENDING as $p){ if($p[1]==0) continue; fwrite($f,"".$p[0]." [[[".$p[1]."]]] $p[2]".PHP_EOL); }
- fclose($f); $f=0;
- }
- if(isset($_GET['confirm']) && isset($_GET['id'])){
- $address=urldecode($_GET['confirm']); $any=0;
- foreach($PENDING as $p){
- if($p[0]==$_GET['id'] && $address==$p[1]){
- $ACCEPTED[]=$p; unset($PENDING[key($PENDING)]); $any=1;
- send_welcome($p[0],$address); break;
- }
- }
- if(!$any){ echo "<h2>Couldn't confirm your subscription.</h2>"; exit; }
- $tries=0; while(!$f && $tries<100){ $f = fopen("all_subscribers.php",'w'); $tries++; }
- fwrite($f,"<php".PHP_EOL);
- foreach($ACCEPTED as $p){ if($p[1]==0) continue; fwrite($f,"".$p[0]." [[[".$p[1]."]]] $p[2]".PHP_EOL); }
- fclose($f); $f=0;
- $tries=0; while(!$f && $tries<100){ $f = fopen("pending_confirm.php",'w'); $tries++; }
- fwrite($f,"<php".PHP_EOL);
- foreach($PENDING as $p){ if($p[1]==0) continue; fwrite($f,"".$p[0]." [[[".$p[1]."]]] $p[2]".PHP_EOL); }
- fclose($f); $f=0;
- echo "<h2>Successfully subscribed</h2>";
- }
- if(isset($_GET['unsubscribe'])&&isset($_GET['id'])){
- if(!isset($_GET['doit'])){
- $link="{$GLOBALS['MY_HOST']}/mailsub.php?unsubscribe={$_GET['unsubscribe']}&doit=1&id={$_GET['id']}";
- $body=preg_replace("/\{UNSUBSCRIBE\}/",$link,$GLOBALS['MAILSUB_EDIT']);
- echo $body; exit;
- }
- $any=0;
- foreach(array_values($ACCEPTED) as $key=>$p){
- if($p[0]==$_GET['id'] && $_GET['unsubscribe']==$p[1]){
- unset($ACCEPTED[$key]); $any=1;
- }
- }
- if(!$any){ echo "<h2>Unknown error</h2>"; exit; }
- $tries=0; while(!$f && $tries<100){ $f = fopen("all_subscribers.php",'w'); $tries++; }
- fwrite($f,"<php".PHP_EOL);
- foreach($ACCEPTED as $p){ if($p[1]==0) continue; fwrite($f,"".$p[0]." [[[".$p[1]."]]] $p[2]".PHP_EOL); }
- fclose($f); $f=0;
- $tries=0; while(!$f && $tries<100){ $f = fopen("pending_confirm.php",'w'); $tries++; }
- fwrite($f,"<php".PHP_EOL);
- foreach($PENDING as $p){ if($p[1]==0) continue; fwrite($f,"".$p[0]." [[[".$p[1]."]]] $p[2]".PHP_EOL); }
- fclose($f); $f=0;
- echo "<h2>Successfully unsubscribed</h2>";
- }
- function send_email($to,$subject,$body){
- try {
- global $mail;
- global $mailconf;
- //Server settings
- //$mail->SMTPDebug = SMTP::DEBUG_SERVER; //Enable verbose debug output
- $mail->isSMTP(); //Send using SMTP
- $mail->Host = $mailconf['host']; //Set the SMTP server to send through
- $mail->SMTPAuth = true; //Enable SMTP authentication
- $mail->Username = $mailconf['username']; //SMTP username
- $mail->Password = $mailconf['password']; //SMTP password
- $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; //Enable implicit TLS encryption
- $mail->Port = 587; //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`
-
- //Recipients
- $mail->setFrom($mailconf['from'], $mailconf['name']);
- $mail->addAddress($to);
-
- //Content
- $mail->isHTML(true); //Set email format to HTML
- $mail->Subject = $subject;
- $mail->Body = $body;
-
- $mail->send();
-
- return null;
- } catch (Exception $e) {
- echo "Message could not be sent."; #Mailer Error: {$mail->ErrorInfo}";
- return -1;
- }
- }
- function send_verification($id,$address){
- $addressencode=urlencode($address);
- $link="{$GLOBALS['MY_HOST']}/mailsub.php?confirm=$addressencode&id=$id";
- $body=preg_replace("/\{CONFIRMLINK\}/",$link,$GLOBALS['MAILSUB_CONFIRM']);
- if(!send_email($address,"Confirm Subscription",$body)){
- echo "Please check your inbox for confirmation e-mail.";
- }else{
- echo "Error sending email.";
- }
- }
- function send_welcome($id,$address){
- $addressencode=urlencode($address);
- $link="{$GLOBALS['MY_HOST']}/mailsub.php?unsubscribe=$addressencode&id=$id";
- $body=preg_replace("/\{EDITSUBSCRIPTION\}/",$link,$GLOBALS['MAILSUB_WELCOME']);
- send_email($address,"Welcome!",$body);
- }
|