开发者

simple mailing list

Can anybody give me details on what to code into a page?

Basically i am required to build a mailing list from scratch w开发者_Go百科here people can subscribe and they are added to a mailing list.

What would be the php/javascript behind it?


A mailing list is quite simple. You'd need to decide how you'd want to do it, but in general, you'd have a subscribe page and a post page. I am not testing this code, so it may contain flaws, but it should give you an idea of a flat-file based mailing list.

In practical implementations, you should use MySQL, validate and confirm e-mails, check for errors and so on. Also, don't forget that this does not authenticate the post page. You ideally need strong authentication to ensure your mailing list isn't compromised. Additionally know that you -must- have an unsubscribe feature - this will be easier with MySQL than with flat-file.

subscribe.php

<?php
// Has the form been posted?
if(isset($_POST['email']))
{
  // Append the submitted e-mail to the list.
  $file = fopen('list.txt', 'a');
  fputs($file, $_POST['email'] . "\n");
  fclose($file);

  // Send a message to the browser.
  die('Added to mailing list.');
}
?>
<html>
 <head>
  <title>Subscribe to Mailing List</title>
 </head>
 <body>
  <form action="#" method="post">
   <input type="text" name="email" />
   <input type="submit" value="Submit" />
  </form>
 </body>
</html>

post.php

<?php
// Has the form been submitted?
if(isset($_POST['body']))
{
  // This should load the file into $lines, as an array of, well, lines.
  $lines = file('list.txt');

  // For each line, send a message. $line should contain an e-mail address.
  foreach($lines as $line)
    mail($line, $_POST['subject'], $_POST['body']);

  // Send a message to the browser.
  die("Message delivered.");
}
?>
<html>
 <head>
  <title>Post to Mailing List</title>
 </head>
 <body>
  <h1>Post</h1>
  <form action="#" method="post">
   <input type="text" name="subject" /><br/>
   <textarea name="body"></textarea><br/>
   <input type="submit" value="Submit" />
  </form>
 </body>
</html>


There's actually more behind it - more MySQL. You'll need MySQL, PHP and Javascript (for UI and client-side validation)

Where do you want to began?

Simple Model: User -> inputs email -> validated via javascript -> if (true) -> submits via POST/GET -> validates PHP -> if (true) -> goes into MySQL database.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜