*/}}

mailsub.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. require_once("PHPMailer/PHPMailer.php");
  3. require_once("PHPMailer/SMTP.php");
  4. require_once("PHPMailer/Exception.php");
  5. require_once("mailconf.php");
  6. use PHPMailer\PHPMailer\PHPMailer;
  7. use PHPMailer\PHPMailer\SMTP;
  8. $mail = new PHPMailer(true);
  9. if(!isset($GLOBALS['MAILSUB_CONFIRM'])){
  10. $GLOBALS['MAILSUB_CONFIRM']="<h2>Confirm Subscription</h2>
  11. <p>You are subscribing to my email newsletter. Click the link below to confirm:</p><br />
  12. <a href='{CONFIRMLINK}'>{CONFIRMLINK}</a>";
  13. }
  14. if(!isset($GLOBALS['MAILSUB_REGULAR'])){
  15. $GLOBALS['MAILSUB_REGULAR']="
  16. <p>This is my email newsletter. <a href='{EDITSUBSCRIPTION}'>Edit your subscription</a></p><br />";
  17. }
  18. if(!isset($GLOBALS['MAILSUB_WELCOME'])){
  19. $GLOBALS['MAILSUB_WELCOME']="<h2>Welcome</h2>
  20. <p>You have subscribed to my email newsletter. Edit your subscription below:</p><br />
  21. <a href='{EDITSUBSCRIPTION}'>{EDITSUBSCRIPTION}</a>";
  22. }
  23. if(!isset($GLOBALS['MAILSUB_EDIT'])){
  24. $GLOBALS['MAILSUB_EDIT']="<h2>Edit Subscription</h2>
  25. <p>Unsubscribe my newsletter: <a href='{UNSUBSCRIBE}'>CONFIRM</a></p><br />";
  26. }
  27. if(!isset($GLOBALS['MY_HOST'])){
  28. $GLOBALS['MY_HOST']="https://www.example.com";
  29. }
  30. $PENDING=[];
  31. $ACCEPTED=[];
  32. $f = fopen("pending_confirm.php",'r'); $tries=0;
  33. while(!$f && $tries<100){ $f = fopen("pending_confirm.php",'w'); fclose($f); $tries++;} $f=0;
  34. $content = file_get_contents("pending_confirm.php");
  35. $f = fopen("all_subscribers.php",'r'); $tries=0;
  36. while(!$f && $tries<100){ $f = fopen("all_subscribers.php",'w'); fclose($f); $tries++;} $f=0;
  37. $subscribers = file_get_contents("all_subscribers.php");
  38. if(preg_match_all("/(.*)\s+?\[\[\[(.*)\]\]\]\s*?(zh|en|)/",$content,$matches,PREG_SET_ORDER)){
  39. foreach($matches as $m){ $PENDING[]=[$m[1],$m[2],$m[3]]; }
  40. }
  41. if(preg_match_all("/(.*)\s+?\[\[\[(.*)\]\]\]\s*?(zh|en|)/",$subscribers,$matches,PREG_SET_ORDER)){
  42. foreach($matches as $m){ $ACCEPTED[]=[$m[1],$m[2],$m[3]]; }
  43. }
  44. if(isset($_GET['subscribe']) && isset($_POST['email_subscribe_name'])){
  45. if(preg_match("/^((?!\.)[\w\-_.]*[^.])(@\w+)(\.\w+(\.\w+)?[^.\W])$/",$_POST['email_subscribe_name'])){
  46. $item=[uniqid(),$_POST['email_subscribe_name'],'en'];
  47. $PENDING[]=$item;
  48. send_verification($item[0],$_POST['email_subscribe_name']);
  49. }else{
  50. echo "Not a valid email.";
  51. }
  52. $tries=0; while(!$f && $tries<100){ $f = fopen("pending_confirm.php",'w'); $tries++; }
  53. fwrite($f,"<php".PHP_EOL);
  54. foreach($PENDING as $p){ if($p[1]==0) continue; fwrite($f,"".$p[0]." [[[".$p[1]."]]] $p[2]".PHP_EOL); }
  55. fclose($f); $f=0;
  56. }
  57. if(isset($_GET['confirm']) && isset($_GET['id'])){
  58. $address=urldecode($_GET['confirm']); $any=0;
  59. foreach($PENDING as $p){
  60. if($p[0]==$_GET['id'] && $address==$p[1]){
  61. $ACCEPTED[]=$p; unset($PENDING[key($PENDING)]); $any=1;
  62. send_welcome($p[0],$address); break;
  63. }
  64. }
  65. if(!$any){ echo "<h2>Couldn't confirm your subscription.</h2>"; exit; }
  66. $tries=0; while(!$f && $tries<100){ $f = fopen("all_subscribers.php",'w'); $tries++; }
  67. fwrite($f,"<php".PHP_EOL);
  68. foreach($ACCEPTED as $p){ if($p[1]==0) continue; fwrite($f,"".$p[0]." [[[".$p[1]."]]] $p[2]".PHP_EOL); }
  69. fclose($f); $f=0;
  70. $tries=0; while(!$f && $tries<100){ $f = fopen("pending_confirm.php",'w'); $tries++; }
  71. fwrite($f,"<php".PHP_EOL);
  72. foreach($PENDING as $p){ if($p[1]==0) continue; fwrite($f,"".$p[0]." [[[".$p[1]."]]] $p[2]".PHP_EOL); }
  73. fclose($f); $f=0;
  74. echo "<h2>Successfully subscribed</h2>";
  75. }
  76. if(isset($_GET['unsubscribe'])&&isset($_GET['id'])){
  77. if(!isset($_GET['doit'])){
  78. $link="{$GLOBALS['MY_HOST']}/mailsub.php?unsubscribe={$_GET['unsubscribe']}&doit=1&id={$_GET['id']}";
  79. $body=preg_replace("/\{UNSUBSCRIBE\}/",$link,$GLOBALS['MAILSUB_EDIT']);
  80. echo $body; exit;
  81. }
  82. $any=0;
  83. foreach(array_values($ACCEPTED) as $key=>$p){
  84. if($p[0]==$_GET['id'] && $_GET['unsubscribe']==$p[1]){
  85. unset($ACCEPTED[$key]); $any=1;
  86. }
  87. }
  88. if(!$any){ echo "<h2>Unknown error</h2>"; exit; }
  89. $tries=0; while(!$f && $tries<100){ $f = fopen("all_subscribers.php",'w'); $tries++; }
  90. fwrite($f,"<php".PHP_EOL);
  91. foreach($ACCEPTED as $p){ if($p[1]==0) continue; fwrite($f,"".$p[0]." [[[".$p[1]."]]] $p[2]".PHP_EOL); }
  92. fclose($f); $f=0;
  93. $tries=0; while(!$f && $tries<100){ $f = fopen("pending_confirm.php",'w'); $tries++; }
  94. fwrite($f,"<php".PHP_EOL);
  95. foreach($PENDING as $p){ if($p[1]==0) continue; fwrite($f,"".$p[0]." [[[".$p[1]."]]] $p[2]".PHP_EOL); }
  96. fclose($f); $f=0;
  97. echo "<h2>Successfully unsubscribed</h2>";
  98. }
  99. function send_email($to,$subject,$body){
  100. try {
  101. global $mail;
  102. global $mailconf;
  103. //Server settings
  104. //$mail->SMTPDebug = SMTP::DEBUG_SERVER; //Enable verbose debug output
  105. $mail->isSMTP(); //Send using SMTP
  106. $mail->Host = $mailconf['host']; //Set the SMTP server to send through
  107. $mail->SMTPAuth = true; //Enable SMTP authentication
  108. $mail->Username = $mailconf['username']; //SMTP username
  109. $mail->Password = $mailconf['password']; //SMTP password
  110. $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; //Enable implicit TLS encryption
  111. $mail->Port = 587; //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`
  112. //Recipients
  113. $mail->setFrom($mailconf['from'], $mailconf['name']);
  114. $mail->addAddress($to);
  115. //Content
  116. $mail->isHTML(true); //Set email format to HTML
  117. $mail->Subject = $subject;
  118. $mail->Body = $body;
  119. $mail->send();
  120. return null;
  121. } catch (Exception $e) {
  122. echo "Message could not be sent."; #Mailer Error: {$mail->ErrorInfo}";
  123. return -1;
  124. }
  125. }
  126. function send_verification($id,$address){
  127. $addressencode=urlencode($address);
  128. $link="{$GLOBALS['MY_HOST']}/mailsub.php?confirm=$addressencode&id=$id";
  129. $body=preg_replace("/\{CONFIRMLINK\}/",$link,$GLOBALS['MAILSUB_CONFIRM']);
  130. if(!send_email($address,"Confirm Subscription",$body)){
  131. echo "Please check your inbox for confirmation e-mail.";
  132. }else{
  133. echo "Error sending email.";
  134. }
  135. }
  136. function send_welcome($id,$address){
  137. $addressencode=urlencode($address);
  138. $link="{$GLOBALS['MY_HOST']}/mailsub.php?unsubscribe=$addressencode&id=$id";
  139. $body=preg_replace("/\{EDITSUBSCRIPTION\}/",$link,$GLOBALS['MAILSUB_WELCOME']);
  140. send_email($address,"Welcome!",$body);
  141. }